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:
Eric Snow 2021-12-09 12:59:26 -07:00 committed by GitHub
parent d8a464ef03
commit c8749b5783
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 818 additions and 361 deletions

View file

@ -4,10 +4,11 @@
#include "Python.h"
#include "pycore_bitutils.h" // _Py_popcount32()
#include "pycore_runtime.h" // _PY_NSMALLPOSINTS
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_long.h" // _Py_SmallInts
#include "pycore_object.h" // _PyObject_InitVar()
#include "pycore_pystate.h" // _Py_IsMainInterpreter()
#include "pycore_runtime.h" // _PY_NSMALLPOSINTS
#include <ctype.h>
#include <float.h>
@ -48,7 +49,7 @@ static PyObject *
get_small_int(sdigit ival)
{
assert(IS_SMALL_INT(ival));
PyObject *v = (PyObject *)&_PyRuntime.small_ints[_PY_NSMALLNEGINTS + ival];
PyObject *v = (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + ival];
Py_INCREF(v);
return v;
}
@ -5828,31 +5829,51 @@ PyLong_GetInfo(void)
return int_info;
}
/* runtime lifecycle */
void
_PyLong_Init(PyInterpreterState *interp)
_PyLong_InitGlobalObjects(PyInterpreterState *interp)
{
if (_PyRuntime.small_ints[0].ob_base.ob_base.ob_refcnt == 0) {
for (Py_ssize_t i=0; i < _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS; i++) {
sdigit ival = (sdigit)i - _PY_NSMALLNEGINTS;
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
_PyRuntime.small_ints[i].ob_base.ob_base.ob_refcnt = 1;
_PyRuntime.small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type;
_PyRuntime.small_ints[i].ob_base.ob_size = size;
_PyRuntime.small_ints[i].ob_digit[0] = (digit)abs(ival);
}
if (!_Py_IsMainInterpreter(interp)) {
return;
}
PyLongObject *small_ints = _PyLong_SMALL_INTS;
if (small_ints[0].ob_base.ob_base.ob_refcnt != 0) {
// Py_Initialize() must be running a second time.
return;
}
for (Py_ssize_t i=0; i < _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS; i++) {
sdigit ival = (sdigit)i - _PY_NSMALLNEGINTS;
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
small_ints[i].ob_base.ob_base.ob_refcnt = 1;
small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type;
small_ints[i].ob_base.ob_size = size;
small_ints[i].ob_digit[0] = (digit)abs(ival);
}
}
int
_PyLong_InitTypes(void)
PyStatus
_PyLong_InitTypes(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}
if (PyType_Ready(&PyLong_Type) < 0) {
return _PyStatus_ERR("Can't initialize int type");
}
/* initialize int_info */
if (Int_InfoType.tp_name == NULL) {
if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) {
return -1;
return _PyStatus_ERR("can't init int info type");
}
}
return 0;
return _PyStatus_OK();
}
void