Change function closure cell contents

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Change function closure cell contents

Tin Tvrtković
Hello!

Here's a very simple example (Python 3):

>>>> class C:
....     def t(self):
....         return __class__
....
>>>> C.t.__closure__[0]
<cell at 0x0000000109eca0c0: type object at 0x00007f7f82c225b0>


My question is, what's the easiest/most natural way of changing the contents of this closure cell on PyPy? On CPython there's a way using ctypes.

This is regarding
https://github.com/python-attrs/attrs/issues/102, but the issue boils down to this.

Cheers!

_______________________________________________
pypy-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/pypy-dev
Reply | Threaded
Open this post in threaded view
|

Re: Change function closure cell contents

Armin Rigo-2
Hi Tin,

On 27 July 2017 at 16:17, Tin Tvrtković <[hidden email]> wrote:
> My question is, what's the easiest/most natural way of changing the contents
> of this closure cell on PyPy?

On PyPy you can do (tested on PyPy2):

    cell = g.func_closure[0]
    cell.__setstate__((43,))    # sets the 'cell_contents' attribute to 43


Armin
_______________________________________________
pypy-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/pypy-dev
Reply | Threaded
Open this post in threaded view
|

Re: Change function closure cell contents

Tin Tvrtković
That seems to work!

I'd just like to add, Armin, that whenever I have a question you are almost always the first to respond and your replies are always very helpful and insightful. It is very much appreciated; thank you. :)

On Thu, Jul 27, 2017 at 11:59 PM Armin Rigo <[hidden email]> wrote:
Hi Tin,

On 27 July 2017 at 16:17, Tin Tvrtković <[hidden email]> wrote:
> My question is, what's the easiest/most natural way of changing the contents
> of this closure cell on PyPy?

On PyPy you can do (tested on PyPy2):

    cell = g.func_closure[0]
    cell.__setstate__((43,))    # sets the 'cell_contents' attribute to 43


Armin

_______________________________________________
pypy-dev mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/pypy-dev