gh-106320: Remove private pylifecycle.h functions (#106400)

Remove private pylifecycle.h functions: move them to the internal C
API ( pycore_atexit.h, pycore_pylifecycle.h and pycore_signal.h). No
longer export most of these functions.

Move _testcapi.test_atexit() to _testinternalcapi.
This commit is contained in:
Victor Stinner 2023-07-04 11:41:43 +02:00 committed by GitHub
parent 8a73b57b9b
commit c9ce983ae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 104 additions and 76 deletions

View file

@ -3,10 +3,11 @@
#endif
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_runtime_init.h" // _Py_ID()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()

View file

@ -11,6 +11,7 @@
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_object.h"
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "structmember.h" // PyMemberDef
#include "_iomodule.h"

View file

@ -6,6 +6,7 @@
#include "Python.h"
#include "pycore_fileutils.h"
#include "pycore_pystate.h"
#include "pycore_signal.h" // _Py_RestoreSignals()
#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
@ -739,8 +740,9 @@ child_exec(char *const exec_array[],
if (child_umask >= 0)
umask(child_umask); /* umask() always succeeds. */
if (restore_signals)
if (restore_signals) {
_Py_RestoreSignals();
}
#ifdef VFORK_USABLE
if (child_sigmask) {

View file

@ -72,6 +72,7 @@
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
#include "pycore_runtime.h"
#ifdef HAVE_PROCESS_H
# include <process.h> // getpid()

View file

@ -34,6 +34,7 @@
#include "prepare_protocol.h"
#include "util.h"
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
#include <stdbool.h>

View file

@ -3293,37 +3293,6 @@ function_set_kw_defaults(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
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 *
check_pyimport_addmodule(PyObject *self, PyObject *args)
{
@ -3613,7 +3582,6 @@ static PyMethodDef TestMethods[] = {
{"function_set_defaults", function_set_defaults, METH_VARARGS, NULL},
{"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
{"test_atexit", test_atexit, METH_NOARGS},
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
{NULL, NULL} /* sentinel */

View file

@ -1264,6 +1264,38 @@ unicode_transformdecimalandspacetoascii(PyObject *self, PyObject *arg)
}
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 PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@ -1316,6 +1348,7 @@ static PyMethodDef module_functions[] = {
{"_PyTraceMalloc_GetTraceback", tracemalloc_get_traceback, METH_VARARGS},
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
{"_PyUnicode_TransformDecimalAndSpaceToASCII", unicode_transformdecimalandspacetoascii, METH_O},
{"test_atexit", test_atexit, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

View file

@ -1,9 +1,13 @@
/* interpreters module */
/* low-level access to interpreter primitives */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h"
#include "interpreteridobject.h"
#include "pycore_atexit.h" // _Py_AtExit()
/*

View file

@ -1,4 +1,9 @@
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h"
#include "pycore_pylifecycle.h" // _Py_gitidentifier()
#ifndef DONT_HAVE_STDIO_H
#include <stdio.h>

View file

@ -19,6 +19,7 @@
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _PyObject_LookupSpecial()
#include "pycore_pylifecycle.h" // _PyOS_URandom()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_signal.h" // Py_NSIG

View file

@ -4,8 +4,13 @@
* recently, it was largely rewritten by Guido van Rossum.
*/
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
/* Standard definitions */
#include "Python.h"
#include "pycore_pylifecycle.h" // _Py_SetLocaleFromEnv()
#include <errno.h>
#include <signal.h>

View file

@ -13,7 +13,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pyerrors.h" // _PyErr_SetString()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_signal.h" // Py_NSIG
#include "pycore_signal.h" // _Py_RestoreSignals()
#ifndef MS_WINDOWS
# include "posixmodule.h"