|
| 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 | +}; |
0 commit comments