Strange behaviour of CO_NEWLOCALS and function
Jeremy Hylton
jeremy at alum.mit.edu
Wed Apr 10 23:52:19 EDT 2002
Boris Boutillier <boris at cantal.lip6.fr> wrote in message news:<pan.2002.04.10.17.29.41.396116.31396 at cantal.lip6.fr>...
> Here is a little example, where I create a new codeobject from one of a
> function by just removing the CO_NEWLOCALS flags :
Boris, I've got no idea what you're trying to accomplish, and I think
I'm happier that way :-).
> The strange thing is that no new dictionnary is created as expected, but
> local variable have been deleted from this dictionnary, why is this ? Is
> there some way to modify my code to avoid this deletion ?
If you want to understand what CO_NEWLOCALS does, please read the
source of frameobject.c. It's not documented, and I don't expect it
ever will be. I think the basic idea is: If CO_NEWLOCALS is set,
create a new locals dict (f_locals attribute) for the frame. If not,
use f_globals for f_locals.
In your example, I don't believe there's any deletion going on. 't'
is set after the call to locals(), so you shouldn't expect to see it.
If you call locals() aftering binding 't', I expect you will see it.
Note that regardless of whether CO_NEWLOCALS is set, the code compiled
for a function body is going to use "fast locals." The compiler
generates LOAD_FAST and STORE_FAST instructions that take an integer
argument that specifies the array index of the PyObject * in the
frame's array of local variables.
Jeremy
More information about the Python-list
mailing list