Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81852 > unrolled thread
| Started by | Kasper Peeters <kasper@phi-sci.com> |
|---|---|
| First post | 2014-12-03 11:18 +0000 |
| Last post | 2014-12-04 08:42 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Kasper Peeters <kasper@phi-sci.com> |
|---|---|
| Date | 2014-12-03 11:18 +0000 |
| Subject | Re: PyEval_GetLocals and unreferenced variables |
| Message-ID | <mailman.16527.1417605559.18130.python-list@python.org> |
> I'm not sure how you think you're adding a local from C > code. If you're using PyEval_GetLocals(), that only gives > you a dict containing a *copy* of the locals; modifying > that dict doesn't change the locals in the function's frame. 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; see also http://stackoverflow.com/questions/22276502/create-python-object-in-local-scope-from-within-c > If the bytecode of the nested function doesn't reference a given > variable in the outer function, it doesn't get passed in. Ok, that's good to know because it rules out doing this without having an explicit reference to the variable in the inner scope. Thanks. Cheers, Kasper
[toc] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2014-12-04 01:28 +1300 |
| Message-ID | <ce8dv5Fm877U1@mid.individual.net> |
| In reply to | #81852 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-12-04 08:42 -0700 |
| Message-ID | <mailman.16561.1417707778.18130.python-list@python.org> |
| In reply to | #81859 |
[Multipart message — attachments visible in raw view] — view raw
On Wed, Dec 3, 2014 at 5:28 AM, Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote: > > 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. That the dict is cached is an implementation detail. It's not advisable to rely upon this, and as stated in the Python docs, the locals dict should not be modified.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web