-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/modatexit: Add atexit module for deinit functionality. #16063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewleech
wants to merge
8
commits into
micropython:master
Choose a base branch
from
andrewleech:module_deinit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−18
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
71364bb
py/modatexit: Add atexit module.
pi-anl 37bd082
examples/usercmodule: Add atexit, __init__ and root pointer to cexample.
pi-anl 88bbdd7
shared/runtime/pyexec: Add default exception handler function.
pi-anl 79ea5a7
unix/main: Update handle_uncaught_exception for consistency with pyexec.
pi-anl f431ecf
unix/main.c: Integrate atexit to be run while exiting.
pi-anl 1ce8827
shared/runtime/pyexec: Integrate atexit to be run during exit process.
andrewleech 763c1d7
webassembly: Disable atexit module.
pi-anl 4bd2fa2
qemu/main.c: Integrate atexit to be run while exiting.
pi-anl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
:mod:`atexit` -- exit handlers | ||
============================== | ||
|
||
.. module:: atexit | ||
:synopsis: The atexit module defines functions to register and unregister cleanup functions. | ||
|
||
|see_cpython_module| :mod:`python:atexit`. | ||
|
||
Functions | ||
--------- | ||
|
||
.. function:: register(func) | ||
|
||
Register func as a function to be executed on :ref:`soft_reset` or exit on unix/windows ports. | ||
Functions are executed in "lifo" registration order, ie the last registered is the first executed. | ||
|
||
.. function:: unregister(func) | ||
|
||
Remove func from the list of functions to be run at interpreter shutdown. Not enabled on all ports. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ library. | |
:maxdepth: 1 | ||
|
||
array.rst | ||
atexit.rst | ||
asyncio.rst | ||
binascii.rst | ||
builtins.rst | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2025 Andrew Leech | ||
* | ||
* 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 <assert.h> | ||
#include <string.h> | ||
|
||
#include "py/obj.h" | ||
#include "py/runtime.h" | ||
#include "py/modatexit.h" | ||
#include "shared/runtime/pyexec.h" | ||
|
||
#if MICROPY_PY_ATEXIT | ||
|
||
mp_obj_t mp_atexit_register(mp_obj_t function) { | ||
if (!mp_obj_is_callable(function)) { | ||
mp_raise_ValueError(MP_ERROR_TEXT("function not callable")); | ||
} | ||
if (MP_STATE_VM(atexit_handlers) == NULL) { | ||
MP_STATE_VM(atexit_handlers) = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL)); | ||
} | ||
mp_obj_list_append(MP_OBJ_FROM_PTR(MP_STATE_VM(atexit_handlers)), function); | ||
// return the passed in function so this can be used as a decorator | ||
return function; | ||
} | ||
static MP_DEFINE_CONST_FUN_OBJ_1(mp_atexit_register_obj, mp_atexit_register); | ||
|
||
#if MICROPY_PY_ATEXIT_UNREGISTER | ||
mp_obj_t mp_atexit_unregister(mp_obj_t function) { | ||
nlr_buf_t nlr; | ||
// ValueError is thrown when function is no longer in the list | ||
if (nlr_push(&nlr) == 0) { | ||
while (MP_STATE_VM(atexit_handlers) != NULL) { | ||
mp_obj_list_remove(MP_OBJ_FROM_PTR(MP_STATE_VM(atexit_handlers)), function); | ||
} | ||
} | ||
return mp_const_none; | ||
} | ||
static MP_DEFINE_CONST_FUN_OBJ_1(mp_atexit_unregister_obj, mp_atexit_unregister); | ||
#endif | ||
|
||
// port specific shutdown procedures should cLl this | ||
// to run any registered atexit handlers. | ||
int mp_atexit_execute(void) { | ||
int exit_code = 0; | ||
if (MP_STATE_VM(atexit_handlers) != NULL) { | ||
mp_obj_list_t *list = MP_STATE_VM(atexit_handlers); | ||
for (int i = list->len - 1; i >= 0; i--) { | ||
mp_obj_t function = list->items[i]; | ||
|
||
nlr_buf_t nlr; | ||
if (nlr_push(&nlr) == 0) { | ||
mp_call_function_0(function); | ||
} else { | ||
exit_code = pyexec_handle_uncaught_exception(nlr.ret_val); | ||
} | ||
} | ||
} | ||
return exit_code; | ||
} | ||
|
||
static const mp_rom_map_elem_t mp_module_atexit_globals_table[] = { | ||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_atexit) }, | ||
{ MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&mp_atexit_register_obj) }, | ||
#if MICROPY_PY_ATEXIT_UNREGISTER | ||
{ MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&mp_atexit_unregister_obj) }, | ||
#endif | ||
}; | ||
static MP_DEFINE_CONST_DICT(mp_module_atexit_globals, mp_module_atexit_globals_table); | ||
|
||
const mp_obj_module_t mp_module_atexit = { | ||
.base = { &mp_type_module }, | ||
.globals = (mp_obj_dict_t *)&mp_module_atexit_globals, | ||
}; | ||
|
||
MP_REGISTER_ROOT_POINTER(mp_obj_list_t * atexit_handlers); | ||
MP_REGISTER_MODULE(MP_QSTR_atexit, mp_module_atexit); | ||
|
||
#endif // MICROPY_PY_ATEXIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2024 Andrew Leech | ||
* | ||
* 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_PY_MODATEXIT_H | ||
#define MICROPY_INCLUDED_PY_MODATEXIT_H | ||
|
||
int mp_atexit_execute(void); | ||
mp_obj_t mp_atexit_register(mp_obj_t callback); | ||
mp_obj_t mp_atexit_unregister(mp_obj_t callback); | ||
|
||
#endif // MICROPY_INCLUDED_PY_MODATEXIT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CPython doesn't seem to update the process exit code based on anything which happens in an atexit handler (I checked by first reading the source and then writing a silly example):
(Similarly if you comment the
raise RuntimeError()
line, the exit code becomes 0.)Removing some of the machinery for this might(?) save a small amount of code size on bare metal ports as well, if we're lucky...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an interesting point, yeah I assumed we should capture any failures here but for code size and compatibility it's worth trying without