Skip to content

Commit 6c76376

Browse files
committed
restore sys.atexit() to prevent merge conflict
1 parent bdf8bc5 commit 6c76376

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

ports/unix/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,13 @@ MP_NOINLINE int main_(int argc, char **argv) {
690690
MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
691691
#endif
692692

693+
#if MICROPY_PY_SYS_ATEXIT
694+
// Beware, the sys.settrace callback should be disabled before running sys.atexit.
695+
if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) {
696+
mp_call_function_0(MP_STATE_VM(sys_exitfunc));
697+
}
698+
#endif
699+
693700
#if MICROPY_PY_MICROPYTHON_MEM_INFO
694701
if (mp_verbose_flag) {
695702
mp_micropython_mem_info(0, NULL);

py/modsys.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
166166
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
167167
#endif
168168

169+
#if MICROPY_PY_SYS_ATEXIT
170+
// atexit(callback): Callback is called when sys.exit is called.
171+
STATIC mp_obj_t mp_sys_atexit(mp_obj_t obj) {
172+
mp_obj_t old = MP_STATE_VM(sys_exitfunc);
173+
MP_STATE_VM(sys_exitfunc) = obj;
174+
return old;
175+
}
176+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_atexit_obj, mp_sys_atexit);
177+
#endif
178+
169179
#if MICROPY_PY_SYS_SETTRACE
170180
// settrace(tracefunc): Set the system’s trace function.
171181
STATIC mp_obj_t mp_sys_settrace(mp_obj_t obj) {
@@ -227,6 +237,14 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
227237
#if MICROPY_PY_SYS_GETSIZEOF
228238
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
229239
#endif
240+
241+
/*
242+
* Extensions to CPython
243+
*/
244+
245+
#if MICROPY_PY_SYS_ATEXIT
246+
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
247+
#endif
230248
};
231249

232250
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,11 @@ typedef double mp_float_t;
13111311
#define MICROPY_PY_SYS_EXIT (1)
13121312
#endif
13131313

1314+
// Whether to provide "sys.atexit" function (MicroPython extension)
1315+
#ifndef MICROPY_PY_SYS_ATEXIT
1316+
#define MICROPY_PY_SYS_ATEXIT (0)
1317+
#endif
1318+
13141319
// Whether to provide "sys.settrace" function
13151320
#ifndef MICROPY_PY_SYS_SETTRACE
13161321
#define MICROPY_PY_SYS_SETTRACE (0)

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ typedef struct _mp_state_vm_t {
157157
mp_obj_base_t *cur_exception;
158158
#endif
159159

160+
#if MICROPY_PY_SYS_ATEXIT
161+
// exposed through sys.atexit function
162+
mp_obj_t sys_exitfunc;
163+
#endif
164+
160165
// dictionary for the __main__ module
161166
mp_obj_dict_t dict_main;
162167

py/runtime.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ void mp_init(void) {
130130
sizeof(MP_STATE_VM(fs_user_mount)) - MICROPY_FATFS_NUM_PERSISTENT);
131131
#endif
132132

133+
#if MICROPY_PY_SYS_ATEXIT
134+
MP_STATE_VM(sys_exitfunc) = mp_const_none;
135+
#endif
136+
133137
#if MICROPY_PY_SYS_SETTRACE
134138
MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
135139
MP_STATE_THREAD(prof_callback_is_executing) = false;

0 commit comments

Comments
 (0)