[New-bugs-announce] [issue43507] Variables in locals scope fails to be printed.

Xinmeng Xia report at bugs.python.org
Mon Mar 15 23:08:01 EDT 2021


New submission from Xinmeng Xia <xiaxm at smail.nju.edu.cn>:

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}

----------
components: Interpreter Core
messages: 388802
nosy: xxm
priority: normal
severity: normal
status: open
title: Variables in locals scope fails to be printed.
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43507>
_______________________________________


More information about the New-bugs-announce mailing list