PyEval_GetLocals and unreferenced variables
Gregory Ewing
greg.ewing at canterbury.ac.nz
Wed Dec 3 07:28:19 EST 2014
Kasper Peeters wrote:
> That may have been the design plan, but in Python 2.7.6, I definitely
> am able to inject locals via PyEval_GetLocals() and have them be visible
> both from the C and Python side;
What seems to be happening is that the dict created by
PyEval_GetLocals() is kept around, so you can change it
and have the changes be visible through locals() in
Python.
However, that doesn't create a local *name* that's
visible from Python. Or if the local name exists,
changes made through locals() aren't reflected in
the value of the local name.
Existing local name is not changed:
>>> def f():
... a = 42
... locals()['a'] = 17
... print a
...
>>> f()
42
Can't create a local name:
>>> def g():
... locals()['a'] = 17
... print a
...
>>> g()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in g
NameError: global name 'a' is not defined
Changes to locals() persist:
>>> def h():
... locals()['a'] = 17
... print locals()['a']
...
>>> h()
17
--
Greg
More information about the Python-list
mailing list