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

Lines changed: 2 additions & 25 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 30 additions & 11 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 32 deletions
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 */

0 commit comments

Comments
 (0)