Commit 791b65f4 authored by Damien George's avatar Damien George
Browse files

py/modmicropython: Add micropython.const, alias for identity function.

Having a micropython.const identity function, and writing "from micropython
import const" at the start of scripts that use the const feature, allows to
write scripts which are compatible with CPython, and with uPy builds that
don't include const optimisation.

This patch adds such a function and updates the tests to do the import.
parent f65e4f0b
......@@ -120,6 +120,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_
STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) },
{ MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) },
{ MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) },
# test constant optimisation
from micropython import const
X = const(123)
Y = const(X + 456)
# check that consts are not replaced in anything except standalone identifiers
from micropython import const
X = const(1)
Y = const(2)
Z = const(3)
# make sure syntax error works correctly for bad const definition
from micropython import const
def test_syntax(code):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment