From a46aa48e23ef8af76ee38431a5ef7fea9988bc86 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Tue, 5 Oct 2021 16:38:24 +0530 Subject: [PATCH 1/3] Convert more modules to use MP_REGISTER_MODULE Convert espidf, os, rp2pio, samd and time to use MP_REGISTER_MODULE. Related to #5183. --- ports/atmel-samd/bindings/samd/__init__.c | 2 + ports/espressif/bindings/espidf/__init__.c | 2 + ports/raspberrypi/bindings/rp2pio/__init__.c | 2 + py/circuitpy_mpconfig.h | 56 ++------------------ shared-bindings/os/__init__.c | 2 + shared-bindings/time/__init__.c | 2 + 6 files changed, 14 insertions(+), 52 deletions(-) diff --git a/ports/atmel-samd/bindings/samd/__init__.c b/ports/atmel-samd/bindings/samd/__init__.c index 207a6d52e6..b5c618108d 100644 --- a/ports/atmel-samd/bindings/samd/__init__.c +++ b/ports/atmel-samd/bindings/samd/__init__.c @@ -57,3 +57,5 @@ const mp_obj_module_t samd_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&samd_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_samd, samd_module, CIRCUITPY_SAMD); diff --git a/ports/espressif/bindings/espidf/__init__.c b/ports/espressif/bindings/espidf/__init__.c index c7d5768cd3..9076294d53 100644 --- a/ports/espressif/bindings/espidf/__init__.c +++ b/ports/espressif/bindings/espidf/__init__.c @@ -208,3 +208,5 @@ void raise_esp_error(esp_err_t err) { } mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err); } + +MP_REGISTER_MODULE(MP_QSTR_espidf, espidf_module, CIRCUITPY_ESPIDF); diff --git a/ports/raspberrypi/bindings/rp2pio/__init__.c b/ports/raspberrypi/bindings/rp2pio/__init__.c index 68f7cf3019..b3b45dc923 100644 --- a/ports/raspberrypi/bindings/rp2pio/__init__.c +++ b/ports/raspberrypi/bindings/rp2pio/__init__.c @@ -58,3 +58,5 @@ const mp_obj_module_t rp2pio_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&rp2pio_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_rp2pio, rp2pio_module, CIRCUITPY_RP2PIO); diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 2ec0d2a849..f4bbbc6ea2 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -255,15 +255,12 @@ typedef long mp_off_t; #define BOARD_I2C (defined(DEFAULT_I2C_BUS_SDA) && defined(DEFAULT_I2C_BUS_SCL)) #define BOARD_SPI (defined(DEFAULT_SPI_BUS_SCK) && defined(DEFAULT_SPI_BUS_MISO) && defined(DEFAULT_SPI_BUS_MOSI)) #define BOARD_UART (defined(DEFAULT_UART_BUS_RX) && defined(DEFAULT_UART_BUS_TX)) - // I2C and SPI are always allocated off the heap. - #if BOARD_UART #define BOARD_UART_ROOT_POINTER mp_obj_t shared_uart_bus; #else #define BOARD_UART_ROOT_POINTER #endif - #else #define BOARD_UART_ROOT_POINTER #endif @@ -283,14 +280,6 @@ typedef long mp_off_t; #define ERRNO_MODULE { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) }, #else #define ERRNO_MODULE -# - #endif - -#if CIRCUITPY_ESPIDF -extern const struct _mp_obj_module_t espidf_module; -#define ESPIDF_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_espidf),(mp_obj_t)&espidf_module }, -#else -#define ESPIDF_MODULE #endif #if CIRCUITPY_GAMEPADSHIFT @@ -331,15 +320,6 @@ extern const struct _mp_obj_module_t espidf_module; extern const struct _mp_obj_module_t nvm_module; #endif -#if CIRCUITPY_OS -extern const struct _mp_obj_module_t os_module; -#define OS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, -#define OS_MODULE_ALT_NAME { MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&os_module }, -#else -#define OS_MODULE -#define OS_MODULE_ALT_NAME -#endif - #if CIRCUITPY_RE #define MICROPY_PY_URE (1) #define RE_MODULE { MP_ROM_QSTR(MP_QSTR_re), MP_ROM_PTR(&mp_module_ure) }, @@ -347,29 +327,6 @@ extern const struct _mp_obj_module_t os_module; #define RE_MODULE #endif -#if CIRCUITPY_RP2PIO -extern const struct _mp_obj_module_t rp2pio_module; -#define RP2PIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rp2pio),(mp_obj_t)&rp2pio_module }, -#else -#define RP2PIO_MODULE -#endif - -#if CIRCUITPY_SAMD -extern const struct _mp_obj_module_t samd_module; -#define SAMD_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, -#else -#define SAMD_MODULE -#endif - -#if CIRCUITPY_TIME -extern const struct _mp_obj_module_t time_module; -#define TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, -#define TIME_MODULE_ALT_NAME { MP_OBJ_NEW_QSTR(MP_QSTR__time), (mp_obj_t)&time_module }, -#else -#define TIME_MODULE -#define TIME_MODULE_ALT_NAME -#endif - #if defined(CIRCUITPY_ULAB) && CIRCUITPY_ULAB // ulab requires reverse special methods #if defined(MICROPY_PY_REVERSE_SPECIAL_METHODS) && !MICROPY_PY_REVERSE_SPECIAL_METHODS @@ -383,17 +340,15 @@ extern const struct _mp_obj_module_t time_module; // Define certain native modules with weak links so they can be replaced with Python // implementations. This list may grow over time. -#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ - OS_MODULE \ - TIME_MODULE \ + +#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS // Native modules that are weak links can be accessed directly // by prepending their name with an underscore. This list should correspond to // MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS, assuming you want the native modules // to be accessible when overriden. -#define MICROPY_PORT_BUILTIN_MODULE_ALT_NAMES \ - OS_MODULE_ALT_NAME \ - TIME_MODULE_ALT_NAME \ + +#define MICROPY_PORT_BUILTIN_MODULE_ALT_NAMES // This is an inclusive list that should correspond to the CIRCUITPY_XXX list above, // including dependencies. @@ -403,11 +358,8 @@ extern const struct _mp_obj_module_t time_module; #define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \ BINASCII_MODULE \ ERRNO_MODULE \ - ESPIDF_MODULE \ JSON_MODULE \ RE_MODULE \ - RP2PIO_MODULE \ - SAMD_MODULE \ // The following modules are defined in their respective __init__.c file in the // shared-bindings directory using MP_REGISTER_MODULE. diff --git a/shared-bindings/os/__init__.c b/shared-bindings/os/__init__.c index 798f3c361f..fc246582fd 100644 --- a/shared-bindings/os/__init__.c +++ b/shared-bindings/os/__init__.c @@ -247,3 +247,5 @@ const mp_obj_module_t os_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&os_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_os, os_module, CIRCUITPY_OS); diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index cc8689ca33..1529f1de50 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -330,3 +330,5 @@ const mp_obj_module_t time_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&time_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_time, time_module, CIRCUITPY_TIME); From 0eafb672b8e5ae984ff5f27ddaa608688e98c9d9 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:45:44 +0530 Subject: [PATCH 2/3] Convert all modules to use MP_REGISTER_MODULE Convert binascii, errno, json, and re to use MP_REGISTER_MODULE. Resolves #5183. --- extmod/modubinascii.c | 2 ++ extmod/modujson.c | 2 ++ extmod/modure.c | 2 ++ py/circuitpy_mpconfig.h | 61 +++++++++++++---------------------------- py/moduerrno.c | 2 ++ 5 files changed, 27 insertions(+), 42 deletions(-) diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index 01a3c62f6c..c21ae8bc80 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -235,3 +235,5 @@ const mp_obj_module_t mp_module_ubinascii = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&mp_module_binascii_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_binascii, mp_module_ubinascii, MICROPY_PY_UBINASCII); diff --git a/extmod/modujson.c b/extmod/modujson.c index a9d231785a..57d72b3bea 100644 --- a/extmod/modujson.c +++ b/extmod/modujson.c @@ -375,4 +375,6 @@ const mp_obj_module_t mp_module_ujson = { .globals = (mp_obj_dict_t *)&mp_module_ujson_globals, }; +MP_REGISTER_MODULE(MP_QSTR_json, mp_module_ujson, MICROPY_PY_UJSON); + #endif // MICROPY_PY_UJSON diff --git a/extmod/modure.c b/extmod/modure.c index 7fd7b09395..18a28d3628 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -469,6 +469,8 @@ const mp_obj_module_t mp_module_ure = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&mp_module_re_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_re, mp_module_ure, MICROPY_PY_URE); #endif // Source files #include'd here to make sure they're compiled in diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index f4bbbc6ea2..3608b9c71d 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -244,13 +244,6 @@ typedef long mp_off_t; // These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off. // So if any are not defined in *.mk, they'll throw an error here. -#if CIRCUITPY_BINASCII -#define MICROPY_PY_UBINASCII CIRCUITPY_BINASCII -#define BINASCII_MODULE { MP_ROM_QSTR(MP_QSTR_binascii), MP_ROM_PTR(&mp_module_ubinascii) }, -#else -#define BINASCII_MODULE -#endif - #if CIRCUITPY_BOARD #define BOARD_I2C (defined(DEFAULT_I2C_BUS_SDA) && defined(DEFAULT_I2C_BUS_SCL)) #define BOARD_SPI (defined(DEFAULT_SPI_BUS_SCK) && defined(DEFAULT_SPI_BUS_MISO) && defined(DEFAULT_SPI_BUS_MOSI)) @@ -273,15 +266,6 @@ typedef long mp_off_t; #define CIRCUITPY_DISPLAY_LIMIT (0) #endif -#if CIRCUITPY_ERRNO -#define MICROPY_PY_UERRNO (1) -// Uses about 80 bytes. -#define MICROPY_PY_UERRNO_ERRORCODE (1) -#define ERRNO_MODULE { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) }, -#else -#define ERRNO_MODULE -#endif - #if CIRCUITPY_GAMEPADSHIFT // Scan gamepad every 32ms #define CIRCUITPY_GAMEPAD_TICKS 0x1f @@ -290,18 +274,6 @@ typedef long mp_off_t; #define GAMEPAD_ROOT_POINTERS #endif -#if CIRCUITPY_JSON -#define MICROPY_PY_UJSON (1) -#define MICROPY_PY_IO (1) -#define JSON_MODULE { MP_ROM_QSTR(MP_QSTR_json), MP_ROM_PTR(&mp_module_ujson) }, -#else -#ifndef MICROPY_PY_IO -// We don't need MICROPY_PY_IO unless someone else wants it. -#define MICROPY_PY_IO (0) -#endif -#define JSON_MODULE -#endif - #if CIRCUITPY_KEYPAD #define KEYPAD_ROOT_POINTERS mp_obj_t keypad_scanners_linked_list; #else @@ -320,22 +292,31 @@ typedef long mp_off_t; extern const struct _mp_obj_module_t nvm_module; #endif -#if CIRCUITPY_RE -#define MICROPY_PY_URE (1) -#define RE_MODULE { MP_ROM_QSTR(MP_QSTR_re), MP_ROM_PTR(&mp_module_ure) }, +// Following modules are implemented in either extmod or py directory. + +#define MICROPY_PY_UBINASCII CIRCUITPY_BINASCII + +#define MICROPY_PY_UERRNO CIRCUITPY_ERRNO +// Uses about 80 bytes. +#define MICROPY_PY_UERRNO_ERRORCODE CIRCUITPY_ERRNO + +#define MICROPY_PY_URE CIRCUITPY_RE + +#if CIRCUITPY_JSON +#define MICROPY_PY_UJSON (1) +#define MICROPY_PY_IO (1) #else -#define RE_MODULE +#ifndef MICROPY_PY_IO +// We don't need MICROPY_PY_IO unless someone else wants it. +#define MICROPY_PY_IO (0) +#endif #endif -#if defined(CIRCUITPY_ULAB) && CIRCUITPY_ULAB +#if CIRCUITPY_ULAB // ulab requires reverse special methods #if defined(MICROPY_PY_REVERSE_SPECIAL_METHODS) && !MICROPY_PY_REVERSE_SPECIAL_METHODS #error "ulab requires MICROPY_PY_REVERSE_SPECIAL_METHODS" #endif -#define ULAB_MODULE \ - { MP_ROM_QSTR(MP_QSTR_ulab), MP_ROM_PTR(&ulab_user_cmodule) }, -#else -#define ULAB_MODULE #endif // Define certain native modules with weak links so they can be replaced with Python @@ -355,11 +336,7 @@ extern const struct _mp_obj_module_t nvm_module; // Some of these definitions will be blank depending on what is turned on and off. // Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above. -#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \ - BINASCII_MODULE \ - ERRNO_MODULE \ - JSON_MODULE \ - RE_MODULE \ +#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS // The following modules are defined in their respective __init__.c file in the // shared-bindings directory using MP_REGISTER_MODULE. diff --git a/py/moduerrno.c b/py/moduerrno.c index d13028e73a..56d903f115 100644 --- a/py/moduerrno.c +++ b/py/moduerrno.c @@ -105,6 +105,8 @@ const mp_obj_module_t mp_module_uerrno = { .globals = (mp_obj_dict_t *)&mp_module_uerrno_globals, }; +MP_REGISTER_MODULE(MP_QSTR_errno, mp_module_uerrno, MICROPY_PY_UERRNO); + qstr mp_errno_to_str(mp_obj_t errno_val) { // Otherwise, return the Exxxx string for that error code #if MICROPY_PY_UERRNO_ERRORCODE From a279f7b42cf8a03b744cccf4fdab4ff9ccd6251d Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Tue, 5 Oct 2021 23:44:32 +0530 Subject: [PATCH 3/3] update extra_coverage.py.exp --- tests/unix/extra_coverage.py.exp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 2cf48de142..893a6b72c3 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -30,9 +30,10 @@ ame__ mport builtins micropython _thread array -btree cexample cmath collections -cppexample ffi framebuf gc -hashlib math qrio sys +binascii btree cexample cmath +collections cppexample errno ffi +framebuf gc hashlib json +math qrio re sys termios ubinascii uctypes uerrno uheapq uio ujson ulab uos urandom ure uselect