summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-04-06 00:42:02 (GMT)
committerGitHub <noreply@github.com>2023-04-06 00:42:02 (GMT)
commit03089fdccc7dbe3f69227fbd570df92278371e7f (patch)
tree3b5440d14daa9f23689bfb2e07d0730f3839b219 /Python
parent4ec8dd10bd4682793559c4eccbcf6ae00688c4c3 (diff)
downloadcpython-03089fdccc7dbe3f69227fbd570df92278371e7f.zip
cpython-03089fdccc7dbe3f69227fbd570df92278371e7f.tar.gz
cpython-03089fdccc7dbe3f69227fbd570df92278371e7f.tar.bz2
gh-101659: Add _Py_AtExit() (gh-103298)
The function is like Py_AtExit() but for a single interpreter. This is a companion to the atexit module's register() function, taking a C callback instead of a Python one. We also update the _xxinterpchannels module to use _Py_AtExit(), which is the motivating case. (This is inspired by pain points felt while working on gh-101660.)
Diffstat (limited to 'Python')
-rw-r--r--Python/pylifecycle.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 8110d94..d6627bc 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2937,23 +2937,23 @@ wait_for_thread_shutdown(PyThreadState *tstate)
Py_DECREF(threading);
}
-#define NEXITFUNCS 32
int Py_AtExit(void (*func)(void))
{
- if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
+ if (_PyRuntime.atexit.ncallbacks >= NEXITFUNCS)
return -1;
- _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
+ _PyRuntime.atexit.callbacks[_PyRuntime.atexit.ncallbacks++] = func;
return 0;
}
static void
call_ll_exitfuncs(_PyRuntimeState *runtime)
{
- while (runtime->nexitfuncs > 0) {
+ struct _atexit_runtime_state *state = &runtime->atexit;
+ while (state->ncallbacks > 0) {
/* pop last function from the list */
- runtime->nexitfuncs--;
- void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
- runtime->exitfuncs[runtime->nexitfuncs] = NULL;
+ state->ncallbacks--;
+ atexit_callbackfunc exitfunc = state->callbacks[state->ncallbacks];
+ state->callbacks[state->ncallbacks] = NULL;
exitfunc();
}