summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-10-28 16:35:43 (GMT)
committerGitHub <noreply@github.com>2021-10-28 16:35:43 (GMT)
commit4fc68560ea0d506c152a82c48c162bfe002f34a8 (patch)
tree025efe26309526cdb86521c1f8f95ad106ff939f
parent13d9205f4057eeeef80a25d410ad123876dc60cd (diff)
downloadcpython-4fc68560ea0d506c152a82c48c162bfe002f34a8.zip
cpython-4fc68560ea0d506c152a82c48c162bfe002f34a8.tar.gz
cpython-4fc68560ea0d506c152a82c48c162bfe002f34a8.tar.bz2
Store actual ints, not pointers to them in the interpreter state. (GH-29274)
-rw-r--r--Include/internal/pycore_interp.h2
-rw-r--r--Include/internal/pycore_long.h2
-rw-r--r--Include/internal/pycore_pylifecycle.h2
-rw-r--r--Objects/longobject.c21
-rw-r--r--Python/pylifecycle.c4
5 files changed, 10 insertions, 21 deletions
diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index c16f0a4..8bd3dc0 100644
--- a/Include/internal/pycore_interp.h
+++ b/Include/internal/pycore_interp.h
@@ -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;
diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h
index 8bdf8e5..773025b 100644
--- a/Include/internal/pycore_long.h
+++ b/Include/internal/pycore_long.h
@@ -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);
diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h
index 53b9474..5e0f36a 100644
--- a/Include/internal/pycore_pylifecycle.h
+++ b/Include/internal/pycore_pylifecycle.h
@@ -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);
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 5325d18..b7392e5 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -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;
}
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 3ccf32a..7e6060e 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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();