Skip to content

Commit

Permalink
micropython: update to v1.20.0
Browse files Browse the repository at this point in the history
There were quite a few breaking changes:
- Type definitions converted to use MP_DEFINE_CONST_OBJ_TYPE.
- Fixes for moved $(PROG) location.
- Fixes for unix variants.
- Fixes for extmod changes.
- Change to upstreamed nlr_abort.
  • Loading branch information
dlech committed Apr 27, 2023
1 parent 73c24ca commit 4cd1e85
Show file tree
Hide file tree
Showing 91 changed files with 583 additions and 597 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross/mpy-cross
path: micropython/mpy-cross/build/mpy-cross

unix_coverage:
name: unix coverage
Expand All @@ -48,9 +48,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Build
run: |
cd micropython
Expand All @@ -62,7 +62,7 @@ jobs:
run: |
cd micropython
make -C ports/unix VARIANT=coverage test_full
(cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/micropython-coverage ./run-multitests.py multi_net/*.py)
(cd tests && MICROPY_CPYTHON3=python3 MICROPY_MICROPYTHON=../ports/unix/build-coverage/micropython ./run-multitests.py multi_net/*.py)
- name: List failed tests
if: failure()
run: micropython/tests/run-tests.py --print-failures
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: pybricks-micropython-build-${{ github.run_number }}
path: bricks/ev3dev/pybricks-micropython
path: bricks/ev3dev/build-armel/pybricks-micropython

ev3dev_ubuntu_lts:
name: ev3dev-ubuntu-lts
Expand Down Expand Up @@ -138,9 +138,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Build
run: make $MAKEOPTS -C bricks/ev3dev
- name: Test
Expand Down Expand Up @@ -172,9 +172,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Install python
uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -227,9 +227,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Build and test
run: COVERAGE=1 ./test-virtualhub.sh
- name: Coveralls
Expand All @@ -256,9 +256,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Build
run: make $MAKEOPTS -C bricks/ev3rt

Expand All @@ -278,9 +278,9 @@ jobs:
uses: actions/download-artifact@v3
with:
name: mpy-cross
path: micropython/mpy-cross
path: micropython/mpy-cross/build
- name: Fix file permission
run: chmod +x micropython/mpy-cross/mpy-cross
run: chmod +x micropython/mpy-cross/build/mpy-cross
- name: Build
run: make $MAKEOPTS -C bricks/debug build/firmware.dfu

Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder}/micropython/tests",
"program": "${workspaceFolder}/bricks/ev3dev/pybricks-micropython",
"program": "${workspaceFolder}/bricks/ev3dev/build-test/pybricks-micropython",
"args": [
"../../tests/ev3dev/brick/battery.py"
],
Expand Down Expand Up @@ -187,7 +187,7 @@
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/bricks/ev3dev/pybricks-micropython",
"program": "${workspaceFolder}/bricks/ev3dev/built-test/pybricks-micropython",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
{
"label": "build ev3dev-test",
"type": "shell",
"command": "poetry run make -C bricks/ev3dev pybricks-micropython build-test/libgrx-3.0-vdriver-test.so DEBUG=1 COPT=-O0 BUILD=build-test CROSS_COMPILE= -j"
"command": "poetry run make -C bricks/ev3dev build-test/pybricks-micropython build-test/libgrx-3.0-vdriver-test.so DEBUG=1 COPT=-O0 BUILD=build-test CROSS_COMPILE= -j"
},
{
"label": "build ev3dev deploy",
"type": "shell",
"command": "make ev3dev-armel -j && cp bricks/ev3dev/pybricks-micropython /run/user/1000/gvfs/sftp:host=*/home/robot"
"command": "make ev3dev-armel -j && cp bricks/ev3dev/build-armel/pybricks-micropython /run/user/1000/gvfs/sftp:host=*/home/robot"
},
{
"label": "build virtualhub",
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## [Unreleased]

### Changed
- Updated MicroPython to v1.20.0.

## [3.3.0b4] - 2023-04-21

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2019-2022 The Pybricks Authors
# Copyright (c) 2019-2023 The Pybricks Authors

# ensure micropython submodule is checked out for building mpy-cross
ifeq ("$(wildcard micropython/README.md)","")
Expand Down Expand Up @@ -30,7 +30,7 @@ clean-doc:

all: movehub cityhub technichub primehub essentialhub virtualhub nxt debug ev3rt doc

clean-all: clean-movehub clean-cityhub clean-technichub clean-primehub clean-essentialhub clean-virtualhub clean-nxt clean-debug clean-doc
clean-all: clean-movehub clean-cityhub clean-technichub clean-primehub clean-essentialhub clean-virtualhub clean-nxt clean-debug clean-ev3rt clean-doc

ifeq ($(HOST_OS),Linux)

Expand Down
3 changes: 1 addition & 2 deletions bricks/_common/arm_none_eabi.mk
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ CFLAGS += -DSTM32_H='<stm32$(PB_MCU_SERIES_LCASE)xx.h>'
CFLAGS += -DSTM32_HAL_H='<stm32$(PB_MCU_SERIES_LCASE)xx_hal.h>'
endif

MPY_CROSS = $(PBTOP)/micropython/mpy-cross/mpy-cross

LIBS = "$(shell $(CC) $(CFLAGS) -print-libgcc-file-name)"

# Sources and libraries common to all pybricks bricks
Expand All @@ -192,6 +190,7 @@ include $(PBTOP)/bricks/_common/sources.mk
# between the top level directory and the micropython/ subdirectory.

PY_EXTRA_SRC_C = $(addprefix shared/,\
libc/printf.c \
libc/string0.c \
readline/readline.c \
runtime/gchelper_native.c \
Expand Down
56 changes: 49 additions & 7 deletions bricks/_common/micropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ void pb_event_poll_hook(void) {

// callback for when stop button is pressed in IDE or on hub
void pbsys_main_stop_program(bool force_stop) {
mp_sched_system_exit_or_abort(force_stop);
if (force_stop) {
mp_sched_vm_abort();
} else {
pyexec_system_exit = PYEXEC_FORCED_EXIT;

static mp_obj_exception_t system_exit;
system_exit.base.type = &mp_type_SystemExit;
system_exit.traceback_alloc = system_exit.traceback_len = 0;
system_exit.traceback_data = NULL;
system_exit.args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;

mp_sched_exception(MP_OBJ_FROM_PTR(&system_exit));
}
}

bool pbsys_main_stdin_event(uint8_t c) {
Expand Down Expand Up @@ -91,8 +103,8 @@ static void mp_vfs_map_minimal_new_reader(mp_reader_t *reader, mp_vfs_map_minima

// Prints the exception that ended the program.
static void print_final_exception(mp_obj_t exc) {
// Handle graceful stop with button or shutdown.
if (mp_obj_exception_match(exc, MP_OBJ_FROM_PTR(&mp_type_SystemAbort)) ||
// Handle graceful stop with button.
if (pyexec_system_exit == PYEXEC_FORCED_EXIT &&
mp_obj_exception_match(exc, MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
mp_printf(&mp_plat_print, "The program was stopped (%q).\n",
((mp_obj_exception_t *)MP_OBJ_TO_PTR(exc))->base.type->name);
Expand All @@ -105,19 +117,31 @@ static void print_final_exception(mp_obj_t exc) {

#if PYBRICKS_OPT_COMPILER
static void run_repl(void) {
// Reset REPL history.
readline_init0();
pyexec_system_exit = 0;

nlr_buf_t nlr;
nlr.ret_val = NULL;

if (nlr_push(&nlr) == 0) {
nlr_set_abort(&nlr);
// Run the REPL.
pyexec_friendly_repl();
nlr_pop();
} else {
// if vm abort
if (nlr.ret_val == NULL) {
// we are shutting down, so don't bother with cleanup
return;
}

// clear any pending exceptions (and run any callbacks).
mp_handle_pending(false);
// Print which exception triggered this.
print_final_exception(MP_OBJ_FROM_PTR(nlr.ret_val));
}

nlr_set_abort(NULL);
}
#endif

Expand Down Expand Up @@ -203,9 +227,14 @@ static mpy_info_t *mpy_data_find(qstr name) {
* Runs the __main__ module from user RAM.
*/
static void run_user_program(void) {
pyexec_system_exit = 0;

nlr_buf_t nlr;
nlr.ret_val = NULL;

if (nlr_push(&nlr) == 0) {
nlr_set_abort(&nlr);

mpy_info_t *info = mpy_data_find(MP_QSTR___main__);

if (!info) {
Expand All @@ -218,7 +247,9 @@ static void run_user_program(void) {
mp_vfs_map_minimal_new_reader(&reader, &data, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size));
mp_module_context_t *context = m_new_obj(mp_module_context_t);
context->module.globals = mp_globals_get();
mp_compiled_module_t compiled_module = mp_raw_code_load(&reader, context);
mp_compiled_module_t compiled_module;
compiled_module.context = context;
mp_raw_code_load(&reader, &compiled_module);
mp_obj_t module_fun = mp_make_function_from_raw_code(compiled_module.rc, context, NULL);

// Run the script while letting CTRL-C interrupt it.
Expand All @@ -231,8 +262,15 @@ static void run_user_program(void) {

nlr_pop();
} else {
// Clear any pending exceptions (and run any callbacks).
mp_hal_set_interrupt_char(-1);

// if vm abort
if (nlr.ret_val == NULL) {
// we are shutting down, so don't bother with cleanup
return;
}

// Clear any pending exceptions (and run any callbacks).
mp_handle_pending(false);

print_final_exception(MP_OBJ_FROM_PTR(nlr.ret_val));
Expand All @@ -251,6 +289,8 @@ static void run_user_program(void) {
}
#endif
}

nlr_set_abort(NULL);
}

// Runs MicroPython with the given program data.
Expand Down Expand Up @@ -340,7 +380,9 @@ mp_obj_t pb_builtin_import(size_t n_args, const mp_obj_t *args) {
// Create new module and execute in its own context.
mp_obj_t module_obj = mp_obj_new_module(module_name_qstr);
mp_module_context_t *context = MP_OBJ_TO_PTR(module_obj);
mp_compiled_module_t compiled_module = mp_raw_code_load(&reader, context);
mp_compiled_module_t compiled_module;
compiled_module.context = context;
mp_raw_code_load(&reader, &compiled_module);
do_execute_raw_code(context, compiled_module.rc, compiled_module.context);

// Return the newly imported module.
Expand Down
3 changes: 2 additions & 1 deletion bricks/_common/mpconfigport.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2019-2022 The Pybricks Authors
// Copyright (c) 2019-2023 The Pybricks Authors

// Common configuration of Pybricks MicroPython ports.

Expand Down Expand Up @@ -106,6 +106,7 @@
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
#endif
#define MICROPY_KBD_EXCEPTION (1)
#define MICROPY_ENABLE_VM_ABORT (1)
#define MICROPY_ENABLE_SCHEDULER (0)
#define MICROPY_PY_INSTANCE_ATTRS (1)

Expand Down
6 changes: 1 addition & 5 deletions bricks/_common_stm32/mpconfigport.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2013, 2014 Damien P. George
// Copyright (c) 2018-2022 The Pybricks Authors
// Copyright (c) 2018-2023 The Pybricks Authors

// Contains the MicroPython configuration for all STM32-based Pybricks ports.

Expand Down Expand Up @@ -67,7 +67,3 @@ static inline mp_uint_t disable_irq(void) {
#include <alloca.h>

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
mp_obj_dict_t *pb_type_Color_dict; \
const char *readline_hist[8];
Loading

0 comments on commit 4cd1e85

Please sign in to comment.