New submission from Xinmeng Xia <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue43507>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com