[Python-ideas] make __closure__ writable

Andrew Svetlov andrew.svetlov at gmail.com
Thu Mar 22 21:56:30 CET 2012


>From my perspective Yury's patch is simple and clean.
Hi described very well why decorators (which often implemented as
functions which own __closure__) cannot be easy patched.

While overriding __closure__ is not common case it's hard to do that
if you really need.
If __code__ is already writable there are no reason to prevent writing
to __closure__ as well.

I am +1 to push the patch in 3.3.

On Tue, Mar 20, 2012 at 4:59 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> On 2012-03-20, at 10:56 AM, Yury Selivanov wrote:
>
>> Because usually you write decorators as functions, not classes.  And
>> when you do the former style, you usually do it in the following way:
>>
>> def decorator(func):
>>    def wrapper(*args, **kwargs):
>>        return func(*args, **kwargs)
>>
>>    functools.wraps(wrapper, func)
>>    return wrapper
>>
>> Now, let's use it:
>>
>> @decorator
>> def some_func(): pass
>>
>> OK.  At this point, 'some_func' object has a '__wrapped__' attribute,
>> that points to original 'some_func' function.  But whatever you write
>> to 'some_func.__wrapped__' won't change anything, as the 'wrapper'
>> will continue to call old 'some_func'.  Instead of assigning something
>> to __wrapped__, we need to change it in-place, by doing
>> '__wrapped__.__closure__ = new_closure'.
>
>
> And as I told you in the first example: there is no problem when you have
> only one decorator.  You can surely just return a new FunctionType().
>
> But when you have many of them, such as:
>
> @decorator3
> @your_magic_decorator_that_modifies_the_closure
> @decorator2
> @decorator1
> def some_func(): pass
>
> The only way to modify the __closure__ it to write to the __wrapped__
> attribute of 'decorator1' wrapper.
>
> -
> Yury
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



-- 
Thanks,
Andrew Svetlov



More information about the Python-ideas mailing list