Issue7083
Created on 2009-10-08 13:49 by andbj, last changed 2009-10-08 20:01 by andbj.
| File name |
Uploaded |
Description |
Edit |
Remove |
|
localstest.py
|
andbj,
2009-10-08 13:49
|
|
|
|
|
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) |
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) |
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 (tjreedy) |
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.
|
|
| Date |
User |
Action |
Args |
| 2009-10-08 20:01:24 | andbj | set | nosy:
- 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:02 | tjreedy | set | nosy:
+ tjreedy messages:
+ msg93758
|
| 2009-10-08 17:37:52 | pitrou | set | assignee: 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:50 | andbj | set | messages:
+ msg93751 |
| 2009-10-08 16:58:24 | gvanrossum | set | status: open -> closed
nosy:
+ gvanrossum messages:
+ msg93750
resolution: rejected |
| 2009-10-08 15:37:19 | pitrou | set | nosy:
+ pitrou messages:
+ msg93747
|
| 2009-10-08 13:49:57 | andbj | create | |
|