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.

Author r.david.murray
Recipients Peter Eastman, docs@python, r.david.murray
Date 2015-08-05.21:06:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1438808795.56.0.415703460865.issue24800@psf.upfronthosting.co.za>
In-reply-to
Content
OK, it looks like what the documentation of exec is missing is the fact that calling exec with no arguments in a non-global is equivalent to calling it with *two* arguments.  That is, your "exec(script)" statement is equivalent to "exec(script, globals(), locals())".  This is implicit but very much *not* explicit in the current documentation, and should be made explicit.

To be sure I'm explaining this fully: the documentation of exec says  "If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition".

>>> class Foo:
...   a = 10
...   [a for x in range(5)]
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in Foo
  File "<stdin>", line 3, in <listcomp>
NameError: name 'a' is not defined
History
Date User Action Args
2015-08-05 21:06:35r.david.murraysetrecipients: + r.david.murray, docs@python, Peter Eastman
2015-08-05 21:06:35r.david.murraysetmessageid: <1438808795.56.0.415703460865.issue24800@psf.upfronthosting.co.za>
2015-08-05 21:06:35r.david.murraylinkissue24800 messages
2015-08-05 21:06:34r.david.murraycreate