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 xxm
Recipients xxm
Date 2021-03-16.03:08:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1615864081.29.0.727460269138.issue43507@roundup.psfhosted.org>
In-reply-to
Content
The following code 1 calls function 'compile' and 'exec' and execute a statement "s=1". Then we print the value of 's'.  This code can perform well on Python 3.9.2 and output the expected result. However, we pack the whole code into a function (code 2).  The execution fails. 


code 1:
===================
mstr = "s=1"
exec(compile(mstr,'','exec'))
print(s)
===================
output: 1


code2:
===================
def foo():
     mstr = "s=1"
     exec(compile(mstr,'','exec'))
     print(s)
foo()
===================
output: 
Traceback (most recent call last):
    File "/home/xxm/Desktop/apifuzz/doc/genDoc.py", line 37, in <module>
            foo()
    File "/home/xxm/Desktop/apifuzz/doc/genDoc.py", line 35, in foo
            print(s)
NameError: name 's' is not defined

By the way, we print locals().  's' exists in the local scope. It should not fail.
>>print(locals())
{'mstr': 's=1', 's': 1}
History
Date User Action Args
2021-03-16 03:08:01xxmsetrecipients: + xxm
2021-03-16 03:08:01xxmsetmessageid: <1615864081.29.0.727460269138.issue43507@roundup.psfhosted.org>
2021-03-16 03:08:01xxmlinkissue43507 messages
2021-03-16 03:08:00xxmcreate