Skip to content

Commit 4fc6856

Browse files
authored
Store actual ints, not pointers to them in the interpreter state. (GH-29274)
1 parent 13d9205 commit 4fc6856

File tree

5 files changed

+10
-21
lines changed

5 files changed

+10
-21
lines changed

Include/internal/pycore_interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ struct _is {
335335
The integers that are preallocated are those in the range
336336
-_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive).
337337
*/
338-
PyLongObject* small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
338+
PyLongObject small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
339339
struct _Py_bytes_state bytes;
340340
struct _Py_unicode_state unicode;
341341
struct _Py_float_state float_state;

Include/internal/pycore_long.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static inline PyObject* __PyLong_GetSmallInt_internal(int value)
1717
PyInterpreterState *interp = _PyInterpreterState_GET();
1818
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
1919
size_t index = _PY_NSMALLNEGINTS + value;
20-
PyObject *obj = (PyObject*)interp->small_ints[index];
20+
PyObject *obj = (PyObject*)&interp->small_ints[index];
2121
// _PyLong_GetZero(), _PyLong_GetOne() and get_small_int() must not be
2222
// called before _PyLong_Init() nor after _PyLong_Fini().
2323
assert(obj != NULL);

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extern PyStatus _PyUnicode_Init(PyInterpreterState *interp);
5353
extern PyStatus _PyUnicode_InitTypes(void);
5454
extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
5555
extern int _PyStructSequence_Init(void);
56-
extern int _PyLong_Init(PyInterpreterState *interp);
56+
extern void _PyLong_Init(PyInterpreterState *interp);
5757
extern int _PyLong_InitTypes(void);
5858
extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
5959
extern PyStatus _PyFaulthandler_Init(int enable);

Objects/longobject.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5827,24 +5827,17 @@ PyLong_GetInfo(void)
58275827
return int_info;
58285828
}
58295829

5830-
int
5830+
void
58315831
_PyLong_Init(PyInterpreterState *interp)
58325832
{
58335833
for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
58345834
sdigit ival = (sdigit)i - NSMALLNEGINTS;
58355835
int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
5836-
5837-
PyLongObject *v = _PyLong_New(1);
5838-
if (!v) {
5839-
return -1;
5840-
}
5841-
5842-
Py_SET_SIZE(v, size);
5843-
v->ob_digit[0] = (digit)abs(ival);
5844-
5845-
interp->small_ints[i] = v;
5836+
interp->small_ints[i].ob_base.ob_base.ob_refcnt = 1;
5837+
interp->small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type;
5838+
interp->small_ints[i].ob_base.ob_size = size;
5839+
interp->small_ints[i].ob_digit[0] = (digit)abs(ival);
58465840
}
5847-
return 0;
58485841
}
58495842

58505843

@@ -5863,7 +5856,5 @@ _PyLong_InitTypes(void)
58635856
void
58645857
_PyLong_Fini(PyInterpreterState *interp)
58655858
{
5866-
for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
5867-
Py_CLEAR(interp->small_ints[i]);
5868-
}
5859+
(void)interp;
58695860
}

Python/pylifecycle.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,7 @@ pycore_init_singletons(PyInterpreterState *interp)
659659
{
660660
PyStatus status;
661661

662-
if (_PyLong_Init(interp) < 0) {
663-
return _PyStatus_ERR("can't init longs");
664-
}
662+
_PyLong_Init(interp);
665663

666664
if (_Py_IsMainInterpreter(interp)) {
667665
_PyFloat_Init();

0 commit comments

Comments
 (0)