Skip to content

Commit d600951

Browse files
committed
Issue python#22869: Split pythonrun into two modules
- interpreter startup and shutdown code moved to a new pylifecycle.c module - Py_OptimizeFlag moved into the new module with the other global flags
1 parent 66fb349 commit d600951

File tree

16 files changed

+1701
-1587
lines changed

16 files changed

+1701
-1587
lines changed

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#include "pyarena.h"
113113
#include "modsupport.h"
114114
#include "pythonrun.h"
115+
#include "pylifecycle.h"
115116
#include "ceval.h"
116117
#include "sysmodule.h"
117118
#include "intrcheck.h"

Include/object.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ whose size is determined when the object is allocated.
6565
#error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
6666
#endif
6767

68+
6869
#ifdef Py_TRACE_REFS
6970
/* Define pointers to support a doubly-linked list of all live heap objects. */
7071
#define _PyObject_HEAD_EXTRA \
@@ -710,11 +711,17 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
710711
_Py_NegativeRefcount(__FILE__, __LINE__, \
711712
(PyObject *)(OP)); \
712713
}
714+
/* Py_REF_DEBUG also controls the display of refcounts and memory block
715+
* allocations at the interactive prompt and at interpreter shutdown
716+
*/
717+
PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
718+
#define _PY_DEBUG_PRINT_TOTAL_REFS() _PyDebug_PrintTotalRefs()
713719
#else
714720
#define _Py_INC_REFTOTAL
715721
#define _Py_DEC_REFTOTAL
716722
#define _Py_REF_DEBUG_COMMA
717723
#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
724+
#define _PY_DEBUG_PRINT_TOTAL_REFS()
718725
#endif /* Py_REF_DEBUG */
719726

720727
#ifdef COUNT_ALLOCS

Include/pydebug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
extern "C" {
66
#endif
77

8+
/* These global variable are defined in pylifecycle.c */
9+
/* XXX (ncoghlan): move these declarations to pylifecycle.h? */
810
PyAPI_DATA(int) Py_DebugFlag;
911
PyAPI_DATA(int) Py_VerboseFlag;
1012
PyAPI_DATA(int) Py_QuietFlag;

Include/pyerrors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
9999
#define _Py_NO_RETURN
100100
#endif
101101

102+
/* Defined in Python/pylifecycle.c */
102103
PyAPI_FUNC(void) Py_FatalError(const char *message) _Py_NO_RETURN;
103104

104105
#if defined(Py_DEBUG) || defined(Py_LIMITED_API)

Include/pylifecycle.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
/* Interfaces to configure, query, create & destroy the Python runtime */
3+
4+
#ifndef Py_PYLIFECYCLE_H
5+
#define Py_PYLIFECYCLE_H
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
11+
PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
12+
13+
PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *);
14+
PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
15+
16+
#ifndef Py_LIMITED_API
17+
/* Only used by applications that embed the interpreter and need to
18+
* override the standard encoding determination mechanism
19+
*/
20+
PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
21+
const char *errors);
22+
#endif
23+
24+
PyAPI_FUNC(void) Py_Initialize(void);
25+
PyAPI_FUNC(void) Py_InitializeEx(int);
26+
#ifndef Py_LIMITED_API
27+
PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
28+
#endif
29+
PyAPI_FUNC(void) Py_Finalize(void);
30+
PyAPI_FUNC(int) Py_IsInitialized(void);
31+
PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
32+
PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
33+
34+
35+
/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
36+
* exit functions.
37+
*/
38+
#ifndef Py_LIMITED_API
39+
PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(void));
40+
#endif
41+
PyAPI_FUNC(int) Py_AtExit(void (*func)(void));
42+
43+
PyAPI_FUNC(void) Py_Exit(int);
44+
45+
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
46+
#ifndef Py_LIMITED_API
47+
PyAPI_FUNC(void) _Py_RestoreSignals(void);
48+
49+
PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
50+
#endif
51+
52+
/* Bootstrap __main__ (defined in Modules/main.c) */
53+
PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
54+
55+
/* In getpath.c */
56+
PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
57+
PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
58+
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
59+
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
60+
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
61+
#ifdef MS_WINDOWS
62+
int _Py_CheckPython3();
63+
#endif
64+
65+
/* In their own files */
66+
PyAPI_FUNC(const char *) Py_GetVersion(void);
67+
PyAPI_FUNC(const char *) Py_GetPlatform(void);
68+
PyAPI_FUNC(const char *) Py_GetCopyright(void);
69+
PyAPI_FUNC(const char *) Py_GetCompiler(void);
70+
PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
71+
#ifndef Py_LIMITED_API
72+
PyAPI_FUNC(const char *) _Py_hgidentifier(void);
73+
PyAPI_FUNC(const char *) _Py_hgversion(void);
74+
#endif
75+
76+
/* Internal -- various one-time initializations */
77+
#ifndef Py_LIMITED_API
78+
PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
79+
PyAPI_FUNC(PyObject *) _PySys_Init(void);
80+
PyAPI_FUNC(void) _PyImport_Init(void);
81+
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
82+
PyAPI_FUNC(void) _PyImportHooks_Init(void);
83+
PyAPI_FUNC(int) _PyFrame_Init(void);
84+
PyAPI_FUNC(int) _PyFloat_Init(void);
85+
PyAPI_FUNC(int) PyByteArray_Init(void);
86+
PyAPI_FUNC(void) _PyRandom_Init(void);
87+
#endif
88+
89+
/* Various internal finalizers */
90+
#ifndef Py_LIMITED_API
91+
PyAPI_FUNC(void) _PyExc_Fini(void);
92+
PyAPI_FUNC(void) _PyImport_Fini(void);
93+
PyAPI_FUNC(void) PyMethod_Fini(void);
94+
PyAPI_FUNC(void) PyFrame_Fini(void);
95+
PyAPI_FUNC(void) PyCFunction_Fini(void);
96+
PyAPI_FUNC(void) PyDict_Fini(void);
97+
PyAPI_FUNC(void) PyTuple_Fini(void);
98+
PyAPI_FUNC(void) PyList_Fini(void);
99+
PyAPI_FUNC(void) PySet_Fini(void);
100+
PyAPI_FUNC(void) PyBytes_Fini(void);
101+
PyAPI_FUNC(void) PyByteArray_Fini(void);
102+
PyAPI_FUNC(void) PyFloat_Fini(void);
103+
PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
104+
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
105+
PyAPI_FUNC(void) _PyGC_Fini(void);
106+
PyAPI_FUNC(void) PySlice_Fini(void);
107+
PyAPI_FUNC(void) _PyType_Fini(void);
108+
PyAPI_FUNC(void) _PyRandom_Fini(void);
109+
110+
PyAPI_DATA(PyThreadState *) _Py_Finalizing;
111+
#endif
112+
113+
/* Signals */
114+
typedef void (*PyOS_sighandler_t)(int);
115+
PyAPI_FUNC(PyOS_sighandler_t) PyOS_getsig(int);
116+
PyAPI_FUNC(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t);
117+
118+
/* Random */
119+
PyAPI_FUNC(int) _PyOS_URandom (void *buffer, Py_ssize_t size);
120+
121+
#ifdef __cplusplus
122+
}
123+
#endif
124+
#endif /* !Py_PYLIFECYCLE_H */

Include/pythonrun.h

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,6 @@ typedef struct {
2222
} PyCompilerFlags;
2323
#endif
2424

25-
PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
26-
PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
27-
28-
PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *);
29-
PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
30-
31-
#ifndef Py_LIMITED_API
32-
/* Only used by applications that embed the interpreter and need to
33-
* override the standard encoding determination mechanism
34-
*/
35-
PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
36-
const char *errors);
37-
#endif
38-
39-
PyAPI_FUNC(void) Py_Initialize(void);
40-
PyAPI_FUNC(void) Py_InitializeEx(int);
41-
#ifndef Py_LIMITED_API
42-
PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
43-
#endif
44-
PyAPI_FUNC(void) Py_Finalize(void);
45-
PyAPI_FUNC(int) Py_IsInitialized(void);
46-
PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
47-
PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
48-
4925
#ifndef Py_LIMITED_API
5026
PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
5127
PyAPI_FUNC(int) PyRun_AnyFileFlags(FILE *, const char *, PyCompilerFlags *);
@@ -166,26 +142,6 @@ PyAPI_FUNC(void) PyErr_Print(void);
166142
PyAPI_FUNC(void) PyErr_PrintEx(int);
167143
PyAPI_FUNC(void) PyErr_Display(PyObject *, PyObject *, PyObject *);
168144

169-
/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
170-
* exit functions.
171-
*/
172-
#ifndef Py_LIMITED_API
173-
PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(void));
174-
#endif
175-
PyAPI_FUNC(int) Py_AtExit(void (*func)(void));
176-
177-
PyAPI_FUNC(void) Py_Exit(int);
178-
179-
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
180-
#ifndef Py_LIMITED_API
181-
PyAPI_FUNC(void) _Py_RestoreSignals(void);
182-
183-
PyAPI_FUNC(int) Py_FdIsInteractive(FILE *, const char *);
184-
#endif
185-
186-
/* Bootstrap */
187-
PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
188-
189145
#ifndef Py_LIMITED_API
190146
/* Use macros for a bunch of old variants */
191147
#define PyRun_String(str, s, g, l) PyRun_StringFlags(str, s, g, l, NULL)
@@ -207,64 +163,6 @@ PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
207163
PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
208164
#endif
209165

210-
/* In getpath.c */
211-
PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
212-
PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
213-
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
214-
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
215-
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
216-
#ifdef MS_WINDOWS
217-
int _Py_CheckPython3();
218-
#endif
219-
220-
/* In their own files */
221-
PyAPI_FUNC(const char *) Py_GetVersion(void);
222-
PyAPI_FUNC(const char *) Py_GetPlatform(void);
223-
PyAPI_FUNC(const char *) Py_GetCopyright(void);
224-
PyAPI_FUNC(const char *) Py_GetCompiler(void);
225-
PyAPI_FUNC(const char *) Py_GetBuildInfo(void);
226-
#ifndef Py_LIMITED_API
227-
PyAPI_FUNC(const char *) _Py_hgidentifier(void);
228-
PyAPI_FUNC(const char *) _Py_hgversion(void);
229-
#endif
230-
231-
/* Internal -- various one-time initializations */
232-
#ifndef Py_LIMITED_API
233-
PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
234-
PyAPI_FUNC(PyObject *) _PySys_Init(void);
235-
PyAPI_FUNC(void) _PyImport_Init(void);
236-
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
237-
PyAPI_FUNC(void) _PyImportHooks_Init(void);
238-
PyAPI_FUNC(int) _PyFrame_Init(void);
239-
PyAPI_FUNC(int) _PyFloat_Init(void);
240-
PyAPI_FUNC(int) PyByteArray_Init(void);
241-
PyAPI_FUNC(void) _PyRandom_Init(void);
242-
#endif
243-
244-
/* Various internal finalizers */
245-
#ifndef Py_LIMITED_API
246-
PyAPI_FUNC(void) _PyExc_Fini(void);
247-
PyAPI_FUNC(void) _PyImport_Fini(void);
248-
PyAPI_FUNC(void) PyMethod_Fini(void);
249-
PyAPI_FUNC(void) PyFrame_Fini(void);
250-
PyAPI_FUNC(void) PyCFunction_Fini(void);
251-
PyAPI_FUNC(void) PyDict_Fini(void);
252-
PyAPI_FUNC(void) PyTuple_Fini(void);
253-
PyAPI_FUNC(void) PyList_Fini(void);
254-
PyAPI_FUNC(void) PySet_Fini(void);
255-
PyAPI_FUNC(void) PyBytes_Fini(void);
256-
PyAPI_FUNC(void) PyByteArray_Fini(void);
257-
PyAPI_FUNC(void) PyFloat_Fini(void);
258-
PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
259-
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
260-
PyAPI_FUNC(void) _PyGC_Fini(void);
261-
PyAPI_FUNC(void) PySlice_Fini(void);
262-
PyAPI_FUNC(void) _PyType_Fini(void);
263-
PyAPI_FUNC(void) _PyRandom_Fini(void);
264-
265-
PyAPI_DATA(PyThreadState *) _Py_Finalizing;
266-
#endif
267-
268166
/* Stuff with no proper home (yet) */
269167
#ifndef Py_LIMITED_API
270168
PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, const char *);
@@ -290,14 +188,6 @@ PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;
290188
PyAPI_FUNC(int) PyOS_CheckStack(void);
291189
#endif
292190

293-
/* Signals */
294-
typedef void (*PyOS_sighandler_t)(int);
295-
PyAPI_FUNC(PyOS_sighandler_t) PyOS_getsig(int);
296-
PyAPI_FUNC(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t);
297-
298-
/* Random */
299-
PyAPI_FUNC(int) _PyOS_URandom (void *buffer, Py_ssize_t size);
300-
301191
#ifdef __cplusplus
302192
}
303193
#endif

Lib/_pyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class OpenWrapper:
257257
Trick so that open won't become a bound method when stored
258258
as a class variable (as dbm.dumb does).
259259
260-
See initstdio() in Python/pythonrun.c.
260+
See initstdio() in Python/pylifecycle.c.
261261
"""
262262
__doc__ = DocDescriptor()
263263

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ PYTHON_OBJS= \
389389
Python/pyctype.o \
390390
Python/pyfpe.o \
391391
Python/pyhash.o \
392+
Python/pylifecycle.o \
392393
Python/pymath.o \
393394
Python/pystate.o \
394395
Python/pythonrun.o \
@@ -909,6 +910,7 @@ PYTHON_HEADERS= \
909910
$(srcdir)/Include/pyerrors.h \
910911
$(srcdir)/Include/pyfpe.h \
911912
$(srcdir)/Include/pyhash.h \
913+
$(srcdir)/Include/pylifecycle.h \
912914
$(srcdir)/Include/pymath.h \
913915
$(srcdir)/Include/pygetopt.h \
914916
$(srcdir)/Include/pymacro.h \

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #22869: Move the interpreter startup & shutdown code to a new
14+
dedicated pylifecycle.c module
15+
1316
- Issue #22847: Improve method cache efficiency.
1417

1518
- Issue #22335: Fix crash when trying to enlarge a bytearray to 0x7fffffff

Modules/atexitmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ atexit_cleanup(atexitmodule_state *modstate)
6060
modstate->ncallbacks = 0;
6161
}
6262

63-
/* Installed into pythonrun.c's atexit mechanism */
63+
/* Installed into pylifecycle.c's atexit mechanism */
6464

6565
static void
6666
atexit_callfuncs(void)

0 commit comments

Comments
 (0)