Issue 1167300: Error "exec"ing python code (original) (raw)

Issue1167300

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 "", line 1, in ? File "", line 2, in ? File "", 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 "", line 1, in ? File "", line 4, in ? File "", 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:10 admin set github: 41731
2012-06-08 15:04:49 terry.reedy set superseder: An inconsistency with nested scopes
2005-03-21 05:35:43 stefan create