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


Groups > comp.lang.python > #81556 > unrolled thread

PyEval_GetLocals and unreferenced variables

Started byKasper Peeters <kasper@phi-sci.com>
First post2014-11-26 10:22 +0000
Last post2014-11-26 10:22 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  PyEval_GetLocals and unreferenced variables Kasper Peeters <kasper@phi-sci.com> - 2014-11-26 10:22 +0000

#81556 — PyEval_GetLocals and unreferenced variables

FromKasper Peeters <kasper@phi-sci.com>
Date2014-11-26 10:22 +0000
SubjectPyEval_GetLocals and unreferenced variables
Message-ID<mailman.16315.1416997980.18130.python-list@python.org>
I have a question about PyEval_GetLocals(). The normal behaviour of
PyEval_GetLocals(), and in fact of the locals() function in Python
itself, is to return a list which includes only those variables which
are actually referenced in the local scope. Example:

  def fun():
     q=3
     def fun2():
        print(locals())
     fun2()

  fun()

will print "{}" because "q" has not been referenced in fun2(). On the
other hand, if you do

  def fun():
     q=3
     def fun2():
	print(q)
        print(locals())
     fun2()

  fun()

you will instead get "{'q': 3}" as output. All fine and understood.

My question: I want to call a C function inside fun2(), which I want to
give access to the 'q' variable in the fun() scope, _without_ there
being any reference to 'q' in the python code itself. That is, I want
to do:

  def fun():
     q=3
     def fun2():
	cfun()
     fun2()

  fun()

and access 'q' inside the C-function cfun(). If I simply let it call
PyEval_GetLocals, then the result will again not contain "q". Is there
any way in which I can convince python to pull 'q' into the local scope
from within my C code?

Thanks!

Cheers,
Kasper

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web