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 Huyston
Recipients Huyston
Date 2019-09-30.20:39:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1569875978.41.0.850227388561.issue38331@roundup.psfhosted.org>
In-reply-to
Content
This seems like a bug for me, forgive me if its not.

Consider the following script:

def func():
    print(var)

my_globals = {'func':func,'var':14}
exec("print(var);func()",my_globals,my_globals)

This is the output:

14
Traceback (most recent call last):
  File "bug.py", line 5, in <module>
    exec("print(var);func()",my_globals,my_globals)
  File "<string>", line 1, in <module>
  File "bug.py", line 2, in func
    print(var)
NameError: name 'var' is not defined

The first line gives the expected result (14). However the second line throws the NameError.

If 'func' is defined inside the exec argument string, then it prints 14 and 14, which is the expected result (for me at least).

Ex:

def func():
    print(var)

my_globals = {'func':func,'var':14}
exec("def func():\n    print(var)\nprint(var);func()",my_globals,my_globals)

Result:
14
14

So is this really a bug or is this the expected behavior somehow?
History
Date User Action Args
2019-09-30 20:39:38Huystonsetrecipients: + Huyston
2019-09-30 20:39:38Huystonsetmessageid: <1569875978.41.0.850227388561.issue38331@roundup.psfhosted.org>
2019-09-30 20:39:38Huystonlinkissue38331 messages
2019-09-30 20:39:38Huystoncreate