mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
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.)
This commit is contained in:
parent
4ec8dd10bd
commit
03089fdccc
13 changed files with 268 additions and 67 deletions
|
|
@ -3381,6 +3381,37 @@ test_gc_visit_objects_exit_early(PyObject *Py_UNUSED(self),
|
|||
}
|
||||
|
||||
|
||||
struct atexit_data {
|
||||
int called;
|
||||
};
|
||||
|
||||
static void
|
||||
callback(void *data)
|
||||
{
|
||||
((struct atexit_data *)data)->called += 1;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
|
||||
{
|
||||
PyThreadState *oldts = PyThreadState_Swap(NULL);
|
||||
PyThreadState *tstate = Py_NewInterpreter();
|
||||
|
||||
struct atexit_data data = {0};
|
||||
int res = _Py_AtExit(tstate->interp, callback, (void *)&data);
|
||||
Py_EndInterpreter(tstate);
|
||||
PyThreadState_Swap(oldts);
|
||||
if (res < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (data.called == 0) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "atexit callback not called");
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
|
||||
|
||||
static PyMethodDef TestMethods[] = {
|
||||
|
|
@ -3525,6 +3556,7 @@ static PyMethodDef TestMethods[] = {
|
|||
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
|
||||
{"test_gc_visit_objects_basic", test_gc_visit_objects_basic, METH_NOARGS, NULL},
|
||||
{"test_gc_visit_objects_exit_early", test_gc_visit_objects_exit_early, METH_NOARGS, NULL},
|
||||
{"test_atexit", test_atexit, METH_NOARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue