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


Groups > comp.lang.python > #28430

Re: C Python: Running Python code within function scope

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'exec': 0.07; 'function,': 0.07; 'nested': 0.07; 'raises': 0.07; 'subject:code': 0.07; 'variable,': 0.07; 'wrapped': 0.07; 'api': 0.09; 'python': 0.09; 'globals': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'xxx:': 0.09; 'def': 0.10; 'do,': 0.15; 'func():': 0.16; 'namespace.': 0.16; 'pyobject': 0.16; 'pyobject*': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'simulate': 0.16; 'varnames': 0.16; 'wrote:': 0.17; 'copied': 0.17; 'variables': 0.17; 'jan': 0.18; 'code,': 0.18; 'module': 0.19; 'code.': 0.20; 'skip:p 30': 0.20; 'variable': 0.20; 'lets': 0.22; 'names.': 0.22; 'seems': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'looks': 0.26; 'compiled': 0.27; 'separate': 0.27; 'executing': 0.27; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'run': 0.28; 'dictionary': 0.29; 'surprised': 0.29; 'case,': 0.29; 'class': 0.29; 'this.': 0.29; 'that.': 0.30; 'function': 0.30; 'code': 0.31; 'function.': 0.33; 'problem': 0.33; 'to:addr:python- list': 0.33; 'presence': 0.33; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'skip:p 20': 0.36; 'enough': 0.36; 'unable': 0.36; 'execute': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'easy': 0.60; 'provide': 0.62; 'email addr:gmail.com': 0.63; 'within': 0.64; 'state.': 0.71; 'introduce': 0.80; 'complexity': 0.84; 'dict.': 0.84; 'locals': 0.84; 'received:fios.verizon.net': 0.84; 'hand,': 0.97
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: C Python: Running Python code within function scope
Date Tue, 04 Sep 2012 18:18:38 -0400
References <522c6780-f093-4456-b87b-fb8eb77e77f6@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <522c6780-f093-4456-b87b-fb8eb77e77f6@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.196.1346797144.27098.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346797144 news.xs4all.nl 6922 [2001:888:2000:d::a6]:53524
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28430

Show key headers only | View raw


On 9/4/2012 4:28 PM, channel727272@gmail.com wrote:
> The Python C API function PyEval_EvalCode let's you execute compiled
> Python code. I want to execute a block of Python code as if it were
> executing within the scope of a function, so that it has its own
> dictionary of local variables which don't affect the global state.

You cannot really do this. The local namespace of functions is not a dict.

> This seems easy enough to do, since PyEval_EvalCode lets you provide
> a Global and Local dictionary:
>
> PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals,
> PyObject *locals)

When you do this, you are executing code as if in a class statement, not 
as if in a function.

> The problem I run into has to do with how Python looks up variable
> names. Consider the following code, that I execute with
> PyEval_EvalCode:
>
> myvar = 300
 > def func(): return myvar
> func()

if you wrapped this in def outer(), myvar would be a nonlocal variable, 
neither local nor global. exec does not allow for that.

> This simple code actually raises an error, because Python is unable
> to find the variable myvar from within func.

This is the same as if you indented the above under class xxx:
...
> It's only with nested functions, that Python seems to copy
> outer-scope locals into inner-scope locals.

I was surprised to find that nonlocals do appear in locals(), but I 
would not exactly say that they are copied into the actual local namespace.

def outer():
     x = 1
     def inner(z=3):
         y = 2
         print(x)
         print(locals())
     inner()
     print(inner.__code__.co_varnames, inner.__code__.co_freevars)

outer()
#
1
{'x': 1, 'y': 2, 'z': 3}
('z', 'y') ('x',)

Varnames are the local names, freevars are the nonlocal names.

In any case, this complexity requires the presence of an outer function 
when the code is compiled. There is no way to simulate it after the fact 
by fiddling with just locals and globals when the compiled code is 
exec'ed. Class statement, on the other hand, simply introduce a local 
namespace separate from the module namespace.

-- 
Terry Jan Reedy

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


Thread

C Python: Running Python code within function scope channel727272@gmail.com - 2012-09-04 13:28 -0700
  Re: C Python: Running Python code within function scope Terry Reedy <tjreedy@udel.edu> - 2012-09-04 18:18 -0400

csiph-web