← index #937PR #3207
Off-topic · high · value 0.266
QUERY · ISSUE

Obscure bug with locals not captured by function

openby dpgeorgeopened 2014-10-26updated 2024-08-28
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.

CANDIDATE · PULL REQUEST

Check eval()/exec() args

closedby tomlogicopened 2017-07-11updated 2017-07-21

During some fuzz testing, a co-worker found that eval() and exec() weren't type checking the second and third arguments (globals and locals). This typically results in a crash when you pass anything other than a dict.

In addition, you can pass "None" to specify globals or locals from the callers environment.

I've added a test and updated MicroPython so its behavior matches Python3. Because of the added functionality, I expect to see an increase in code size.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied