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.
zephyr-rebase
Damien George 2016-09-27 13:34:21 +10:00
parent f65e4f0b8f
commit 791b65f4b2
4 changed files with 7 additions and 0 deletions

View File

@ -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) },
#if MICROPY_PY_MICROPYTHON_MEM_INFO
#if MICROPY_MEM_STATS
{ MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) },

View File

@ -1,5 +1,7 @@
# test constant optimisation
from micropython import const
X = const(123)
Y = const(X + 456)

View File

@ -1,5 +1,7 @@
# check that consts are not replaced in anything except standalone identifiers
from micropython import const
X = const(1)
Y = const(2)
Z = const(3)

View File

@ -1,5 +1,7 @@
# make sure syntax error works correctly for bad const definition
from micropython import const
def test_syntax(code):
try:
exec(code)