Skip to content

Commit a3998d0

Browse files
committed
add atexit module
1 parent afa4ddb commit a3998d0

File tree

10 files changed

+255
-11
lines changed

10 files changed

+255
-11
lines changed

lib/utils/pyexec.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#include "lib/utils/pyexec.h"
4545
#include "genhdr/mpversion.h"
4646

47+
#if CIRCUITPY_ATEXIT
48+
#include "shared-module/atexit/__init__.h"
49+
#endif
50+
4751
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
4852
int pyexec_system_exit = 0;
4953

@@ -127,6 +131,10 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
127131
start = mp_hal_ticks_ms();
128132
#endif
129133
mp_call_function_0(module_fun);
134+
// execute exit handlers (if any).
135+
#if CIRCUITPY_ATEXIT
136+
shared_module_atexit_execute();
137+
#endif
130138
mp_hal_set_interrupt_char(-1); // disable interrupt
131139
mp_handle_pending(true); // handle any pending exceptions (and any callbacks)
132140
nlr_pop();

locale/circuitpython.pot

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ msgstr ""
185185
msgid "'%q' object is not an iterator"
186186
msgstr ""
187187

188-
#: py/objtype.c py/runtime.c
188+
#: py/objtype.c py/runtime.c shared-module/atexit/__init__.c
189189
msgid "'%q' object is not callable"
190190
msgstr ""
191191

@@ -1185,11 +1185,6 @@ msgstr ""
11851185
msgid "Input/output error"
11861186
msgstr ""
11871187

1188-
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
1189-
#, c-format
1190-
msgid "Missing jmp_pin. Instruction %d jumps on pin"
1191-
msgstr ""
1192-
11931188
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
11941189
#, c-format
11951190
msgid "Instruction %d shifts in more bits than pin count"
@@ -1506,6 +1501,11 @@ msgstr ""
15061501
msgid "Missing first_set_pin. Instruction %d sets pin(s)"
15071502
msgstr ""
15081503

1504+
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
1505+
#, c-format
1506+
msgid "Missing jmp_pin. Instruction %d jumps on pin"
1507+
msgstr ""
1508+
15091509
#: shared-bindings/busio/UART.c shared-bindings/displayio/Group.c
15101510
msgid "Must be a %q subclass."
15111511
msgstr ""
@@ -2510,7 +2510,7 @@ msgid "argument name reused"
25102510
msgstr ""
25112511

25122512
#: py/argcheck.c shared-bindings/_stage/__init__.c
2513-
#: shared-bindings/digitalio/DigitalInOut.c shared-bindings/gamepad/GamePad.c
2513+
#: shared-bindings/digitalio/DigitalInOut.c
25142514
msgid "argument num/types mismatch"
25152515
msgstr ""
25162516

@@ -3594,10 +3594,6 @@ msgstr ""
35943594
msgid "no active exception to reraise"
35953595
msgstr ""
35963596

3597-
#: shared-bindings/socket/__init__.c shared-module/network/__init__.c
3598-
msgid "no available NIC"
3599-
msgstr ""
3600-
36013597
#: py/compile.c
36023598
msgid "no binding for nonlocal found"
36033599
msgstr ""

main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
#include "shared-bindings/alarm/__init__.h"
7171
#endif
7272

73+
#if CIRCUITPY_ATEXIT
74+
#include "shared-module/atexit/__init__.h"
75+
#endif
76+
7377
#if CIRCUITPY_BLEIO
7478
#include "shared-bindings/_bleio/__init__.h"
7579
#include "supervisor/shared/bluetooth/bluetooth.h"
@@ -253,6 +257,11 @@ STATIC void cleanup_after_vm(supervisor_allocation* heap, mp_obj_t exception) {
253257

254258
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
255259
reset_devices();
260+
261+
#if CIRCUITPY_ATEXIT
262+
atexit_reset();
263+
#endif
264+
256265
// Turn off the display and flush the filesystem before the heap disappears.
257266
#if CIRCUITPY_DISPLAYIO
258267
reset_displays();

ports/atmel-samd/boards/adafruit_proxlight_trinkey_m0/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CIRCUITPY_ROTARYIO = 0
2020
CIRCUITPY_RTC = 0
2121
CIRCUITPY_USB_MIDI = 0
2222

23+
CIRCUITPY_ATEXIT = 0
2324
CIRCUITPY_PIXELBUF = 1
2425
CIRCUITPY_BUSDEVICE = 1
2526

py/circuitpy_defns.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ endif
114114
ifeq ($(CIRCUITPY_ANALOGIO),1)
115115
SRC_PATTERNS += analogio/%
116116
endif
117+
ifeq ($(CIRCUITPY_ATEXIT),1)
118+
SRC_PATTERNS += atexit/%
119+
endif
117120
ifeq ($(CIRCUITPY_AUDIOBUSIO),1)
118121
SRC_PATTERNS += audiobusio/%
119122
endif
@@ -471,6 +474,7 @@ SRC_SHARED_MODULE_ALL = \
471474
_stage/__init__.c \
472475
aesio/__init__.c \
473476
aesio/aes.c \
477+
atexit/__init__.c \
474478
audiocore/RawSample.c \
475479
audiocore/WaveFile.c \
476480
audiocore/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ extern const struct _mp_obj_module_t analogio_module;
255255
#define ANALOGIO_MODULE
256256
#endif
257257

258+
#if CIRCUITPY_ATEXIT
259+
extern const struct _mp_obj_module_t atexit_module;
260+
#define ATEXIT_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_atexit), (mp_obj_t)&atexit_module },
261+
#else
262+
#define ATEXIT_MODULE
263+
#endif
264+
258265
#if CIRCUITPY_AUDIOBUSIO
259266
#define AUDIOBUSIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_audiobusio), (mp_obj_t)&audiobusio_module },
260267
extern const struct _mp_obj_module_t audiobusio_module;
@@ -853,6 +860,7 @@ extern const struct _mp_obj_module_t msgpack_module;
853860
AESIO_MODULE \
854861
ALARM_MODULE \
855862
ANALOGIO_MODULE \
863+
ATEXIT_MODULE \
856864
AUDIOBUSIO_MODULE \
857865
AUDIOCORE_MODULE \
858866
AUDIOIO_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM)
4848
CIRCUITPY_ANALOGIO ?= 1
4949
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
5050

51+
CIRCUITPY_ATEXIT ?= 1
52+
CFLAGS += -DCIRCUITPY_ATEXIT=$(CIRCUITPY_ATEXIT)
53+
5154
CIRCUITPY_AUDIOBUSIO ?= $(CIRCUITPY_FULL_BUILD)
5255
CFLAGS += -DCIRCUITPY_AUDIOBUSIO=$(CIRCUITPY_AUDIOBUSIO)
5356

shared-bindings/atexit/__init__.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-module/atexit/__init__.h"
28+
29+
//| """Atexit Module
30+
//|
31+
//| This module defines functions to register and unregister cleanup functions.
32+
//| Functions thus registered are automatically executed upon normal vm termination.
33+
//|
34+
//| """
35+
//| ...
36+
//|
37+
38+
//| def register(func: Callable[..., Any], *args: Optional[Any], **kwargs: Optional[Any]) -> Callable[..., Any]:
39+
//|
40+
//| """Register func as a function to be executed at termination.
41+
//|
42+
//| Any optional arguments that are to be passed to func must be passed as arguments to register().
43+
//| It is possible to register the same function and arguments more than once.
44+
//|
45+
//| At normal program termination (for instance, if `sys.exit()` is called or the vm execution completes),
46+
//| all functions registered are called in order of there registration.
47+
//|
48+
//| If an exception is raised during execution of the exit handlers, a traceback is printed and the execution stops.
49+
//|
50+
//| This function returns func, which makes it possible to use it as a decorator.
51+
//|
52+
//| """
53+
//| ...
54+
//|
55+
STATIC mp_obj_t atexit_register(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
56+
shared_module_atexit_register(pos_args[0], (n_args - 1), ((n_args > 1) ? &pos_args[1] : NULL), kw_args);
57+
return pos_args[0];
58+
}
59+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(atexit_register_obj, 1, atexit_register);
60+
61+
//| def unregister(func: Callable[..., Any]) -> None:
62+
//|
63+
//| """Remove func from the list of functions to be run at termination.
64+
//|
65+
//| `unregister()` silently does nothing if func was not previously registered. If func has been registered more than once,
66+
//| every occurrence of that function in the atexit call stack will be removed.
67+
//|
68+
//| """
69+
//| ...
70+
//|
71+
STATIC mp_obj_t atexit_unregister(const mp_obj_t self_in) {
72+
shared_module_atexit_unregister(&self_in);
73+
return mp_const_none;
74+
}
75+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(atexit_unregister_obj, atexit_unregister);
76+
77+
STATIC const mp_rom_map_elem_t atexit_module_globals_table[] = {
78+
// module name
79+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_atexit) },
80+
// module functions
81+
{ MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&atexit_register_obj) },
82+
{ MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&atexit_unregister_obj) },
83+
};
84+
STATIC MP_DEFINE_CONST_DICT(atexit_module_globals, atexit_module_globals_table);
85+
86+
const mp_obj_module_t atexit_module = {
87+
.base = { &mp_type_module },
88+
.globals = (mp_obj_dict_t *)&atexit_module_globals,
89+
};

shared-module/atexit/__init__.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "shared-module/atexit/__init__.h"
29+
30+
typedef struct _atexit_callback_t {
31+
size_t n_pos, n_kw;
32+
mp_obj_t func, *args;
33+
} atexit_callback_t;
34+
35+
size_t callback_len = 0;
36+
atexit_callback_t *callback = NULL;
37+
38+
void atexit_reset(void) {
39+
callback_len = 0;
40+
m_free(callback);
41+
callback = NULL;
42+
}
43+
44+
void shared_module_atexit_register(mp_obj_t *func, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
45+
if (!mp_obj_is_callable(func)) {
46+
mp_raise_TypeError_varg(translate("'%q' object is not callable"), mp_obj_get_type_qstr(func));
47+
}
48+
size_t n_kw_args = (kw_args) ? kw_args->used : 0;
49+
atexit_callback_t cb = {
50+
.n_pos = 0,
51+
.n_kw = 0,
52+
.func = func,
53+
.args = ((n_args + n_kw_args) == 0) ? NULL : m_malloc((n_args + (2 * n_kw_args)) * sizeof(mp_obj_t), false)
54+
};
55+
for (; cb.n_pos < n_args; cb.n_pos++) {
56+
cb.args[cb.n_pos] = pos_args[cb.n_pos];
57+
}
58+
for (size_t i = cb.n_pos; cb.n_kw < n_kw_args; i++, cb.n_kw++) {
59+
cb.args[i] = kw_args[cb.n_kw].table->key;
60+
cb.args[i += 1] = kw_args[cb.n_kw].table->value;
61+
}
62+
if (!callback) {
63+
callback = (atexit_callback_t *)m_realloc(callback, sizeof(cb));
64+
}
65+
callback[callback_len++] = cb;
66+
}
67+
68+
void shared_module_atexit_unregister(const mp_obj_t *func) {
69+
for (size_t i = 0; i < callback_len; i++) {
70+
if (callback[i].func == func) {
71+
callback[i].n_pos = 0;
72+
callback[i].n_kw = 0;
73+
callback[i].func = mp_const_none;
74+
callback[i].args = NULL;
75+
}
76+
}
77+
}
78+
79+
void shared_module_atexit_execute(void) {
80+
if (callback) {
81+
for (size_t i = 0; i < callback_len; i++) {
82+
if (callback[i].func != mp_const_none) {
83+
mp_call_function_n_kw(callback[i].func, callback[i].n_pos, callback[i].n_kw, callback[i].args);
84+
}
85+
}
86+
}
87+
}

shared-module/atexit/__init__.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H
29+
30+
#include "py/obj.h"
31+
32+
extern void atexit_reset(void);
33+
34+
extern void shared_module_atexit_register(mp_obj_t *func,
35+
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
36+
extern void shared_module_atexit_unregister(const mp_obj_t *func);
37+
extern void shared_module_atexit_execute(void);
38+
39+
#endif // MICROPY_INCLUDED_SHARED_MODULE_ATEXIT___INIT___H

0 commit comments

Comments
 (0)