summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-12-09 19:59:26 (GMT)
committerGitHub <noreply@github.com>2021-12-09 19:59:26 (GMT)
commitc8749b578324ad4089c8d014d9136bc42b065343 (patch)
tree8b74af3da8568651c2c2068d9fe544617d70554f /Python/pylifecycle.c
parentd8a464ef0380692975d73a3a1513d901b6af8e65 (diff)
downloadcpython-c8749b578324ad4089c8d014d9136bc42b065343.zip
cpython-c8749b578324ad4089c8d014d9136bc42b065343.tar.gz
cpython-c8749b578324ad4089c8d014d9136bc42b065343.tar.bz2
bpo-46008: Make runtime-global object/type lifecycle functions and state consistent. (gh-29998)
This change is strictly renames and moving code around. It helps in the following ways: * ensures type-related init functions focus strictly on one of the three aspects (state, objects, types) * passes in PyInterpreterState * to all those functions, simplifying work on moving types/objects/state to the interpreter * consistent naming conventions help make what's going on more clear * keeping API related to a type in the corresponding header file makes it more obvious where to look for it https://bugs.python.org/issue46008
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c108
1 files changed, 71 insertions, 37 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 2b386a1..b6d73a9 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2,18 +2,31 @@
#include "Python.h"
+#include "pycore_bytesobject.h" // _PyBytes_InitTypes()
#include "pycore_ceval.h" // _PyEval_FiniGIL()
#include "pycore_context.h" // _PyContext_Init()
+#include "pycore_exceptions.h" // _PyExc_InitTypes()
+#include "pycore_dict.h" // _PyDict_Fini()
#include "pycore_fileutils.h" // _Py_ResetForceASCII()
+#include "pycore_floatobject.h" // _PyFloat_InitTypes()
+#include "pycore_frame.h" // _PyFrame_Fini()
+#include "pycore_genobject.h" // _PyAsyncGen_Fini()
#include "pycore_import.h" // _PyImport_BootstrapImp()
#include "pycore_initconfig.h" // _PyStatus_OK()
+#include "pycore_list.h" // _PyList_Fini()
+#include "pycore_long.h" // _PyLong_InitTypes()
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pylifecycle.h" // _PyErr_Print()
#include "pycore_pystate.h" // _PyThreadState_GET()
+#include "pycore_sliceobject.h" // _PySlice_Fini()
+#include "pycore_structseq.h" // _PyStructSequence_InitState()
#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
+#include "pycore_tuple.h" // _PyTuple_InitTypes()
+#include "pycore_typeobject.h" // _PyTypes_InitTypes()
+#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
#include <locale.h> // setlocale()
#include <stdlib.h> // getenv()
@@ -659,27 +672,27 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
static PyStatus
-pycore_init_singletons(PyInterpreterState *interp)
+pycore_init_global_objects(PyInterpreterState *interp)
{
PyStatus status;
- _PyLong_Init(interp);
+ _PyLong_InitGlobalObjects(interp);
- if (_Py_IsMainInterpreter(interp)) {
- _PyFloat_Init();
- }
+ _PyFloat_InitState(interp);
- status = _PyBytes_Init(interp);
+ status = _PyBytes_InitGlobalObjects(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = _PyUnicode_Init(interp);
+ status = _PyUnicode_InitGlobalObjects(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = _PyTuple_Init(interp);
+ _PyUnicode_InitState(interp);
+
+ status = _PyTuple_InitGlobalObjects(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@@ -692,48 +705,70 @@ static PyStatus
pycore_init_types(PyInterpreterState *interp)
{
PyStatus status;
- int is_main_interp = _Py_IsMainInterpreter(interp);
- if (is_main_interp) {
- if (_PyStructSequence_Init() < 0) {
- return _PyStatus_ERR("can't initialize structseq");
- }
+ status = _PyStructSequence_InitState(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
- status = _PyTypes_Init();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ status = _PyTypes_InitState(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
- if (_PyLong_InitTypes() < 0) {
- return _PyStatus_ERR("can't init int type");
- }
+ status = _PyTypes_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
- status = _PyUnicode_InitTypes();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ status = _PyBytes_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- if (is_main_interp) {
- if (_PyFloat_InitTypes() < 0) {
- return _PyStatus_ERR("can't init float");
- }
+ status = _PyLong_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- status = _PyExc_Init(interp);
+ status = _PyUnicode_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = _PyErr_InitTypes();
+ status = _PyFloat_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- if (is_main_interp) {
- if (!_PyContext_Init()) {
- return _PyStatus_ERR("can't init context");
- }
+ status = _PyTuple_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyExc_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyExc_InitGlobalObjects(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyExc_InitState(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyErr_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyContext_InitTypes(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
return _PyStatus_OK();
@@ -799,7 +834,7 @@ pycore_interp_init(PyThreadState *tstate)
// Create singletons before the first PyType_Ready() call, since
// PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
// and the empty tuple singletons (tp_bases).
- status = pycore_init_singletons(interp);
+ status = pycore_init_global_objects(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@@ -1641,7 +1676,7 @@ finalize_interp_types(PyInterpreterState *interp)
_PyFrame_Fini(interp);
_PyAsyncGen_Fini(interp);
_PyContext_Fini(interp);
- _PyType_Fini(interp);
+ _PyTypes_Fini(interp);
// Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
// a dict internally.
_PyUnicode_ClearInterned(interp);
@@ -1655,7 +1690,6 @@ finalize_interp_types(PyInterpreterState *interp)
_PyBytes_Fini(interp);
_PyUnicode_Fini(interp);
_PyFloat_Fini(interp);
- _PyLong_Fini(interp);
}