bpo-42639: Move atexit state to PyInterpreterState (GH-23763)

* Add _PyAtExit_Call() function and remove pyexitfunc and
  pyexitmodule members of PyInterpreterState. The function
  logs atexit callback errors using _PyErr_WriteUnraisableMsg().
* Add _PyAtExit_Init() and _PyAtExit_Fini() functions.
* Remove traverse, clear and free functions of the atexit module.

Co-authored-by: Dong-hee Na <donghee.na@python.org>
This commit is contained in:
Victor Stinner 2020-12-15 14:34:19 +01:00 committed by GitHub
parent 8473cf89bd
commit b8fa135908
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 113 deletions

View file

@ -55,7 +55,6 @@ static PyStatus add_main_module(PyInterpreterState *interp);
static PyStatus init_import_site(void);
static PyStatus init_set_builtins_open(void);
static PyStatus init_sys_streams(PyThreadState *tstate);
static void call_py_exitfuncs(PyThreadState *tstate);
static void wait_for_thread_shutdown(PyThreadState *tstate);
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
@ -690,6 +689,12 @@ pycore_init_types(PyThreadState *tstate)
if (_PyWarnings_InitState(tstate) < 0) {
return _PyStatus_ERR("can't initialize warnings");
}
status = _PyAtExit_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_OK();
}
@ -1655,7 +1660,7 @@ Py_FinalizeEx(void)
* the threads created via Threading.
*/
call_py_exitfuncs(tstate);
_PyAtExit_Call(tstate);
/* Copy the core config, PyInterpreterState_Delete() free
the core config memory */
@ -1946,7 +1951,7 @@ Py_EndInterpreter(PyThreadState *tstate)
// Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown(tstate);
call_py_exitfuncs(tstate);
_PyAtExit_Call(tstate);
if (tstate != interp->tstate_head || tstate->next != NULL) {
Py_FatalError("not the last thread");
@ -2633,15 +2638,6 @@ Py_ExitStatusException(PyStatus status)
}
/* Clean up and exit */
static void
call_py_exitfuncs(PyThreadState *tstate)
{
_PyAtExit_Call(tstate->interp->atexit_module);
}
/* Wait until threading._shutdown completes, provided
the threading module was imported in the first place.
The shutdown routine will wait until all non-daemon