]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Store actual ints, not pointers to them in the interpreter state. (GH-29274)
authorMark Shannon <mark@hotpy.org>
Thu, 28 Oct 2021 16:35:43 +0000 (17:35 +0100)
committerGitHub <noreply@github.com>
Thu, 28 Oct 2021 16:35:43 +0000 (17:35 +0100)
Include/internal/pycore_interp.h
Include/internal/pycore_long.h
Include/internal/pycore_pylifecycle.h
Objects/longobject.c
Python/pylifecycle.c

index c16f0a4b5e643e7a9f41eccaa84622b2e09d5c32..8bd3dc064eea31f96fc48df7bbf0a17a44384a58 100644 (file)
@@ -335,7 +335,7 @@ struct _is {
        The integers that are preallocated are those in the range
        -_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_unicode_state unicode;
     struct _Py_float_state float_state;
index 8bdf8e5736d20b8c5612ae9766cade1d1a1151ad..773025b4a5add5443ab4f2b7bb0d95443b90059f 100644 (file)
@@ -17,7 +17,7 @@ static inline PyObject* __PyLong_GetSmallInt_internal(int value)
     PyInterpreterState *interp = _PyInterpreterState_GET();
     assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
     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
     // called before _PyLong_Init() nor after _PyLong_Fini().
     assert(obj != NULL);
index 53b94748b32e9e6f28ff76d6b753c44c0a050298..5e0f36ab2ae494a20bc12243762e0566fd8c7757 100644 (file)
@@ -53,7 +53,7 @@ extern PyStatus _PyUnicode_Init(PyInterpreterState *interp);
 extern PyStatus _PyUnicode_InitTypes(void);
 extern PyStatus _PyBytes_Init(PyInterpreterState *interp);
 extern int _PyStructSequence_Init(void);
-extern int _PyLong_Init(PyInterpreterState *interp);
+extern void _PyLong_Init(PyInterpreterState *interp);
 extern int _PyLong_InitTypes(void);
 extern PyStatus _PyTuple_Init(PyInterpreterState *interp);
 extern PyStatus _PyFaulthandler_Init(int enable);
index 5325d1852bc029fe239c827dbfbbac9668b7d1f3..b7392e50e7f6c2ff37643ce09192110240f72c57 100644 (file)
@@ -5827,24 +5827,17 @@ PyLong_GetInfo(void)
     return int_info;
 }
 
-int
+void
 _PyLong_Init(PyInterpreterState *interp)
 {
     for (Py_ssize_t i=0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
         sdigit ival = (sdigit)i - NSMALLNEGINTS;
         int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
-
-        PyLongObject *v = _PyLong_New(1);
-        if (!v) {
-            return -1;
-        }
-
-        Py_SET_SIZE(v, size);
-        v->ob_digit[0] = (digit)abs(ival);
-
-        interp->small_ints[i] = v;
+        interp->small_ints[i].ob_base.ob_base.ob_refcnt = 1;
+        interp->small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type;
+        interp->small_ints[i].ob_base.ob_size = size;
+        interp->small_ints[i].ob_digit[0] = (digit)abs(ival);
     }
-    return 0;
 }
 
 
@@ -5863,7 +5856,5 @@ _PyLong_InitTypes(void)
 void
 _PyLong_Fini(PyInterpreterState *interp)
 {
-    for (Py_ssize_t i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++) {
-        Py_CLEAR(interp->small_ints[i]);
-    }
+    (void)interp;
 }
index 3ccf32ab1bb4df246f85eaacf1228f0c9f5f184e..7e6060e4ebcf774c2769aee143761d860cb63425 100644 (file)
@@ -659,9 +659,7 @@ pycore_init_singletons(PyInterpreterState *interp)
 {
     PyStatus status;
 
-    if (_PyLong_Init(interp) < 0) {
-        return _PyStatus_ERR("can't init longs");
-    }
+    _PyLong_Init(interp);
 
     if (_Py_IsMainInterpreter(interp)) {
         _PyFloat_Init();