Closed robert-billing closed 1 year ago
Hi, that's not a bug, it's an optimization in the core, see the Micropython docs on that: https://docs.micropython.org/en/latest/genrst/core_language.html#local-variables-aren-t-included-in-locals-result
Local variables aren’t included in locals() result
Cause: MicroPython doesn’t maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can’t be accessed by a name.
Code running in eval() function doesn’t have access to local variables¶
Cause: MicroPython doesn’t maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can’t be accessed by a name. Effectively, eval(expr) in MicroPython is equivalent to eval(expr, globals(), globals()).
Closing since this isn't an issue with the library and is a known, specified deviation between CircuitPython/MicroPython and CPython.
I have tried this on versions
adafruit-circuitpython-raspberry_pi_pico-en_GB-7.3.3.uf2 adafruit-circuitpython-raspberry_pi_pico-en_GB-8.0.0-beta.0.uf2 adafruit-circuitpython-raspberry_pi_pico-en_GB-8.0.0-beta.5.uf2
with the same results. This test program
gives results which would imply that locals() is returning the global symbol table:
Running this on
Python 3.10.8 (main, Nov 14 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
gives me the expected result
Since eval() is supposed to use the local symbol table, and the circuitpython eval() does not have a locals= option this is giving me anomalous results from eval().
I suspect that this is a fairly simple bug.