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


Groups > comp.lang.python > #29550

Re: [Q] How to exec code object with local variables specified?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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:: [': 0.03; 'subject:code': 0.07; 'subject:How': 0.09; 'python:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; '2",': 0.16; '2.7.3': 0.16; 'bytecode': 0.16; 'globals(),': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'variables': 0.17; '>>>': 0.18; 'subject:] ': 0.19; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'run': 0.28; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; '(1)': 0.34; 'expected': 0.35; 'subject:?': 0.35; "won't": 0.35; 'received:org': 0.36; 'but': 0.36; 'subject:with': 0.36; 'possible': 0.37; 'uses': 0.37; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'different': 0.63
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: [Q] How to exec code object with local variables specified?
Date Thu, 20 Sep 2012 15:15:24 +0200
Organization None
References <CAFTm5Ru=E8Zii4k54fc4ii43wKPSwTq=iT4iRQCWy=46YiEZZw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849578.dip.t-dialin.net
User-Agent KNode/4.7.3
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.957.1348146880.27098.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348146880 news.xs4all.nl 6915 [2001:888:2000:d::a6]:36522
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29550

Show key headers only | View raw


Makoto Kuwata wrote:

> Is it possible to run code object with local variables specified?
> I'm trying the following code but not work:
> 
>     def fn():
>        x = 1
>        y = 2
>     localvars = {'x': 0}
>     exec(fn.func_code, globals(), localvars)
>     print(localvars)
>     ## what I expected is: {'x': 1, 'y': 2}
>     ## but actual is:      {'x': 0}
> 
> Python: 2.7.3

>>> loc = {}
>>> exec("x = 1; y = 2", globals(), loc)
>>> loc
{'y': 2, 'x': 1}

However, this won't work with the code object taken from a function which 
uses a different a bytecode (STORE_FAST instead of STORE_NAME):


>>> import dis
>>> def f(): x = 1
... 
>>> dis.dis(f)
  1           0 LOAD_CONST               1 (1)
              3 STORE_FAST               0 (x)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE        
>>> dis.dis(compile("x=1", "<nofile>", "exec"))
  1           0 LOAD_CONST               0 (1)
              3 STORE_NAME               0 (x)
              6 LOAD_CONST               1 (None)
              9 RETURN_VALUE        

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


Thread

Re: [Q] How to exec code object with local variables specified? Peter Otten <__peter__@web.de> - 2012-09-20 15:15 +0200

csiph-web