classification
Title: locals() behaviour differs when tracing is in effect
Type: behavior Stage:
Components: Extension Modules, Interpreter Core Versions: Python 2.4, Python 3.1, Python 2.6, Python 2.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, gvanrossum, pitrou, terry.reedy
Priority: normal Keywords:

Created on 2009-10-08 13:49 by andbj, last changed 2009-10-08 20:01 by andbj. This issue is now closed.

Files
File name Uploaded Description Edit
localstest.py andbj, 2009-10-08 13:49
Messages (5)
msg93745 - (view) Author: André Bjärby (andbj) Date: 2009-10-08 13:49
Running the attached script shows that locals() behaves differently
depending on whether sys.settrace() is active.

This does not occur when calling locals() in the global scope, only when
used inside functions.
msg93747 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-10-08 15:37
The same is thing is true of the frame's f_locals attribute. This
attribute is a copy of the local variables in the frame, because the
internal storage of these variables is a raw C array for faster access.
This copy is only synchronized back when a tracing function returns, so
as to allow implementing a debugger.

>>> def f():
...   a = 1
...   l = sys._getframe().f_locals
...   b = 2
...   return l
... 
>>> f()
{'a': 1}

The above optimization (raw C array for faster access of local
variables) is not done at the global scope, and therefore locals() at
that scope give you direct access to the variables' internal store
(which is, actually, the module's __dict__).

>>> import __main__
>>> __main__.__dict__ is locals()
True
msg93750 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2009-10-08 16:58
So there's no bug here right? It's even documented.
msg93751 - (view) Author: André Bjärby (andbj) Date: 2009-10-08 17:35
It's documented that "The contents of this dictionary should not be
modified; changes may not affect the values of local variables used by
the interpreter."

The script does the opposite. Perhaps an addition to the documentation?
"The contents of this dictionary may not be affected by local variables
used by the interpreter".
msg93758 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-10-08 19:02
I think the doc is clear enough if one reads and believes it.  The main
doc is "Update and return a dictionary representing the current local
symbol table." That pretty clearly says that subsequent changes to the
local symbol table may make the dict out of date until updated by a
subsequent call.
History
Date User Action Args
2009-10-08 20:01:24andbjsetnosy: - andbj

components: + Extension Modules, Interpreter Core, - Documentation
versions: + Python 2.5, Python 2.4, - Python 2.7, Python 3.2
2009-10-08 19:02:02terry.reedysetnosy: + terry.reedy
messages: + msg93758
2009-10-08 17:37:52pitrousetassignee: georg.brandl

nosy: + georg.brandl
components: + Documentation, - Extension Modules, Interpreter Core
versions: + Python 2.7, Python 3.2, - Python 2.5, Python 2.4
2009-10-08 17:35:50andbjsetmessages: + msg93751
2009-10-08 16:58:24gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg93750

resolution: rejected
2009-10-08 15:37:19pitrousetnosy: + pitrou
messages: + msg93747
2009-10-08 13:49:57andbjcreate