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

SourceForge.net noreply at sourceforge.net
Wed Mar 30 14:40:31 CEST 2005


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

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
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: Michael Chermside (mcherm)
Date: 2005-03-30 07: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