File tree Expand file tree Collapse file tree 5 files changed +39
-0
lines changed Expand file tree Collapse file tree 5 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -690,6 +690,13 @@ MP_NOINLINE int main_(int argc, char **argv) {
690
690
MP_STATE_THREAD (prof_trace_callback ) = MP_OBJ_NULL ;
691
691
#endif
692
692
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
+
693
700
#if MICROPY_PY_MICROPYTHON_MEM_INFO
694
701
if (mp_verbose_flag ) {
695
702
mp_micropython_mem_info (0 , NULL );
Original file line number Diff line number Diff line change @@ -166,6 +166,16 @@ STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
166
166
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_sys_getsizeof_obj , mp_sys_getsizeof );
167
167
#endif
168
168
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
+
169
179
#if MICROPY_PY_SYS_SETTRACE
170
180
// settrace(tracefunc): Set the system’s trace function.
171
181
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[] = {
227
237
#if MICROPY_PY_SYS_GETSIZEOF
228
238
{ MP_ROM_QSTR (MP_QSTR_getsizeof ), MP_ROM_PTR (& mp_sys_getsizeof_obj ) },
229
239
#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
230
248
};
231
249
232
250
STATIC MP_DEFINE_CONST_DICT (mp_module_sys_globals , mp_module_sys_globals_table );
Original file line number Diff line number Diff line change @@ -1311,6 +1311,11 @@ typedef double mp_float_t;
1311
1311
#define MICROPY_PY_SYS_EXIT (1)
1312
1312
#endif
1313
1313
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
+
1314
1319
// Whether to provide "sys.settrace" function
1315
1320
#ifndef MICROPY_PY_SYS_SETTRACE
1316
1321
#define MICROPY_PY_SYS_SETTRACE (0)
Original file line number Diff line number Diff line change @@ -157,6 +157,11 @@ typedef struct _mp_state_vm_t {
157
157
mp_obj_base_t * cur_exception ;
158
158
#endif
159
159
160
+ #if MICROPY_PY_SYS_ATEXIT
161
+ // exposed through sys.atexit function
162
+ mp_obj_t sys_exitfunc ;
163
+ #endif
164
+
160
165
// dictionary for the __main__ module
161
166
mp_obj_dict_t dict_main ;
162
167
Original file line number Diff line number Diff line change @@ -130,6 +130,10 @@ void mp_init(void) {
130
130
sizeof (MP_STATE_VM (fs_user_mount )) - MICROPY_FATFS_NUM_PERSISTENT );
131
131
#endif
132
132
133
+ #if MICROPY_PY_SYS_ATEXIT
134
+ MP_STATE_VM (sys_exitfunc ) = mp_const_none ;
135
+ #endif
136
+
133
137
#if MICROPY_PY_SYS_SETTRACE
134
138
MP_STATE_THREAD (prof_trace_callback ) = MP_OBJ_NULL ;
135
139
MP_STATE_THREAD (prof_callback_is_executing ) = false;
You can’t perform that action at this time.
0 commit comments