locals() different behaviour when tracing?

Is it intentional that locals() returns a copy/snapshot of the local variables sometimes, and sometimes a reference? Like when enabling tracing, such as in the code pasted below. The documentation ("Update and return a dictionary containing the current scope's local variables.") is pretty unclear.
import sys
def X(): l = locals() i = "foo" print "Is 'i' in stored locals()? ", ('i' in l)
print "Running normally" X()
print "Enabling tracing" def t(*a): return t sys.settrace(t) X()

2009/10/8 Anders Waldenborg anders@0x63.nu:
Is it intentional that locals() returns a copy/snapshot of the local variables sometimes, and sometimes a reference? Like when enabling tracing, such as in the code pasted below. The documentation ("Update and return a dictionary containing the current scope's local variables.") is pretty unclear.
Yes, it does, and that's why the documentation says changing it is undefined. :)

Anders Waldenborg <anders <at> 0x63.nu> writes:
Is it intentional that locals() returns a copy/snapshot of the local variables sometimes, and sometimes a reference? Like when enabling tracing, such as in the code pasted below.
Since someone else opened a bug, I answered there. Anyone, feel free to correct me if my answer is wrong. http://bugs.python.org/issue7083
Regards
Antoine.
participants (3)
-
Anders Waldenborg
-
Antoine Pitrou
-
Benjamin Peterson