Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81859

Re: PyEval_GetLocals and unreferenced variables

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Gregory Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Re: PyEval_GetLocals and unreferenced variables
Date Thu, 04 Dec 2014 01:28:19 +1300
Lines 48
Message-ID <ce8dv5Fm877U1@mid.individual.net> (permalink)
References <mailman.70.1417604405.15533.python-list@python.org> <mailman.16527.1417605559.18130.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net hvWaZuU4y1K9ZfHgzkmMjg+9HMp0GjBvp5Yu/sNdAGIMdRIwl1
Cancel-Lock sha1:U/Q/zK4ZeJXg87U2230vkt256sA=
User-Agent Mozilla Thunderbird 1.0.5 (Macintosh/20050711)
X-Accept-Language en-us, en
In-Reply-To <mailman.16527.1417605559.18130.python-list@python.org>
Xref csiph.com comp.lang.python:81859

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: PyEval_GetLocals and unreferenced variables Kasper Peeters <kasper@phi-sci.com> - 2014-12-03 11:18 +0000
  Re: PyEval_GetLocals and unreferenced variables Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-12-04 01:28 +1300
    Re: PyEval_GetLocals and unreferenced variables Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-04 08:42 -0700

csiph-web