homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Error "exec"ing python code
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder: An inconsistency with nested scopes
View: 991196
Assigned To: Nosy List: georg.brandl, mcherm, stefan, terry.reedy
Priority: normal Keywords:

Created on 2005年03月21日 05:35 by stefan, last changed 2022年04月11日 14:56 by admin. This issue is now closed.

Messages (4)
msg24729 - (view) Author: Stefan Seefeld (stefan) * (Python committer) Date: 2005年03月21日 05:35
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.
msg24730 - (view) Author: Michael Chermside (mcherm) (Python triager) Date: 2005年03月30日 12:40
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
msg24731 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2005年03月30日 21:11
Logged In: YES 
user_id=593130
I got a similar NameError in 2.2.1, so this is not due to a recent 
change.
msg24732 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006年02月20日 22:06
Logged In: YES 
user_id=849994
This seems like a duplicate of #991196.
History
Date User Action Args
2022年04月11日 14:56:10adminsetgithub: 41731
2012年06月08日 15:04:49terry.reedysetsuperseder: An inconsistency with nested scopes
2005年03月21日 05:35:43stefancreate

AltStyle によって変換されたページ (->オリジナル) /