mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
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
This commit is contained in:
parent
d8a464ef03
commit
c8749b5783
38 changed files with 818 additions and 361 deletions
|
@ -3,6 +3,7 @@
|
|||
#include "pycore_context.h"
|
||||
#include "pycore_gc.h" // _PyObject_GC_MAY_BE_TRACKED()
|
||||
#include "pycore_hamt.h"
|
||||
#include "pycore_initconfig.h" // _PyStatus_OK()
|
||||
#include "pycore_object.h"
|
||||
#include "pycore_pyerrors.h"
|
||||
#include "pycore_pystate.h" // _PyThreadState_GET()
|
||||
|
@ -1317,15 +1318,20 @@ _PyContext_Fini(PyInterpreterState *interp)
|
|||
struct _Py_context_state *state = &interp->context;
|
||||
state->numfree = -1;
|
||||
#endif
|
||||
_PyHamt_Fini();
|
||||
_PyHamt_Fini(interp);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
_PyContext_Init(void)
|
||||
PyStatus
|
||||
_PyContext_InitTypes(PyInterpreterState *interp)
|
||||
{
|
||||
if (!_PyHamt_Init()) {
|
||||
return 0;
|
||||
if (!_Py_IsMainInterpreter(interp)) {
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
PyStatus status = _PyHamt_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if ((PyType_Ready(&PyContext_Type) < 0) ||
|
||||
|
@ -1333,7 +1339,7 @@ _PyContext_Init(void)
|
|||
(PyType_Ready(&PyContextToken_Type) < 0) ||
|
||||
(PyType_Ready(&PyContextTokenMissing_Type) < 0))
|
||||
{
|
||||
return 0;
|
||||
return _PyStatus_ERR("can't init context types");
|
||||
}
|
||||
|
||||
PyObject *missing = get_token_missing();
|
||||
|
@ -1341,9 +1347,9 @@ _PyContext_Init(void)
|
|||
PyContextToken_Type.tp_dict, "MISSING", missing))
|
||||
{
|
||||
Py_DECREF(missing);
|
||||
return 0;
|
||||
return _PyStatus_ERR("can't init context types");
|
||||
}
|
||||
Py_DECREF(missing);
|
||||
|
||||
return 1;
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
|
|
@ -1241,8 +1241,12 @@ static PyStructSequence_Desc UnraisableHookArgs_desc = {
|
|||
|
||||
|
||||
PyStatus
|
||||
_PyErr_InitTypes(void)
|
||||
_PyErr_InitTypes(PyInterpreterState *interp)
|
||||
{
|
||||
if (!_Py_IsMainInterpreter(interp)) {
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
if (UnraisableHookArgsType.tp_name == NULL) {
|
||||
if (PyStructSequence_InitType2(&UnraisableHookArgsType,
|
||||
&UnraisableHookArgs_desc) < 0) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "pycore_bitutils.h" // _Py_popcount32
|
||||
#include "pycore_hamt.h"
|
||||
#include "pycore_initconfig.h" // _PyStatus_OK()
|
||||
#include "pycore_object.h" // _PyObject_GC_TRACK()
|
||||
#include <stddef.h> // offsetof()
|
||||
|
||||
|
@ -2952,9 +2953,13 @@ PyTypeObject _PyHamt_CollisionNode_Type = {
|
|||
};
|
||||
|
||||
|
||||
int
|
||||
_PyHamt_Init(void)
|
||||
PyStatus
|
||||
_PyHamt_InitTypes(PyInterpreterState *interp)
|
||||
{
|
||||
if (!_Py_IsMainInterpreter(interp)) {
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
if ((PyType_Ready(&_PyHamt_Type) < 0) ||
|
||||
(PyType_Ready(&_PyHamt_ArrayNode_Type) < 0) ||
|
||||
(PyType_Ready(&_PyHamt_BitmapNode_Type) < 0) ||
|
||||
|
@ -2963,14 +2968,14 @@ _PyHamt_Init(void)
|
|||
(PyType_Ready(&_PyHamtValues_Type) < 0) ||
|
||||
(PyType_Ready(&_PyHamtItems_Type) < 0))
|
||||
{
|
||||
return 0;
|
||||
return _PyStatus_ERR("can't init hamt types");
|
||||
}
|
||||
|
||||
return 1;
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
void
|
||||
_PyHamt_Fini(void)
|
||||
_PyHamt_Fini(PyInterpreterState *interp)
|
||||
{
|
||||
Py_CLEAR(_empty_hamt);
|
||||
Py_CLEAR(_empty_bitmap_node);
|
||||
|
|
|
@ -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 = _PyTypes_Init();
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (_PyLong_InitTypes() < 0) {
|
||||
return _PyStatus_ERR("can't init int type");
|
||||
}
|
||||
|
||||
status = _PyUnicode_InitTypes();
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_main_interp) {
|
||||
if (_PyFloat_InitTypes() < 0) {
|
||||
return _PyStatus_ERR("can't init float");
|
||||
}
|
||||
}
|
||||
|
||||
status = _PyExc_Init(interp);
|
||||
status = _PyStructSequence_InitState(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyErr_InitTypes();
|
||||
status = _PyTypes_InitState(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (is_main_interp) {
|
||||
if (!_PyContext_Init()) {
|
||||
return _PyStatus_ERR("can't init context");
|
||||
}
|
||||
status = _PyTypes_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyBytes_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyLong_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyUnicode_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = _PyFloat_InitTypes(interp);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue