QUERY · ISSUE
py/compile: compile built-in with 'single' option is not CPython compatible
py-core
In CPython:
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> eval(compile("4+5", "<string>", "single"))
9
>>>
In MicroPython (1.12):
MicroPython v1.12-68-g3032ae115-dirty on 2020-01-15; ESP32 module with ESP32
Type "help()" for more information.
>>> 4+5
9
>>> eval(compile("4+5", "<string>", "single"))
>>>
CANDIDATE · ISSUE
Obscure bug with locals not captured by function
bug
This is a bug for the record. It exists because a compiled bytecode function captures only its globals, not its locals. The only way I can make the bug appear (ie the only time a function needs to know the locals it was compiled in context of) is using the builtin compile function, and execute pre compiled code within another module, as follows.
Put this in t1.py:
x = 1
import t2
c = compile('print(x)', 'me', 'exec')
t2.C().ex(c)
and this in t2.py:
x = 2
c = compile('print(x)', 'me2', 'exec')
class C:
x = 3
exec('print(x)')
exec(c)
def ex(self, c):
exec(c)
Then run micropython t1.py. Check against CPythons output.
The above code tests more than just this bug, in order that one can see exactly what contexts are used where.