Skip to content

Commit dcac880

Browse files
committed
Add empty "micropython" module to allow more seamless CPython portability.
Implicit "micropython" module contains (at least) codegeneration decorators. Make it explicit, so an app could have "import micropython". On MicroPython, that will be no-op. On CPython, that will give a chance to have a module with placeholder decorators.
1 parent c8742a0 commit dcac880

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

py/mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ typedef long long mp_longint_impl_t;
8585
#define MICROPY_ENABLE_SLICE (1)
8686
#endif
8787

88+
// Enable features which improve CPython compatibility
89+
// but may lead to more code size/memory usage.
90+
// TODO: Originally intended as generic category to not
91+
// add bunch of once-off options. May need refactoring later
92+
#ifndef MICROPY_CPYTHON_COMPAT
93+
#define MICROPY_CPYTHON_COMPAT (1)
94+
#endif
95+
8896
/*****************************************************************************/
8997
/* Miscellaneous settings */
9098

py/runtime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ void rt_init(void) {
143143
mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
144144
mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
145145

146+
#if MICROPY_CPYTHON_COMPAT
147+
// Add (empty) micropython module, so it was possible to "import micropython",
148+
// which can be a placeholder module on CPython.
149+
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("micropython"));
150+
rt_store_name(qstr_from_str_static("micropython"), m);
151+
#endif
152+
146153
next_unique_code_id = 1; // 0 indicates "no code"
147154
unique_codes_alloc = 0;
148155
unique_codes = NULL;

0 commit comments

Comments
 (0)