Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'variables': 0.07; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; 'itself.': 0.14; "'q'": 0.16; 'code?': 0.16; 'itself,': 0.16; 'kasper': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'variable': 0.18; 'print': 0.22; 'fine': 0.24; 'cheers,': 0.24; 'question': 0.24; 'function': 0.29; 'code': 0.31; 'convince': 0.31; 'question:': 0.31; 'thanks!': 0.32; 'there': 0.35; 'charset:us- ascii': 0.36; 'list': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'simply': 0.61; 'within': 0.65; 'do:': 0.91; 'hand,': 0.93 Date: Wed, 26 Nov 2014 10:22:57 +0000 From: Kasper Peeters To: python-list@python.org Subject: PyEval_GetLocals and unreferenced variables X-Mailer: Claws Mail 3.10.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416997980 news.xs4all.nl 2908 [2001:888:2000:d::a6]:56389 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 3318 X-Received-Body-CRC: 732269209 Xref: csiph.com comp.lang.python:81556 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