Skip to content

Commit 22e87fb

Browse files
committed
Add atexit module.
1 parent 7f30be8 commit 22e87fb

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

docs/library/atexit.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ Functions
1515

1616
.. function:: unregister(func)
1717

18-
Remove func from the list of functions to be run at interpreter shutdown. Not enebled on all ports.
18+
Remove func from the list of functions to be run at interpreter shutdown. Not enabled on all ports.

py/modatexit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mp_obj_t mp_atexit_register(mp_obj_t function) {
3939
mp_raise_ValueError(MP_ERROR_TEXT("function not callable"));
4040
}
4141
if (MP_STATE_VM(atexit_handlers) == NULL) {
42-
MP_STATE_VM(atexit_handlers) = mp_obj_new_list(0, NULL);
42+
MP_STATE_VM(atexit_handlers) = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL));
4343
}
4444
mp_obj_list_append(MP_OBJ_FROM_PTR(MP_STATE_VM(atexit_handlers)), function);
4545
// return the passed in function so this can be used as a decorator
@@ -61,11 +61,13 @@ mp_obj_t mp_atexit_unregister(mp_obj_t function) {
6161
static MP_DEFINE_CONST_FUN_OBJ_1(mp_atexit_unregister_obj, mp_atexit_unregister);
6262
#endif
6363

64+
// port specific shutdown procedures should cLl this
65+
// to run any registered atexit handlers.
6466
int mp_atexit_execute(void) {
6567
int exit_code = 0;
6668
if (MP_STATE_VM(atexit_handlers) != NULL) {
6769
mp_obj_list_t *list = MP_STATE_VM(atexit_handlers);
68-
for (size_t i = 0; i < list->len; i++) {
70+
for (int i = list->len - 1; i >= 0; i--) {
6971
mp_obj_t function = list->items[i];
7072

7173
nlr_buf_t nlr;

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void mp_init(void) {
172172
#endif
173173

174174
#if MICROPY_PY_ATEXIT
175-
MP_STATE_VM(atexit) = NULL;
175+
MP_STATE_VM(atexit_handlers) = NULL;
176176
#endif
177177

178178
#if MICROPY_PY_BLUETOOTH

0 commit comments

Comments
 (0)