[ python-Bugs-1167300 ] Error "exec"ing python code

SourceForge.net noreply at sourceforge.net
Mon Feb 20 23:06:42 CET 2006


Bugs item #1167300, was opened at 2005-03-21 05:35
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1167300&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Duplicate
Priority: 5
Submitted By: Stefan Seefeld (stefan)
Assigned to: Nobody/Anonymous (nobody)
Summary: Error "exec"ing python code

Initial Comment:
I'm trying to 'exec'ing the following code:

class Foo: pass
class Bar:
    f = Foo
    
The error appears when using 'exec f in {}, {}':

>>> f = ''.join(open('/home/stefan/t.py').readlines())
>>> exec f in {}, {}
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 2, in ?
  File "<string>", line 3, in Bar
NameError: name 'Foo' is not defined

I tested on python 2.3 and python 2.4, both show the same
behavior.

----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2006-02-20 22:06

Message:
Logged In: YES 
user_id=849994

This seems like a duplicate of #991196.

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2005-03-30 21:11

Message:
Logged In: YES 
user_id=593130

I got a similar NameError in 2.2.1, so this is not due to a recent 
change.

----------------------------------------------------------------------

Comment By: Michael Chermside (mcherm)
Date: 2005-03-30 12:40

Message:
Logged In: YES 
user_id=99874

I can confirm this... it appears that things which are set
in the global scope within an "exec ... in {}, {}" are not
then correctly accessed in the global scope when being read.
The following two examples illustrate the problem:

>>> exec """\
... x = 3
... def f():
...     global x
...     print x
... f()
... """ in {}, {}
3

... and again without the global definition:

>>> exec """\
... x = 3
... def f():
...     print x
... f()
... """ in {}, {}
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 4, in ?
  File "<string>", line 3, in f
NameError: global name 'x' is not defined

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1167300&group_id=5470


More information about the Python-bugs-list mailing list