[Python-ideas] make __closure__ writable
Yury Selivanov
yselivanov.ml at gmail.com
Mon Mar 19 16:29:07 CET 2012
On 2012-03-16, at 9:44 PM, Andrew Svetlov wrote:
> I'm ok with mutable __closure__ but can you point the real use case?
Well, we need mutable __closure__ to be able to inject some constants
or objects to the namespace the corresponding __code__ works with.
If you want to know why on earth did we need to mess with the __code__
object at all, that's to control the execution of the 'finally' statement
in generator-based coroutines. We modify the __code__ of generators to
signal when they are executing in their 'finally' blocks, and when they
are, we never abort the execution (by timeout, for instance). The code
we inject needs to call one function, and for now, we just inject that
function to the generator's __globals__, but the cleaner solution would
be to just modify its __closure__. BTW, that's the real problem many
coroutine-based frameworks will encounter some day.
While this may all sound too complicated, the case is real. We had an
option to either patch CPython (and later PyPy), or to inject the needed
opcodes in the __code__ object directly. We found that the latter is more
preferable.
So as I said: I see no reason in protecting the __closure__ attribute,
when the __code__ object is writeable.
-
Yury Selivanov
http://sprymix.com
More information about the Python-ideas
mailing list