mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Store actual ints, not pointers to them in the interpreter state. (GH-29274)
This commit is contained in:
parent
13d9205f40
commit
4fc68560ea
5 changed files with 10 additions and 21 deletions
|
@ -335,7 +335,7 @@ struct _is {
|
||||||
The integers that are preallocated are those in the range
|
The integers that are preallocated are those in the range
|
||||||
-_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive).
|
-_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive).
|
||||||
*/
|
*/
|
||||||
PyLongObject* small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
|
PyLongObject small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
|
||||||
struct _Py_bytes_state bytes;
|
struct _Py_bytes_state bytes;
|
||||||
struct _Py_unicode_state unicode;
|
struct _Py_unicode_state unicode;
|
||||||
struct _Py_float_state float_state;
|
struct _Py_float_state float_state;
|
||||||
|
|
|
@ -17,7 +17,7 @@ static inline PyObject* __PyLong_GetSmallInt_internal(int value)
|
||||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||||
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
|
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
|
||||||
size_t index = _PY_NSMALLNEGINTS + value;
|
size_t index = _PY_NSMALLNEGINTS + value;
|
||||||
PyObject *obj = (PyObject*)interp->small_ints[index];
|
PyObject *obj = (PyObject*)&interp->small_ints[index];
|
||||||
// _PyLong_GetZero(), _PyLong_GetOne() and get_small_int() must not be
|
// _PyLong_GetZero(), _PyLong_GetOne() and get_small_int() must not be
|
||||||
// called before _PyLong_Init() nor after _PyLong_Fini().
|
// called before _PyLong_Init() nor after _PyLong_Fini().
|
||||||
assert(obj != NULL);
|
assert(obj != NULL);
|
||||||
|
|
|
@ -53,7 +53,7 @@ extern PyStatus _PyUnicode_Init(PyInterpreterState *interp);
|
||||||
extern PyStatus _PyUnicode_InitTypes(void);
|
extern PyStatus _PyUnicode_InitTypes(void);
|
||||||
extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
|
extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
|
||||||
extern int _PyStructSequence_Init(void);
|
extern int _PyStructSequence_Init(void);
|
||||||
extern int _PyLong_Init(PyInterpreterState *interp);
|
extern void _PyLong_Init(PyInterpreterState *interp);
|
||||||
extern int _PyLong_InitTypes(void);
|
extern int _PyLong_InitTypes(void);
|
||||||
extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
|
extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
|
||||||
extern PyStatus _PyFaulthandler_Init(int enable);
|
extern PyStatus _PyFaulthandler_Init(int enable);
|
||||||
|
|
|
@ -5827,24 +5827,17 @@ PyLong_GetInfo(void)
|
||||||
return int_info;
|
return int_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
_PyLong_Init(PyInterpreterState *interp)
|
_PyLong_Init(PyInterpreterState *interp)
|
||||||
{
|
{
|
||||||
for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
|
for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
|
||||||
sdigit ival = (sdigit)i - NSMALLNEGINTS;
|
sdigit ival = (sdigit)i - NSMALLNEGINTS;
|
||||||
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
|
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
|
||||||
|
interp->small_ints[i].ob_base.ob_base.ob_refcnt = 1;
|
||||||
PyLongObject *v = _PyLong_New(1);
|
interp->small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type;
|
||||||
if (!v) {
|
interp->small_ints[i].ob_base.ob_size = size;
|
||||||
return -1;
|
interp->small_ints[i].ob_digit[0] = (digit)abs(ival);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_SET_SIZE(v, size);
|
|
||||||
v->ob_digit[0] = (digit)abs(ival);
|
|
||||||
|
|
||||||
interp->small_ints[i] = v;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -5863,7 +5856,5 @@ _PyLong_InitTypes(void)
|
||||||
void
|
void
|
||||||
_PyLong_Fini(PyInterpreterState *interp)
|
_PyLong_Fini(PyInterpreterState *interp)
|
||||||
{
|
{
|
||||||
for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
|
(void)interp;
|
||||||
Py_CLEAR(interp->small_ints[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -659,9 +659,7 @@ pycore_init_singletons(PyInterpreterState *interp)
|
||||||
{
|
{
|
||||||
PyStatus status;
|
PyStatus status;
|
||||||
|
|
||||||
if (_PyLong_Init(interp) < 0) {
|
_PyLong_Init(interp);
|
||||||
return _PyStatus_ERR("can't init longs");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_Py_IsMainInterpreter(interp)) {
|
if (_Py_IsMainInterpreter(interp)) {
|
||||||
_PyFloat_Init();
|
_PyFloat_Init();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue