33/* XXX The functional organization of this file is terrible */
44
55#include "Python.h"
6+ #include "pycore_pystate.h" /* _Py_IsMainInterpreter() */
67#include "longintrepr.h"
78
89#include <float.h>
@@ -15,12 +16,8 @@ class int "PyObject *" "&PyLong_Type"
1516[clinic start generated code]*/
1617/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/
1718
18- #ifndef NSMALLPOSINTS
19- #define NSMALLPOSINTS 257
20- #endif
21- #ifndef NSMALLNEGINTS
22- #define NSMALLNEGINTS 5
23- #endif
19+ #define NSMALLPOSINTS _PY_NSMALLPOSINTS
20+ #define NSMALLNEGINTS _PY_NSMALLNEGINTS
2421
2522_Py_IDENTIFIER (little );
2623_Py_IDENTIFIER (big );
@@ -35,13 +32,6 @@ PyObject *_PyLong_Zero = NULL;
3532PyObject * _PyLong_One = NULL ;
3633
3734#if NSMALLNEGINTS + NSMALLPOSINTS > 0
38- /* Small integers are preallocated in this array so that they
39- can be shared.
40- The integers that are preallocated are those in the range
41- -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
42- */
43- static PyLongObject * small_ints [NSMALLNEGINTS + NSMALLPOSINTS ] = {0 };
44-
4535#define IS_SMALL_INT (ival ) (-NSMALLNEGINTS <= (ival) && (ival) < NSMALLPOSINTS)
4636#define IS_SMALL_UINT (ival ) ((ival) < NSMALLPOSINTS)
4737
@@ -53,7 +43,8 @@ static PyObject *
5343get_small_int (sdigit ival )
5444{
5545 assert (IS_SMALL_INT (ival ));
56- PyObject * v = (PyObject * )small_ints [ival + NSMALLNEGINTS ];
46+ PyThreadState * tstate = _PyThreadState_GET ();
47+ PyObject * v = (PyObject * )tstate -> interp -> small_ints [ival + NSMALLNEGINTS ];
5748 Py_INCREF (v );
5849#ifdef COUNT_ALLOCS
5950 if (ival >= 0 )
@@ -5782,7 +5773,7 @@ PyLong_GetInfo(void)
57825773}
57835774
57845775int
5785- _PyLong_Init (void )
5776+ _PyLong_Init (PyThreadState * tstate )
57865777{
57875778#if NSMALLNEGINTS + NSMALLPOSINTS > 0
57885779 for (Py_ssize_t i = 0 ; i < NSMALLNEGINTS + NSMALLPOSINTS ; i ++ ) {
@@ -5797,37 +5788,43 @@ _PyLong_Init(void)
57975788 Py_SIZE (v ) = size ;
57985789 v -> ob_digit [0 ] = (digit )abs (ival );
57995790
5800- small_ints [i ] = v ;
5791+ tstate -> interp -> small_ints [i ] = v ;
58015792 }
58025793#endif
5803- _PyLong_Zero = PyLong_FromLong (0 );
5804- if (_PyLong_Zero == NULL ) {
5805- return 0 ;
5806- }
58075794
5808- _PyLong_One = PyLong_FromLong (1 );
5809- if (_PyLong_One == NULL ) {
5810- return 0 ;
5811- }
5795+ if (_Py_IsMainInterpreter (tstate )) {
5796+ _PyLong_Zero = PyLong_FromLong (0 );
5797+ if (_PyLong_Zero == NULL ) {
5798+ return 0 ;
5799+ }
58125800
5813- /* initialize int_info */
5814- if (Int_InfoType .tp_name == NULL ) {
5815- if (PyStructSequence_InitType2 (& Int_InfoType , & int_info_desc ) < 0 ) {
5801+ _PyLong_One = PyLong_FromLong (1 );
5802+ if (_PyLong_One == NULL ) {
58165803 return 0 ;
58175804 }
5805+
5806+ /* initialize int_info */
5807+ if (Int_InfoType .tp_name == NULL ) {
5808+ if (PyStructSequence_InitType2 (& Int_InfoType , & int_info_desc ) < 0 ) {
5809+ return 0 ;
5810+ }
5811+ }
58185812 }
58195813
58205814 return 1 ;
58215815}
58225816
58235817void
5824- _PyLong_Fini (void )
5818+ _PyLong_Fini (PyThreadState * tstate )
58255819{
5826- Py_CLEAR (_PyLong_One );
5827- Py_CLEAR (_PyLong_Zero );
5820+ if (_Py_IsMainInterpreter (tstate )) {
5821+ Py_CLEAR (_PyLong_One );
5822+ Py_CLEAR (_PyLong_Zero );
5823+ }
5824+
58285825#if NSMALLNEGINTS + NSMALLPOSINTS > 0
58295826 for (Py_ssize_t i = 0 ; i < NSMALLNEGINTS + NSMALLPOSINTS ; i ++ ) {
5830- Py_CLEAR (small_ints [i ]);
5827+ Py_CLEAR (tstate -> interp -> small_ints [i ]);
58315828 }
58325829#endif
58335830}
0 commit comments