bpo-45953: Statically initialize the small ints. (gh-30092)

The array of small PyLong objects has been statically declared. Here I also statically initialize them. Consequently they are no longer initialized dynamically during runtime init.

I've also moved them under a new sub-struct in _PyRuntimeState, in preparation for static allocation and initialization of other global objects.

https://bugs.python.org/issue45953
This commit is contained in:
Eric Snow 2021-12-13 18:04:05 -07:00 committed by GitHub
parent cb589d1b6b
commit 121f1f893a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 360 additions and 81 deletions

View file

@ -5832,29 +5832,6 @@ PyLong_GetInfo(void)
/* runtime lifecycle */
void
_PyLong_InitGlobalObjects(PyInterpreterState *interp)
{
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);
}
}
PyStatus
_PyLong_InitTypes(PyInterpreterState *interp)
{
@ -5875,9 +5852,3 @@ _PyLong_InitTypes(PyInterpreterState *interp)
return _PyStatus_OK();
}
void
_PyLong_Fini(PyInterpreterState *interp)
{
(void)interp;
}