add atexit module

This commit is contained in:
microDev 2021-07-30 08:36:24 +05:30
parent afa4ddb8d6
commit a3998d0626
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
10 changed files with 255 additions and 11 deletions

View File

@ -44,6 +44,10 @@
#include "lib/utils/pyexec.h"
#include "genhdr/mpversion.h"
#if CIRCUITPY_ATEXIT
#include "shared-module/atexit/__init__.h"
#endif
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
int pyexec_system_exit = 0;
@ -127,6 +131,10 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
start = mp_hal_ticks_ms();
#endif
mp_call_function_0(module_fun);
// execute exit handlers (if any).
#if CIRCUITPY_ATEXIT
shared_module_atexit_execute();
#endif
mp_hal_set_interrupt_char(-1); // disable interrupt
mp_handle_pending(true); // handle any pending exceptions (and any callbacks)
nlr_pop();

View File

@ -185,7 +185,7 @@ msgstr ""
msgid "'%q' object is not an iterator"
msgstr ""
#: py/objtype.c py/runtime.c
#: py/objtype.c py/runtime.c shared-module/atexit/__init__.c
msgid "'%q' object is not callable"
msgstr ""
@ -1185,11 +1185,6 @@ msgstr ""
msgid "Input/output error"
msgstr ""
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#, c-format
msgid "Missing jmp_pin. Instruction %d jumps on pin"
msgstr ""
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#, c-format
msgid "Instruction %d shifts in more bits than pin count"
@ -1506,6 +1501,11 @@ msgstr ""
msgid "Missing first_set_pin. Instruction %d sets pin(s)"
msgstr ""
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#, c-format
msgid "Missing jmp_pin. Instruction %d jumps on pin"
msgstr ""
#: shared-bindings/busio/UART.c shared-bindings/displayio/Group.c
msgid "Must be a %q subclass."
msgstr ""
@ -2510,7 +2510,7 @@ msgid "argument name reused"
msgstr ""
#: py/argcheck.c shared-bindings/_stage/__init__.c
#: shared-bindings/digitalio/DigitalInOut.c shared-bindings/gamepad/GamePad.c
#: shared-bindings/digitalio/DigitalInOut.c
msgid "argument num/types mismatch"
msgstr ""
@ -3594,10 +3594,6 @@ msgstr ""
msgid "no active exception to reraise"
msgstr ""
#: shared-bindings/socket/__init__.c shared-module/network/__init__.c
msgid "no available NIC"
msgstr ""
#: py/compile.c
msgid "no binding for nonlocal found"
msgstr ""

9
main.c
View File

@ -70,6 +70,10 @@
#include "shared-bindings/alarm/__init__.h"
#endif
#if CIRCUITPY_ATEXIT
#include "shared-module/atexit/__init__.h"
#endif
#if CIRCUITPY_BLEIO
#include "shared-bindings/_bleio/__init__.h"
#include "supervisor/shared/bluetooth/bluetooth.h"
@ -253,6 +257,11 @@ STATIC void cleanup_after_vm(supervisor_allocation* heap, mp_obj_t exception) {
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
reset_devices();
#if CIRCUITPY_ATEXIT
atexit_reset();
#endif
// Turn off the display and flush the filesystem before the heap disappears.
#if CIRCUITPY_DISPLAYIO
reset_displays();

View File

@ -20,6 +20,7 @@ CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_ATEXIT = 0
CIRCUITPY_PIXELBUF = 1
CIRCUITPY_BUSDEVICE = 1

View File

@ -114,6 +114,9 @@ endif
ifeq ($(CIRCUITPY_ANALOGIO),1)
SRC_PATTERNS += analogio/%
endif
ifeq ($(CIRCUITPY_ATEXIT),1)
SRC_PATTERNS += atexit/%
endif
ifeq ($(CIRCUITPY_AUDIOBUSIO),1)
SRC_PATTERNS += audiobusio/%
endif
@ -471,6 +474,7 @@ SRC_SHARED_MODULE_ALL = \
_stage/__init__.c \
aesio/__init__.c \
aesio/aes.c \
atexit/__init__.c \
audiocore/RawSample.c \
audiocore/WaveFile.c \
audiocore/__init__.c \

View File

@ -255,6 +255,13 @@ extern const struct _mp_obj_module_t analogio_module;
#define ANALOGIO_MODULE
#endif
#if CIRCUITPY_ATEXIT
extern const struct _mp_obj_module_t atexit_module;
#define ATEXIT_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_atexit), (mp_obj_t)&atexit_module },
#else
#define ATEXIT_MODULE
#endif
#if CIRCUITPY_AUDIOBUSIO
#define AUDIOBUSIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_audiobusio), (mp_obj_t)&audiobusio_module },
extern const struct _mp_obj_module_t audiobusio_module;
@ -853,6 +860,7 @@ extern const struct _mp_obj_module_t msgpack_module;
AESIO_MODULE \
ALARM_MODULE \
ANALOGIO_MODULE \
ATEXIT_MODULE \
AUDIOBUSIO_MODULE \
AUDIOCORE_MODULE \
AUDIOIO_MODULE \

View File

@ -48,6 +48,9 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM)
CIRCUITPY_ANALOGIO ?= 1
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
CIRCUITPY_ATEXIT ?= 1
CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT)
CIRCUITPY_AUDIOBUSIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_AUDIOBUSIO=$(CIRCUITPY_AUDIOBUSIO)

View File

@ -0,0 +1,89 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-module/atexit/__init__.h"
//| """Atexit Module
//|
//| This module defines functions to register and unregister cleanup functions.
//| Functions thus registered are automatically executed upon normal vm termination.
//|
//| """
//| ...
//|
//| def register(func: Callable[..., Any], *args: Optional[Any], **kwargs: Optional[Any]) -> Callable[..., Any]:
//|
//| """Register func as a function to be executed at termination.
//|
//| Any optional arguments that are to be passed to func must be passed as arguments to register().
//| It is possible to register the same function and arguments more than once.
//|
//| At normal program termination (for instance, if `sys.exit()` is called or the vm execution completes),
//| all functions registered are called in order of there registration.
//|
//| If an exception is raised during execution of the exit handlers, a traceback is printed and the execution stops.
//|
//| This function returns func, which makes it possible to use it as a decorator.
//|
//| """
//| ...
//|
STATIC mp_obj_t atexit_register(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
shared_module_atexit_register(pos_args[0], (n_args - 1), ((n_args > 1) ? &pos_args[1] : NULL), kw_args);
return pos_args[0];
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(atexit_register_obj, 1, atexit_register);
//| def unregister(func: Callable[..., Any]) -> None:
//|
//| """Remove func from the list of functions to be run at termination.
//|
//| `unregister()` silently does nothing if func was not previously registered. If func has been registered more than once,
//| every occurrence of that function in the atexit call stack will be removed.
//|
//| """
//| ...
//|
STATIC mp_obj_t atexit_unregister(const mp_obj_t self_in) {
shared_module_atexit_unregister(&self_in);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(atexit_unregister_obj, atexit_unregister);
STATIC const mp_rom_map_elem_t atexit_module_globals_table[] = {
// module name
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_atexit) },
// module functions
{ MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&atexit_register_obj) },
{ MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&atexit_unregister_obj) },
};
STATIC MP_DEFINE_CONST_DICT(atexit_module_globals, atexit_module_globals_table);
const mp_obj_module_t atexit_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&atexit_module_globals,
};

View File

@ -0,0 +1,87 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "shared-module/atexit/__init__.h"
typedef struct _atexit_callback_t {
size_t n_pos, n_kw;
mp_obj_t func, *args;
} atexit_callback_t;
size_t callback_len = 0;
atexit_callback_t *callback = NULL;
void atexit_reset(void) {
callback_len = 0;
m_free(callback);
callback = NULL;
}
void shared_module_atexit_register(mp_obj_t *func, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
if (!mp_obj_is_callable(func)) {
mp_raise_TypeError_varg(translate("'%q' object is not callable"), mp_obj_get_type_qstr(func));
}
size_t n_kw_args = (kw_args) ? kw_args->used : 0;
atexit_callback_t cb = {
.n_pos = 0,
.n_kw = 0,
.func = func,
.args = ((n_args + n_kw_args) == 0) ? NULL : m_malloc((n_args + (2 * n_kw_args)) * sizeof(mp_obj_t), false)
};
for (; cb.n_pos < n_args; cb.n_pos++) {
cb.args[cb.n_pos] = pos_args[cb.n_pos];
}
for (size_t i = cb.n_pos; cb.n_kw < n_kw_args; i++, cb.n_kw++) {
cb.args[i] = kw_args[cb.n_kw].table->key;
cb.args[i += 1] = kw_args[cb.n_kw].table->value;
}
if (!callback) {
callback = (atexit_callback_t *)m_realloc(callback, sizeof(cb));
}
callback[callback_len++] = cb;
}
void shared_module_atexit_unregister(const mp_obj_t *func) {
for (size_t i = 0; i < callback_len; i++) {
if (callback[i].func == func) {
callback[i].n_pos = 0;
callback[i].n_kw = 0;
callback[i].func = mp_const_none;
callback[i].args = NULL;
}
}
}
void shared_module_atexit_execute(void) {
if (callback) {
for (size_t i = 0; i < callback_len; i++) {
if (callback[i].func != mp_const_none) {
mp_call_function_n_kw(callback[i].func, callback[i].n_pos, callback[i].n_kw, callback[i].args);
}
}
}
}

View File

@ -0,0 +1,39 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H
#define MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H
#include "py/obj.h"
extern void atexit_reset(void);
extern void shared_module_atexit_register(mp_obj_t *func,
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern void shared_module_atexit_unregister(const mp_obj_t *func);
extern void shared_module_atexit_execute(void);
#endif // MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H