Skip to content

Commit c9ce983

Browse files
authored
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.
1 parent 8a73b57 commit c9ce983

19 files changed

+104
-76
lines changed

Include/cpython/pylifecycle.h

+2-25
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,22 @@ PyAPI_FUNC(PyStatus) Py_PreInitializeFromArgs(
1919
Py_ssize_t argc,
2020
wchar_t **argv);
2121

22-
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
23-
2422

2523
/* Initialization and finalization */
2624

2725
PyAPI_FUNC(PyStatus) Py_InitializeFromConfig(
2826
const PyConfig *config);
27+
28+
// Python 3.8 provisional API (PEP 587)
2929
PyAPI_FUNC(PyStatus) _Py_InitializeMain(void);
3030

3131
PyAPI_FUNC(int) Py_RunMain(void);
3232

3333

3434
PyAPI_FUNC(void) _Py_NO_RETURN Py_ExitStatusException(PyStatus err);
3535

36-
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
37-
PyAPI_FUNC(void) _Py_RestoreSignals(void);
38-
3936
PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
40-
PyAPI_FUNC(int) _Py_FdIsInteractive(FILE *fp, PyObject *filename);
41-
42-
PyAPI_FUNC(const char *) _Py_gitidentifier(void);
43-
PyAPI_FUNC(const char *) _Py_gitversion(void);
44-
45-
PyAPI_FUNC(int) _Py_IsFinalizing(void);
46-
PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp);
47-
48-
/* Random */
49-
PyAPI_FUNC(int) _PyOS_URandom(void *buffer, Py_ssize_t size);
50-
PyAPI_FUNC(int) _PyOS_URandomNonblock(void *buffer, Py_ssize_t size);
51-
52-
/* Legacy locale support */
53-
PyAPI_FUNC(int) _Py_CoerceLegacyLocale(int warn);
54-
PyAPI_FUNC(int) _Py_LegacyLocaleDetected(int warn);
55-
PyAPI_FUNC(char *) _Py_SetLocaleFromEnv(int category);
5637

5738
PyAPI_FUNC(PyStatus) Py_NewInterpreterFromConfig(
5839
PyThreadState **tstate_p,
5940
const PyInterpreterConfig *config);
60-
61-
typedef void (*atexit_datacallbackfunc)(void *);
62-
PyAPI_FUNC(int) _Py_AtExit(
63-
PyInterpreterState *, atexit_datacallbackfunc, void *);

Include/internal/pycore_atexit.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct _atexit_runtime_state {
2525
//###################
2626
// interpreter atexit
2727

28-
struct atexit_callback;
28+
typedef void (*atexit_datacallbackfunc)(void *);
29+
2930
typedef struct atexit_callback {
3031
atexit_datacallbackfunc func;
3132
void *data;
@@ -50,6 +51,10 @@ struct atexit_state {
5051
int callback_len;
5152
};
5253

54+
PyAPI_FUNC(int) _Py_AtExit(
55+
PyInterpreterState *interp,
56+
atexit_datacallbackfunc func,
57+
void *data);
5358

5459
#ifdef __cplusplus
5560
}

Include/internal/pycore_pylifecycle.h

+30-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern PyStatus _PyUnicode_InitEncodings(PyThreadState *tstate);
2323
extern int _PyUnicode_EnableLegacyWindowsFSEncoding(void);
2424
#endif
2525

26-
PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
26+
extern int _Py_IsLocaleCoercionTarget(const char *ctype_loc);
2727

2828
/* Various one-time initializers */
2929

@@ -67,30 +67,49 @@ extern PyStatus _PyGILState_Init(PyInterpreterState *interp);
6767
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
6868
extern void _PyGILState_Fini(PyInterpreterState *interp);
6969

70-
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(PyInterpreterState *interp);
70+
extern void _PyGC_DumpShutdownStats(PyInterpreterState *interp);
7171

72-
PyAPI_FUNC(PyStatus) _Py_PreInitializeFromPyArgv(
72+
extern PyStatus _Py_PreInitializeFromPyArgv(
7373
const PyPreConfig *src_config,
7474
const struct _PyArgv *args);
75-
PyAPI_FUNC(PyStatus) _Py_PreInitializeFromConfig(
75+
extern PyStatus _Py_PreInitializeFromConfig(
7676
const PyConfig *config,
7777
const struct _PyArgv *args);
7878

79-
PyAPI_FUNC(wchar_t *) _Py_GetStdlibDir(void);
79+
extern wchar_t * _Py_GetStdlibDir(void);
8080

81-
PyAPI_FUNC(int) _Py_HandleSystemExit(int *exitcode_p);
81+
extern int _Py_HandleSystemExit(int *exitcode_p);
8282

83-
PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable);
83+
extern PyObject* _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable);
8484

85-
PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate);
86-
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception,
85+
extern void _PyErr_Print(PyThreadState *tstate);
86+
extern void _PyErr_Display(PyObject *file, PyObject *exception,
8787
PyObject *value, PyObject *tb);
88-
PyAPI_FUNC(void) _PyErr_DisplayException(PyObject *file, PyObject *exc);
88+
extern void _PyErr_DisplayException(PyObject *file, PyObject *exc);
8989

90-
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(PyThreadState *tstate);
90+
extern void _PyThreadState_DeleteCurrent(PyThreadState *tstate);
9191

9292
extern void _PyAtExit_Call(PyInterpreterState *interp);
9393

94+
extern int _Py_IsCoreInitialized(void);
95+
96+
extern int _Py_FdIsInteractive(FILE *fp, PyObject *filename);
97+
98+
extern const char* _Py_gitidentifier(void);
99+
extern const char* _Py_gitversion(void);
100+
101+
extern int _Py_IsFinalizing(void);
102+
PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp);
103+
104+
/* Random */
105+
extern int _PyOS_URandom(void *buffer, Py_ssize_t size);
106+
PyAPI_FUNC(int) _PyOS_URandomNonblock(void *buffer, Py_ssize_t size);
107+
108+
/* Legacy locale support */
109+
extern int _Py_CoerceLegacyLocale(int warn);
110+
extern int _Py_LegacyLocaleDetected(int warn);
111+
PyAPI_FUNC(char*) _Py_SetLocaleFromEnv(int category);
112+
94113
#ifdef __cplusplus
95114
}
96115
#endif

Include/internal/pycore_signal.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ extern "C" {
1111
#endif
1212

1313
#include "pycore_atomic.h" // _Py_atomic_address
14-
1514
#include <signal.h> // NSIG
1615

1716

17+
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
18+
PyAPI_FUNC(void) _Py_RestoreSignals(void);
19+
1820
#ifdef _SIG_MAXSIG
1921
// gh-91145: On FreeBSD, <signal.h> defines NSIG as 32: it doesn't include
2022
// realtime signals: [SIGRTMIN,SIGRTMAX]. Use _SIG_MAXSIG instead. For

Modules/_asynciomodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#endif
44

55
#include "Python.h"
6+
#include "pycore_moduleobject.h" // _PyModule_GetState()
67
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
8+
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
79
#include "pycore_pystate.h" // _PyThreadState_GET()
810
#include "pycore_runtime_init.h" // _Py_ID()
9-
#include "pycore_moduleobject.h" // _PyModule_GetState()
1011
#include "structmember.h" // PyMemberDef
1112
#include <stddef.h> // offsetof()
1213

Modules/_io/bufferedio.c

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_call.h" // _PyObject_CallNoArgs()
1212
#include "pycore_object.h"
1313
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
14+
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
1415
#include "structmember.h" // PyMemberDef
1516
#include "_iomodule.h"
1617

Modules/_posixsubprocess.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Python.h"
77
#include "pycore_fileutils.h"
88
#include "pycore_pystate.h"
9+
#include "pycore_signal.h" // _Py_RestoreSignals()
910
#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
1011
# define _GNU_SOURCE
1112
#endif
@@ -739,8 +740,9 @@ child_exec(char *const exec_array[],
739740
if (child_umask >= 0)
740741
umask(child_umask); /* umask() always succeeds. */
741742

742-
if (restore_signals)
743+
if (restore_signals) {
743744
_Py_RestoreSignals();
745+
}
744746

745747
#ifdef VFORK_USABLE
746748
if (child_sigmask) {

Modules/_randommodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
#include "Python.h"
7474
#include "pycore_moduleobject.h" // _PyModule_GetState()
75+
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
7576
#include "pycore_runtime.h"
7677
#ifdef HAVE_PROCESS_H
7778
# include <process.h> // getpid()

Modules/_sqlite/connection.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "prepare_protocol.h"
3535
#include "util.h"
3636
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
37+
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
3738
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
3839

3940
#include <stdbool.h>

Modules/_testcapimodule.c

-32
Original file line numberDiff line numberDiff line change
@@ -3293,37 +3293,6 @@ function_set_kw_defaults(PyObject *self, PyObject *args)
32933293
Py_RETURN_NONE;
32943294
}
32953295

3296-
struct atexit_data {
3297-
int called;
3298-
};
3299-
3300-
static void
3301-
callback(void *data)
3302-
{
3303-
((struct atexit_data *)data)->called += 1;
3304-
}
3305-
3306-
static PyObject *
3307-
test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
3308-
{
3309-
PyThreadState *oldts = PyThreadState_Swap(NULL);
3310-
PyThreadState *tstate = Py_NewInterpreter();
3311-
3312-
struct atexit_data data = {0};
3313-
int res = _Py_AtExit(tstate->interp, callback, (void *)&data);
3314-
Py_EndInterpreter(tstate);
3315-
PyThreadState_Swap(oldts);
3316-
if (res < 0) {
3317-
return NULL;
3318-
}
3319-
if (data.called == 0) {
3320-
PyErr_SetString(PyExc_RuntimeError, "atexit callback not called");
3321-
return NULL;
3322-
}
3323-
Py_RETURN_NONE;
3324-
}
3325-
3326-
33273296
static PyObject *
33283297
check_pyimport_addmodule(PyObject *self, PyObject *args)
33293298
{
@@ -3613,7 +3582,6 @@ static PyMethodDef TestMethods[] = {
36133582
{"function_set_defaults", function_set_defaults, METH_VARARGS, NULL},
36143583
{"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
36153584
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
3616-
{"test_atexit", test_atexit, METH_NOARGS},
36173585
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
36183586
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
36193587
{NULL, NULL} /* sentinel */

Modules/_testinternalcapi.c

+33
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,38 @@ unicode_transformdecimalandspacetoascii(PyObject *self, PyObject *arg)
12641264
}
12651265

12661266

1267+
struct atexit_data {
1268+
int called;
1269+
};
1270+
1271+
static void
1272+
callback(void *data)
1273+
{
1274+
((struct atexit_data *)data)->called += 1;
1275+
}
1276+
1277+
static PyObject *
1278+
test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
1279+
{
1280+
PyThreadState *oldts = PyThreadState_Swap(NULL);
1281+
PyThreadState *tstate = Py_NewInterpreter();
1282+
1283+
struct atexit_data data = {0};
1284+
int res = _Py_AtExit(tstate->interp, callback, (void *)&data);
1285+
Py_EndInterpreter(tstate);
1286+
PyThreadState_Swap(oldts);
1287+
if (res < 0) {
1288+
return NULL;
1289+
}
1290+
1291+
if (data.called == 0) {
1292+
PyErr_SetString(PyExc_RuntimeError, "atexit callback not called");
1293+
return NULL;
1294+
}
1295+
Py_RETURN_NONE;
1296+
}
1297+
1298+
12671299
static PyMethodDef module_functions[] = {
12681300
{"get_configs", get_configs, METH_NOARGS},
12691301
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -1316,6 +1348,7 @@ static PyMethodDef module_functions[] = {
13161348
{"_PyTraceMalloc_GetTraceback", tracemalloc_get_traceback, METH_VARARGS},
13171349
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
13181350
{"_PyUnicode_TransformDecimalAndSpaceToASCII", unicode_transformdecimalandspacetoascii, METH_O},
1351+
{"test_atexit", test_atexit, METH_NOARGS},
13191352
{NULL, NULL} /* sentinel */
13201353
};
13211354

Modules/_xxinterpchannelsmodule.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
21
/* interpreters module */
32
/* low-level access to interpreter primitives */
43

4+
#ifndef Py_BUILD_CORE_BUILTIN
5+
# define Py_BUILD_CORE_MODULE 1
6+
#endif
7+
58
#include "Python.h"
69
#include "interpreteridobject.h"
10+
#include "pycore_atexit.h" // _Py_AtExit()
711

812

913
/*

Modules/getbuildinfo.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
#ifndef Py_BUILD_CORE_BUILTIN
2+
# define Py_BUILD_CORE_MODULE 1
3+
#endif
4+
15
#include "Python.h"
6+
#include "pycore_pylifecycle.h" // _Py_gitidentifier()
27

38
#ifndef DONT_HAVE_STDIO_H
49
#include <stdio.h>

Modules/posixmodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
2020
#include "pycore_moduleobject.h" // _PyModule_GetState()
2121
#include "pycore_object.h" // _PyObject_LookupSpecial()
22+
#include "pycore_pylifecycle.h" // _PyOS_URandom()
2223
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2324
#include "pycore_signal.h" // Py_NSIG
2425

Modules/readline.c

+5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
* recently, it was largely rewritten by Guido van Rossum.
55
*/
66

7+
#ifndef Py_BUILD_CORE_BUILTIN
8+
# define Py_BUILD_CORE_MODULE 1
9+
#endif
10+
711
/* Standard definitions */
812
#include "Python.h"
13+
#include "pycore_pylifecycle.h" // _Py_SetLocaleFromEnv()
914

1015
#include <errno.h>
1116
#include <signal.h>

Modules/signalmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "pycore_moduleobject.h" // _PyModule_GetState()
1414
#include "pycore_pyerrors.h" // _PyErr_SetString()
1515
#include "pycore_pystate.h" // _PyThreadState_GET()
16-
#include "pycore_signal.h" // Py_NSIG
16+
#include "pycore_signal.h" // _Py_RestoreSignals()
1717

1818
#ifndef MS_WINDOWS
1919
# include "posixmodule.h"

Python/_warnings.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "Python.h"
2+
#include "pycore_frame.h"
23
#include "pycore_initconfig.h"
34
#include "pycore_interp.h" // PyInterpreterState.warnings
45
#include "pycore_long.h" // _PyLong_GetZero()
56
#include "pycore_pyerrors.h"
7+
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
68
#include "pycore_pystate.h" // _PyThreadState_GET()
7-
#include "pycore_frame.h"
89
#include "clinic/_warnings.c.h"
910

1011
#define MODULE_NAME "_warnings"

Python/bootstrap_hash.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
2-
#include "pycore_initconfig.h"
32
#include "pycore_fileutils.h" // _Py_fstat_noraise()
3+
#include "pycore_initconfig.h"
4+
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
45
#include "pycore_runtime.h" // _PyRuntime
56

67
#ifdef MS_WINDOWS

Python/preconfig.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pycore_fileutils.h" // DECODE_LOCALE_ERR
33
#include "pycore_getopt.h" // _PyOS_GetOpt()
44
#include "pycore_initconfig.h" // _PyArgv
5+
#include "pycore_pylifecycle.h" // _Py_LegacyLocaleDetected()
56
#include "pycore_pymem.h" // _PyMem_GetAllocatorName()
67
#include "pycore_runtime.h" // _PyRuntime_Initialize()
78

0 commit comments

Comments
 (0)