Message388802
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} |
|
Date |
User |
Action |
Args |
2021-03-16 03:08:01 | xxm | set | recipients:
+ xxm |
2021-03-16 03:08:01 | xxm | set | messageid: <1615864081.29.0.727460269138.issue43507@roundup.psfhosted.org> |
2021-03-16 03:08:01 | xxm | link | issue43507 messages |
2021-03-16 03:08:00 | xxm | create | |
|