diff options
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index ce4f0d72540..f6d5e7648be 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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 |