From 0d9794ad7c888bb5bb5c694401717b1d58b1191d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 8 Dec 2021 14:02:50 -0700 Subject: [PATCH 01/12] Add struct _Py_global_objects. --- Include/internal/pycore_global_objects.h | 28 ++++++++++++++++++++++++ Include/internal/pycore_runtime.h | 13 ++++++----- Makefile.pre.in | 1 + PCbuild/pythoncore.vcxproj | 1 + PCbuild/pythoncore.vcxproj.filters | 3 +++ 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 Include/internal/pycore_global_objects.h diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h new file mode 100644 index 00000000000000..34064ffa760453 --- /dev/null +++ b/Include/internal/pycore_global_objects.h @@ -0,0 +1,28 @@ +#ifndef Py_INTERNAL_GLOBAL_OBJECTS_H +#define Py_INTERNAL_GLOBAL_OBJECTS_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + + +// Only immutable objects should be considered runtime-global. +// All others must be per-interpreter. + +#define _Py_GLOBAL_OBJECT(NAME) \ + _PyRuntime.global_objects.NAME + +struct _Py_global_objects { +}; + + +#define _Py_global_objects_INIT { \ +} + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_GLOBAL_OBJECTS_H */ diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index bd88510d1f0562..104958ad735912 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -8,10 +8,11 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "pycore_atomic.h" /* _Py_atomic_address */ -#include "pycore_gil.h" // struct _gil_runtime_state -#include "pycore_long_state.h" // struct _Py_long_state -#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids +#include "pycore_atomic.h" /* _Py_atomic_address */ +#include "pycore_gil.h" // struct _gil_runtime_state +#include "pycore_global_objects.h" // struct _Py_global_objects +#include "pycore_long_state.h" // struct _Py_long_state +#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids /* ceval state */ @@ -103,6 +104,8 @@ typedef struct pyruntimestate { struct _Py_long_state int_state; + struct _Py_global_objects global_objects; + #define NEXITFUNCS 32 void (*exitfuncs[NEXITFUNCS])(void); int nexitfuncs; @@ -125,7 +128,7 @@ typedef struct pyruntimestate { #define _PyRuntimeState_INIT \ { \ - ._initialized = 0, \ + .global_objects = _Py_global_objects_INIT, \ } /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ diff --git a/Makefile.pre.in b/Makefile.pre.in index 57928eead4384b..1645923f482b0f 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -1593,6 +1593,7 @@ PYTHON_HEADERS= \ $(srcdir)/Include/internal/pycore_genobject.h \ $(srcdir)/Include/internal/pycore_getopt.h \ $(srcdir)/Include/internal/pycore_gil.h \ + $(srcdir)/Include/internal/pycore_global_objects.h \ $(srcdir)/Include/internal/pycore_hamt.h \ $(srcdir)/Include/internal/pycore_hashtable.h \ $(srcdir)/Include/internal/pycore_import.h \ diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index 33abfaf53652f9..30d92f51cefad0 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -213,6 +213,7 @@ + diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index c99595755e3dc1..3f29cc704488a8 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -549,6 +549,9 @@ Include\internal + + Include\internal + Include\internal From 3b0abc91f5054c5f909aaf2c0a060a6f1f9d99cc Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 10 Dec 2021 14:00:32 -0700 Subject: [PATCH 02/12] Add _Py_global_objects.singletons. --- Include/internal/pycore_global_objects.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 34064ffa760453..c51d70209db238 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -14,12 +14,17 @@ extern "C" { #define _Py_GLOBAL_OBJECT(NAME) \ _PyRuntime.global_objects.NAME +#define _Py_SINGLETON(NAME) \ + _Py_GLOBAL_OBJECT(singletons.NAME) struct _Py_global_objects { + struct { + } singletons; }; - #define _Py_global_objects_INIT { \ + .singletons = { \ + }, \ } #ifdef __cplusplus From c5e57021c685e26a327b86a85b084c977a71014e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 15:19:22 -0700 Subject: [PATCH 03/12] Add _Py_global_objects_reset(). --- Include/internal/pycore_global_objects.h | 5 +++++ Include/internal/pycore_runtime.h | 1 + 2 files changed, 6 insertions(+) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index c51d70209db238..6cc50080497cd7 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -27,6 +27,11 @@ struct _Py_global_objects { }, \ } +static inline void +_Py_global_objects_reset(struct _Py_global_objects *objects) +{ +} + #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index 104958ad735912..20fb73ab1c1e4d 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -137,6 +137,7 @@ _PyRuntimeState_reset(_PyRuntimeState *runtime) { /* Make it match _PyRuntimeState_INIT. */ memset(runtime, 0, sizeof(*runtime)); + _Py_global_objects_reset(&runtime->global_objects); } From 1432a354e020f45b3dd23d2a5f8ccd9d374eba1d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 8 Dec 2021 14:05:34 -0700 Subject: [PATCH 04/12] Move the small ints to _Py_global_objects. --- Include/internal/pycore_global_objects.h | 15 +++++++++++ Include/internal/pycore_long.h | 4 ++- Include/internal/pycore_long_state.h | 33 ------------------------ Include/internal/pycore_runtime.h | 7 +---- Makefile.pre.in | 1 - PCbuild/pythoncore.vcxproj | 1 - PCbuild/pythoncore.vcxproj.filters | 3 --- 7 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 Include/internal/pycore_long_state.h diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 6cc50080497cd7..0bf84ee26866ee 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -9,6 +9,15 @@ extern "C" { #endif +#define _PY_NSMALLPOSINTS 257 +#define _PY_NSMALLNEGINTS 5 + +// _PyLong_GetZero() and _PyLong_GetOne() must always be available +#if _PY_NSMALLPOSINTS < 2 +# error "_PY_NSMALLPOSINTS must be greater than 1" +#endif + + // Only immutable objects should be considered runtime-global. // All others must be per-interpreter. @@ -19,6 +28,12 @@ extern "C" { struct _Py_global_objects { struct { + /* Small integers are preallocated in this array so that they + * can be shared. + * 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]; } singletons; }; diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index a5639ceb6924a8..36ce0ea45e4991 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -8,7 +8,7 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "pycore_long_state.h" // _PyLong_SMALL_INTS +#include "pycore_global_objects.h" // _PY_NSMALLNEGINTS #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime.h" // _PyRuntime @@ -21,6 +21,8 @@ extern PyStatus _PyLong_InitTypes(PyInterpreterState *); /* other API */ +#define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints) + // Return a borrowed reference to the zero singleton. // The function cannot return NULL. static inline PyObject* _PyLong_GetZero(void) diff --git a/Include/internal/pycore_long_state.h b/Include/internal/pycore_long_state.h deleted file mode 100644 index 5fe8e623a9effe..00000000000000 --- a/Include/internal/pycore_long_state.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef Py_INTERNAL_LONG_STATE_H -#define Py_INTERNAL_LONG_STATE_H -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef Py_BUILD_CORE -# error "this header requires Py_BUILD_CORE define" -#endif - -#define _PY_NSMALLPOSINTS 257 -#define _PY_NSMALLNEGINTS 5 - -// _PyLong_GetZero() and _PyLong_GetOne() must always be available -#if _PY_NSMALLPOSINTS < 2 -# error "_PY_NSMALLPOSINTS must be greater than 1" -#endif - -struct _Py_long_state { - /* Small integers are preallocated in this array so that they - * can be shared. - * 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]; -}; - -#define _PyLong_SMALL_INTS _PyRuntime.int_state.small_ints - -#ifdef __cplusplus -} -#endif -#endif /* !Py_INTERNAL_LONG_STATE_H */ diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index 20fb73ab1c1e4d..cb8b02b7b11891 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -11,7 +11,6 @@ extern "C" { #include "pycore_atomic.h" /* _Py_atomic_address */ #include "pycore_gil.h" // struct _gil_runtime_state #include "pycore_global_objects.h" // struct _Py_global_objects -#include "pycore_long_state.h" // struct _Py_long_state #include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids /* ceval state */ @@ -102,10 +101,6 @@ typedef struct pyruntimestate { unsigned long main_thread; - struct _Py_long_state int_state; - - struct _Py_global_objects global_objects; - #define NEXITFUNCS 32 void (*exitfuncs[NEXITFUNCS])(void); int nexitfuncs; @@ -123,7 +118,7 @@ typedef struct pyruntimestate { struct _Py_unicode_runtime_ids unicode_ids; - // XXX Consolidate globals found via the check-c-globals script. + struct _Py_global_objects global_objects; } _PyRuntimeState; #define _PyRuntimeState_INIT \ diff --git a/Makefile.pre.in b/Makefile.pre.in index 1645923f482b0f..e024363592d98b 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -1602,7 +1602,6 @@ PYTHON_HEADERS= \ $(srcdir)/Include/internal/pycore_interpreteridobject.h \ $(srcdir)/Include/internal/pycore_list.h \ $(srcdir)/Include/internal/pycore_long.h \ - $(srcdir)/Include/internal/pycore_long_state.h \ $(srcdir)/Include/internal/pycore_moduleobject.h \ $(srcdir)/Include/internal/pycore_namespace.h \ $(srcdir)/Include/internal/pycore_object.h \ diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index 30d92f51cefad0..97e9bfd21326f4 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -222,7 +222,6 @@ - diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index 3f29cc704488a8..cc0a2b0f75924d 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -576,9 +576,6 @@ Include\internal - - Include\internal - Include\internal From e7d41f2ec5e8f2a6d4288403f6ae197678019221 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 12:40:32 -0700 Subject: [PATCH 05/12] Drop an unneeded include. --- Include/internal/pycore_long.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index 36ce0ea45e4991..58e00baee78108 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -9,7 +9,6 @@ extern "C" { #endif #include "pycore_global_objects.h" // _PY_NSMALLNEGINTS -#include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime.h" // _PyRuntime From fca595b4374738b277a9cd19c398254112b74da7 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 12:42:31 -0700 Subject: [PATCH 06/12] Move a build-time check to the right file. --- Include/internal/pycore_global_objects.h | 4 ---- Include/internal/pycore_long.h | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 0bf84ee26866ee..88cdc92e1ac029 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -12,10 +12,6 @@ extern "C" { #define _PY_NSMALLPOSINTS 257 #define _PY_NSMALLNEGINTS 5 -// _PyLong_GetZero() and _PyLong_GetOne() must always be available -#if _PY_NSMALLPOSINTS < 2 -# error "_PY_NSMALLPOSINTS must be greater than 1" -#endif // Only immutable objects should be considered runtime-global. diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index 58e00baee78108..299988d6b385b7 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -22,6 +22,11 @@ extern PyStatus _PyLong_InitTypes(PyInterpreterState *); #define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints) +// _PyLong_GetZero() and _PyLong_GetOne() must always be available +#if _PY_NSMALLPOSINTS < 2 +# error "_PY_NSMALLPOSINTS must be greater than 1" +#endif + // Return a borrowed reference to the zero singleton. // The function cannot return NULL. static inline PyObject* _PyLong_GetZero(void) From 8f10ad04f82df485d5095bd6d8ad32eb453d44a5 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 13:31:06 -0700 Subject: [PATCH 07/12] Statically initialize the small ints. --- Include/internal/pycore_global_objects.h | 270 +++++++++++++++++++++++ Include/internal/pycore_long.h | 1 - Objects/longobject.c | 29 --- Python/pylifecycle.c | 2 - 4 files changed, 270 insertions(+), 32 deletions(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 88cdc92e1ac029..d655f882af6b40 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -12,6 +12,12 @@ extern "C" { #define _PY_NSMALLPOSINTS 257 #define _PY_NSMALLNEGINTS 5 +#define _PyLong_NEG_INIT(val) \ + { PyVarObject_HEAD_INIT(&PyLong_Type, -1) .ob_digit = { -(val) }, } +#define _PyLong_ZERO_INIT \ + { PyVarObject_HEAD_INIT(&PyLong_Type, 0) .ob_digit = { 0 }, } +#define _PyLong_POS_INIT(val) \ + { PyVarObject_HEAD_INIT(&PyLong_Type, 1) .ob_digit = { val }, } // Only immutable objects should be considered runtime-global. @@ -35,6 +41,270 @@ struct _Py_global_objects { #define _Py_global_objects_INIT { \ .singletons = { \ + .small_ints = { \ + _PyLong_NEG_INIT(-5), \ + _PyLong_NEG_INIT(-4), \ + _PyLong_NEG_INIT(-3), \ + _PyLong_NEG_INIT(-2), \ + _PyLong_NEG_INIT(-1), \ + _PyLong_ZERO_INIT, \ + _PyLong_POS_INIT(1), \ + _PyLong_POS_INIT(2), \ + _PyLong_POS_INIT(3), \ + _PyLong_POS_INIT(4), \ + _PyLong_POS_INIT(5), \ + _PyLong_POS_INIT(6), \ + _PyLong_POS_INIT(7), \ + _PyLong_POS_INIT(8), \ + _PyLong_POS_INIT(9), \ + _PyLong_POS_INIT(10), \ + _PyLong_POS_INIT(11), \ + _PyLong_POS_INIT(12), \ + _PyLong_POS_INIT(13), \ + _PyLong_POS_INIT(14), \ + _PyLong_POS_INIT(15), \ + _PyLong_POS_INIT(16), \ + _PyLong_POS_INIT(17), \ + _PyLong_POS_INIT(18), \ + _PyLong_POS_INIT(19), \ + _PyLong_POS_INIT(20), \ + _PyLong_POS_INIT(21), \ + _PyLong_POS_INIT(22), \ + _PyLong_POS_INIT(23), \ + _PyLong_POS_INIT(24), \ + _PyLong_POS_INIT(25), \ + _PyLong_POS_INIT(26), \ + _PyLong_POS_INIT(27), \ + _PyLong_POS_INIT(28), \ + _PyLong_POS_INIT(29), \ + _PyLong_POS_INIT(30), \ + _PyLong_POS_INIT(31), \ + _PyLong_POS_INIT(32), \ + _PyLong_POS_INIT(33), \ + _PyLong_POS_INIT(34), \ + _PyLong_POS_INIT(35), \ + _PyLong_POS_INIT(36), \ + _PyLong_POS_INIT(37), \ + _PyLong_POS_INIT(38), \ + _PyLong_POS_INIT(39), \ + _PyLong_POS_INIT(40), \ + _PyLong_POS_INIT(41), \ + _PyLong_POS_INIT(42), \ + _PyLong_POS_INIT(43), \ + _PyLong_POS_INIT(44), \ + _PyLong_POS_INIT(45), \ + _PyLong_POS_INIT(46), \ + _PyLong_POS_INIT(47), \ + _PyLong_POS_INIT(48), \ + _PyLong_POS_INIT(49), \ + _PyLong_POS_INIT(50), \ + _PyLong_POS_INIT(51), \ + _PyLong_POS_INIT(52), \ + _PyLong_POS_INIT(53), \ + _PyLong_POS_INIT(54), \ + _PyLong_POS_INIT(55), \ + _PyLong_POS_INIT(56), \ + _PyLong_POS_INIT(57), \ + _PyLong_POS_INIT(58), \ + _PyLong_POS_INIT(59), \ + _PyLong_POS_INIT(60), \ + _PyLong_POS_INIT(61), \ + _PyLong_POS_INIT(62), \ + _PyLong_POS_INIT(63), \ + _PyLong_POS_INIT(64), \ + _PyLong_POS_INIT(65), \ + _PyLong_POS_INIT(66), \ + _PyLong_POS_INIT(67), \ + _PyLong_POS_INIT(68), \ + _PyLong_POS_INIT(69), \ + _PyLong_POS_INIT(70), \ + _PyLong_POS_INIT(71), \ + _PyLong_POS_INIT(72), \ + _PyLong_POS_INIT(73), \ + _PyLong_POS_INIT(74), \ + _PyLong_POS_INIT(75), \ + _PyLong_POS_INIT(76), \ + _PyLong_POS_INIT(77), \ + _PyLong_POS_INIT(78), \ + _PyLong_POS_INIT(79), \ + _PyLong_POS_INIT(80), \ + _PyLong_POS_INIT(81), \ + _PyLong_POS_INIT(82), \ + _PyLong_POS_INIT(83), \ + _PyLong_POS_INIT(84), \ + _PyLong_POS_INIT(85), \ + _PyLong_POS_INIT(86), \ + _PyLong_POS_INIT(87), \ + _PyLong_POS_INIT(88), \ + _PyLong_POS_INIT(89), \ + _PyLong_POS_INIT(90), \ + _PyLong_POS_INIT(91), \ + _PyLong_POS_INIT(92), \ + _PyLong_POS_INIT(93), \ + _PyLong_POS_INIT(94), \ + _PyLong_POS_INIT(95), \ + _PyLong_POS_INIT(96), \ + _PyLong_POS_INIT(97), \ + _PyLong_POS_INIT(98), \ + _PyLong_POS_INIT(99), \ + _PyLong_POS_INIT(100), \ + _PyLong_POS_INIT(101), \ + _PyLong_POS_INIT(102), \ + _PyLong_POS_INIT(103), \ + _PyLong_POS_INIT(104), \ + _PyLong_POS_INIT(105), \ + _PyLong_POS_INIT(106), \ + _PyLong_POS_INIT(107), \ + _PyLong_POS_INIT(108), \ + _PyLong_POS_INIT(109), \ + _PyLong_POS_INIT(110), \ + _PyLong_POS_INIT(111), \ + _PyLong_POS_INIT(112), \ + _PyLong_POS_INIT(113), \ + _PyLong_POS_INIT(114), \ + _PyLong_POS_INIT(115), \ + _PyLong_POS_INIT(116), \ + _PyLong_POS_INIT(117), \ + _PyLong_POS_INIT(118), \ + _PyLong_POS_INIT(119), \ + _PyLong_POS_INIT(120), \ + _PyLong_POS_INIT(121), \ + _PyLong_POS_INIT(122), \ + _PyLong_POS_INIT(123), \ + _PyLong_POS_INIT(124), \ + _PyLong_POS_INIT(125), \ + _PyLong_POS_INIT(126), \ + _PyLong_POS_INIT(127), \ + _PyLong_POS_INIT(128), \ + _PyLong_POS_INIT(129), \ + _PyLong_POS_INIT(130), \ + _PyLong_POS_INIT(131), \ + _PyLong_POS_INIT(132), \ + _PyLong_POS_INIT(133), \ + _PyLong_POS_INIT(134), \ + _PyLong_POS_INIT(135), \ + _PyLong_POS_INIT(136), \ + _PyLong_POS_INIT(137), \ + _PyLong_POS_INIT(138), \ + _PyLong_POS_INIT(139), \ + _PyLong_POS_INIT(140), \ + _PyLong_POS_INIT(141), \ + _PyLong_POS_INIT(142), \ + _PyLong_POS_INIT(143), \ + _PyLong_POS_INIT(144), \ + _PyLong_POS_INIT(145), \ + _PyLong_POS_INIT(146), \ + _PyLong_POS_INIT(147), \ + _PyLong_POS_INIT(148), \ + _PyLong_POS_INIT(149), \ + _PyLong_POS_INIT(150), \ + _PyLong_POS_INIT(151), \ + _PyLong_POS_INIT(152), \ + _PyLong_POS_INIT(153), \ + _PyLong_POS_INIT(154), \ + _PyLong_POS_INIT(155), \ + _PyLong_POS_INIT(156), \ + _PyLong_POS_INIT(157), \ + _PyLong_POS_INIT(158), \ + _PyLong_POS_INIT(159), \ + _PyLong_POS_INIT(160), \ + _PyLong_POS_INIT(161), \ + _PyLong_POS_INIT(162), \ + _PyLong_POS_INIT(163), \ + _PyLong_POS_INIT(164), \ + _PyLong_POS_INIT(165), \ + _PyLong_POS_INIT(166), \ + _PyLong_POS_INIT(167), \ + _PyLong_POS_INIT(168), \ + _PyLong_POS_INIT(169), \ + _PyLong_POS_INIT(170), \ + _PyLong_POS_INIT(171), \ + _PyLong_POS_INIT(172), \ + _PyLong_POS_INIT(173), \ + _PyLong_POS_INIT(174), \ + _PyLong_POS_INIT(175), \ + _PyLong_POS_INIT(176), \ + _PyLong_POS_INIT(177), \ + _PyLong_POS_INIT(178), \ + _PyLong_POS_INIT(179), \ + _PyLong_POS_INIT(180), \ + _PyLong_POS_INIT(181), \ + _PyLong_POS_INIT(182), \ + _PyLong_POS_INIT(183), \ + _PyLong_POS_INIT(184), \ + _PyLong_POS_INIT(185), \ + _PyLong_POS_INIT(186), \ + _PyLong_POS_INIT(187), \ + _PyLong_POS_INIT(188), \ + _PyLong_POS_INIT(189), \ + _PyLong_POS_INIT(190), \ + _PyLong_POS_INIT(191), \ + _PyLong_POS_INIT(192), \ + _PyLong_POS_INIT(193), \ + _PyLong_POS_INIT(194), \ + _PyLong_POS_INIT(195), \ + _PyLong_POS_INIT(196), \ + _PyLong_POS_INIT(197), \ + _PyLong_POS_INIT(198), \ + _PyLong_POS_INIT(199), \ + _PyLong_POS_INIT(200), \ + _PyLong_POS_INIT(201), \ + _PyLong_POS_INIT(202), \ + _PyLong_POS_INIT(203), \ + _PyLong_POS_INIT(204), \ + _PyLong_POS_INIT(205), \ + _PyLong_POS_INIT(206), \ + _PyLong_POS_INIT(207), \ + _PyLong_POS_INIT(208), \ + _PyLong_POS_INIT(209), \ + _PyLong_POS_INIT(210), \ + _PyLong_POS_INIT(211), \ + _PyLong_POS_INIT(212), \ + _PyLong_POS_INIT(213), \ + _PyLong_POS_INIT(214), \ + _PyLong_POS_INIT(215), \ + _PyLong_POS_INIT(216), \ + _PyLong_POS_INIT(217), \ + _PyLong_POS_INIT(218), \ + _PyLong_POS_INIT(219), \ + _PyLong_POS_INIT(220), \ + _PyLong_POS_INIT(221), \ + _PyLong_POS_INIT(222), \ + _PyLong_POS_INIT(223), \ + _PyLong_POS_INIT(224), \ + _PyLong_POS_INIT(225), \ + _PyLong_POS_INIT(226), \ + _PyLong_POS_INIT(227), \ + _PyLong_POS_INIT(228), \ + _PyLong_POS_INIT(229), \ + _PyLong_POS_INIT(230), \ + _PyLong_POS_INIT(231), \ + _PyLong_POS_INIT(232), \ + _PyLong_POS_INIT(233), \ + _PyLong_POS_INIT(234), \ + _PyLong_POS_INIT(235), \ + _PyLong_POS_INIT(236), \ + _PyLong_POS_INIT(237), \ + _PyLong_POS_INIT(238), \ + _PyLong_POS_INIT(239), \ + _PyLong_POS_INIT(240), \ + _PyLong_POS_INIT(241), \ + _PyLong_POS_INIT(242), \ + _PyLong_POS_INIT(243), \ + _PyLong_POS_INIT(244), \ + _PyLong_POS_INIT(245), \ + _PyLong_POS_INIT(246), \ + _PyLong_POS_INIT(247), \ + _PyLong_POS_INIT(248), \ + _PyLong_POS_INIT(249), \ + _PyLong_POS_INIT(250), \ + _PyLong_POS_INIT(251), \ + _PyLong_POS_INIT(252), \ + _PyLong_POS_INIT(253), \ + _PyLong_POS_INIT(254), \ + _PyLong_POS_INIT(255), \ + _PyLong_POS_INIT(256), \ + }, \ }, \ } diff --git a/Include/internal/pycore_long.h b/Include/internal/pycore_long.h index 299988d6b385b7..4d1a0d0424969b 100644 --- a/Include/internal/pycore_long.h +++ b/Include/internal/pycore_long.h @@ -14,7 +14,6 @@ extern "C" { /* runtime lifecycle */ -extern void _PyLong_InitGlobalObjects(PyInterpreterState *); extern PyStatus _PyLong_InitTypes(PyInterpreterState *); diff --git a/Objects/longobject.c b/Objects/longobject.c index f6d5e7648be16f..77434e67642f5e 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5832,29 +5832,6 @@ PyLong_GetInfo(void) /* runtime lifecycle */ -void -_PyLong_InitGlobalObjects(PyInterpreterState *interp) -{ - if (!_Py_IsMainInterpreter(interp)) { - return; - } - - PyLongObject *small_ints = _PyLong_SMALL_INTS; - if (small_ints[0].ob_base.ob_base.ob_refcnt != 0) { - // Py_Initialize() must be running a second time. - return; - } - - for (Py_ssize_t i=0; i < _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS; i++) { - sdigit ival = (sdigit)i - _PY_NSMALLNEGINTS; - int size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); - small_ints[i].ob_base.ob_base.ob_refcnt = 1; - small_ints[i].ob_base.ob_base.ob_type = &PyLong_Type; - small_ints[i].ob_base.ob_size = size; - small_ints[i].ob_digit[0] = (digit)abs(ival); - } -} - PyStatus _PyLong_InitTypes(PyInterpreterState *interp) { @@ -5875,9 +5852,3 @@ _PyLong_InitTypes(PyInterpreterState *interp) return _PyStatus_OK(); } - -void -_PyLong_Fini(PyInterpreterState *interp) -{ - (void)interp; -} diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index b6d73a9ce22160..73290afb99332c 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -676,8 +676,6 @@ pycore_init_global_objects(PyInterpreterState *interp) { PyStatus status; - _PyLong_InitGlobalObjects(interp); - _PyFloat_InitState(interp); status = _PyBytes_InitGlobalObjects(interp); From 564a77c82df194b43601a7852a975a13ad2eeb60 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 15:22:37 -0700 Subject: [PATCH 08/12] Fix a comment. --- Include/internal/pycore_global_objects.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index d655f882af6b40..83e1c9e37da288 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -33,7 +33,7 @@ struct _Py_global_objects { /* Small integers are preallocated in this array so that they * can be shared. * The integers that are preallocated are those in the range - *-_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive). + * -_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (exclusive). */ PyLongObject small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS]; } singletons; From 9959c5a3f2caeceed8e601bb75fe5e0dd5fea383 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 16:57:00 -0700 Subject: [PATCH 09/12] Clean up the initializer. --- Include/internal/pycore_global_objects.h | 548 ++++++++++++----------- 1 file changed, 280 insertions(+), 268 deletions(-) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 83e1c9e37da288..83b78044d9bb46 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -9,15 +9,27 @@ extern "C" { #endif +#define _PyObject_IMMORTAL_INIT(type) \ + { \ + .ob_refcnt = 999999999, \ + .ob_type = type, \ + } +#define _PyVarObject_IMMORTAL_INIT(type, size) \ + { \ + .ob_base = _PyObject_IMMORTAL_INIT(type), \ + .ob_size = size, \ + } + + #define _PY_NSMALLPOSINTS 257 #define _PY_NSMALLNEGINTS 5 -#define _PyLong_NEG_INIT(val) \ - { PyVarObject_HEAD_INIT(&PyLong_Type, -1) .ob_digit = { -(val) }, } -#define _PyLong_ZERO_INIT \ - { PyVarObject_HEAD_INIT(&PyLong_Type, 0) .ob_digit = { 0 }, } -#define _PyLong_POS_INIT(val) \ - { PyVarObject_HEAD_INIT(&PyLong_Type, 1) .ob_digit = { val }, } +#define _PyLong_DIGIT_INIT(val) \ + { \ + _PyVarObject_IMMORTAL_INIT(&PyLong_Type, \ + ((val) == 0 ? 0 : ((val) > 0 ? 1 : -1))), \ + .ob_digit = { ((val) >= 0 ? (val) : -(val)) }, \ + } // Only immutable objects should be considered runtime-global. @@ -42,268 +54,268 @@ struct _Py_global_objects { #define _Py_global_objects_INIT { \ .singletons = { \ .small_ints = { \ - _PyLong_NEG_INIT(-5), \ - _PyLong_NEG_INIT(-4), \ - _PyLong_NEG_INIT(-3), \ - _PyLong_NEG_INIT(-2), \ - _PyLong_NEG_INIT(-1), \ - _PyLong_ZERO_INIT, \ - _PyLong_POS_INIT(1), \ - _PyLong_POS_INIT(2), \ - _PyLong_POS_INIT(3), \ - _PyLong_POS_INIT(4), \ - _PyLong_POS_INIT(5), \ - _PyLong_POS_INIT(6), \ - _PyLong_POS_INIT(7), \ - _PyLong_POS_INIT(8), \ - _PyLong_POS_INIT(9), \ - _PyLong_POS_INIT(10), \ - _PyLong_POS_INIT(11), \ - _PyLong_POS_INIT(12), \ - _PyLong_POS_INIT(13), \ - _PyLong_POS_INIT(14), \ - _PyLong_POS_INIT(15), \ - _PyLong_POS_INIT(16), \ - _PyLong_POS_INIT(17), \ - _PyLong_POS_INIT(18), \ - _PyLong_POS_INIT(19), \ - _PyLong_POS_INIT(20), \ - _PyLong_POS_INIT(21), \ - _PyLong_POS_INIT(22), \ - _PyLong_POS_INIT(23), \ - _PyLong_POS_INIT(24), \ - _PyLong_POS_INIT(25), \ - _PyLong_POS_INIT(26), \ - _PyLong_POS_INIT(27), \ - _PyLong_POS_INIT(28), \ - _PyLong_POS_INIT(29), \ - _PyLong_POS_INIT(30), \ - _PyLong_POS_INIT(31), \ - _PyLong_POS_INIT(32), \ - _PyLong_POS_INIT(33), \ - _PyLong_POS_INIT(34), \ - _PyLong_POS_INIT(35), \ - _PyLong_POS_INIT(36), \ - _PyLong_POS_INIT(37), \ - _PyLong_POS_INIT(38), \ - _PyLong_POS_INIT(39), \ - _PyLong_POS_INIT(40), \ - _PyLong_POS_INIT(41), \ - _PyLong_POS_INIT(42), \ - _PyLong_POS_INIT(43), \ - _PyLong_POS_INIT(44), \ - _PyLong_POS_INIT(45), \ - _PyLong_POS_INIT(46), \ - _PyLong_POS_INIT(47), \ - _PyLong_POS_INIT(48), \ - _PyLong_POS_INIT(49), \ - _PyLong_POS_INIT(50), \ - _PyLong_POS_INIT(51), \ - _PyLong_POS_INIT(52), \ - _PyLong_POS_INIT(53), \ - _PyLong_POS_INIT(54), \ - _PyLong_POS_INIT(55), \ - _PyLong_POS_INIT(56), \ - _PyLong_POS_INIT(57), \ - _PyLong_POS_INIT(58), \ - _PyLong_POS_INIT(59), \ - _PyLong_POS_INIT(60), \ - _PyLong_POS_INIT(61), \ - _PyLong_POS_INIT(62), \ - _PyLong_POS_INIT(63), \ - _PyLong_POS_INIT(64), \ - _PyLong_POS_INIT(65), \ - _PyLong_POS_INIT(66), \ - _PyLong_POS_INIT(67), \ - _PyLong_POS_INIT(68), \ - _PyLong_POS_INIT(69), \ - _PyLong_POS_INIT(70), \ - _PyLong_POS_INIT(71), \ - _PyLong_POS_INIT(72), \ - _PyLong_POS_INIT(73), \ - _PyLong_POS_INIT(74), \ - _PyLong_POS_INIT(75), \ - _PyLong_POS_INIT(76), \ - _PyLong_POS_INIT(77), \ - _PyLong_POS_INIT(78), \ - _PyLong_POS_INIT(79), \ - _PyLong_POS_INIT(80), \ - _PyLong_POS_INIT(81), \ - _PyLong_POS_INIT(82), \ - _PyLong_POS_INIT(83), \ - _PyLong_POS_INIT(84), \ - _PyLong_POS_INIT(85), \ - _PyLong_POS_INIT(86), \ - _PyLong_POS_INIT(87), \ - _PyLong_POS_INIT(88), \ - _PyLong_POS_INIT(89), \ - _PyLong_POS_INIT(90), \ - _PyLong_POS_INIT(91), \ - _PyLong_POS_INIT(92), \ - _PyLong_POS_INIT(93), \ - _PyLong_POS_INIT(94), \ - _PyLong_POS_INIT(95), \ - _PyLong_POS_INIT(96), \ - _PyLong_POS_INIT(97), \ - _PyLong_POS_INIT(98), \ - _PyLong_POS_INIT(99), \ - _PyLong_POS_INIT(100), \ - _PyLong_POS_INIT(101), \ - _PyLong_POS_INIT(102), \ - _PyLong_POS_INIT(103), \ - _PyLong_POS_INIT(104), \ - _PyLong_POS_INIT(105), \ - _PyLong_POS_INIT(106), \ - _PyLong_POS_INIT(107), \ - _PyLong_POS_INIT(108), \ - _PyLong_POS_INIT(109), \ - _PyLong_POS_INIT(110), \ - _PyLong_POS_INIT(111), \ - _PyLong_POS_INIT(112), \ - _PyLong_POS_INIT(113), \ - _PyLong_POS_INIT(114), \ - _PyLong_POS_INIT(115), \ - _PyLong_POS_INIT(116), \ - _PyLong_POS_INIT(117), \ - _PyLong_POS_INIT(118), \ - _PyLong_POS_INIT(119), \ - _PyLong_POS_INIT(120), \ - _PyLong_POS_INIT(121), \ - _PyLong_POS_INIT(122), \ - _PyLong_POS_INIT(123), \ - _PyLong_POS_INIT(124), \ - _PyLong_POS_INIT(125), \ - _PyLong_POS_INIT(126), \ - _PyLong_POS_INIT(127), \ - _PyLong_POS_INIT(128), \ - _PyLong_POS_INIT(129), \ - _PyLong_POS_INIT(130), \ - _PyLong_POS_INIT(131), \ - _PyLong_POS_INIT(132), \ - _PyLong_POS_INIT(133), \ - _PyLong_POS_INIT(134), \ - _PyLong_POS_INIT(135), \ - _PyLong_POS_INIT(136), \ - _PyLong_POS_INIT(137), \ - _PyLong_POS_INIT(138), \ - _PyLong_POS_INIT(139), \ - _PyLong_POS_INIT(140), \ - _PyLong_POS_INIT(141), \ - _PyLong_POS_INIT(142), \ - _PyLong_POS_INIT(143), \ - _PyLong_POS_INIT(144), \ - _PyLong_POS_INIT(145), \ - _PyLong_POS_INIT(146), \ - _PyLong_POS_INIT(147), \ - _PyLong_POS_INIT(148), \ - _PyLong_POS_INIT(149), \ - _PyLong_POS_INIT(150), \ - _PyLong_POS_INIT(151), \ - _PyLong_POS_INIT(152), \ - _PyLong_POS_INIT(153), \ - _PyLong_POS_INIT(154), \ - _PyLong_POS_INIT(155), \ - _PyLong_POS_INIT(156), \ - _PyLong_POS_INIT(157), \ - _PyLong_POS_INIT(158), \ - _PyLong_POS_INIT(159), \ - _PyLong_POS_INIT(160), \ - _PyLong_POS_INIT(161), \ - _PyLong_POS_INIT(162), \ - _PyLong_POS_INIT(163), \ - _PyLong_POS_INIT(164), \ - _PyLong_POS_INIT(165), \ - _PyLong_POS_INIT(166), \ - _PyLong_POS_INIT(167), \ - _PyLong_POS_INIT(168), \ - _PyLong_POS_INIT(169), \ - _PyLong_POS_INIT(170), \ - _PyLong_POS_INIT(171), \ - _PyLong_POS_INIT(172), \ - _PyLong_POS_INIT(173), \ - _PyLong_POS_INIT(174), \ - _PyLong_POS_INIT(175), \ - _PyLong_POS_INIT(176), \ - _PyLong_POS_INIT(177), \ - _PyLong_POS_INIT(178), \ - _PyLong_POS_INIT(179), \ - _PyLong_POS_INIT(180), \ - _PyLong_POS_INIT(181), \ - _PyLong_POS_INIT(182), \ - _PyLong_POS_INIT(183), \ - _PyLong_POS_INIT(184), \ - _PyLong_POS_INIT(185), \ - _PyLong_POS_INIT(186), \ - _PyLong_POS_INIT(187), \ - _PyLong_POS_INIT(188), \ - _PyLong_POS_INIT(189), \ - _PyLong_POS_INIT(190), \ - _PyLong_POS_INIT(191), \ - _PyLong_POS_INIT(192), \ - _PyLong_POS_INIT(193), \ - _PyLong_POS_INIT(194), \ - _PyLong_POS_INIT(195), \ - _PyLong_POS_INIT(196), \ - _PyLong_POS_INIT(197), \ - _PyLong_POS_INIT(198), \ - _PyLong_POS_INIT(199), \ - _PyLong_POS_INIT(200), \ - _PyLong_POS_INIT(201), \ - _PyLong_POS_INIT(202), \ - _PyLong_POS_INIT(203), \ - _PyLong_POS_INIT(204), \ - _PyLong_POS_INIT(205), \ - _PyLong_POS_INIT(206), \ - _PyLong_POS_INIT(207), \ - _PyLong_POS_INIT(208), \ - _PyLong_POS_INIT(209), \ - _PyLong_POS_INIT(210), \ - _PyLong_POS_INIT(211), \ - _PyLong_POS_INIT(212), \ - _PyLong_POS_INIT(213), \ - _PyLong_POS_INIT(214), \ - _PyLong_POS_INIT(215), \ - _PyLong_POS_INIT(216), \ - _PyLong_POS_INIT(217), \ - _PyLong_POS_INIT(218), \ - _PyLong_POS_INIT(219), \ - _PyLong_POS_INIT(220), \ - _PyLong_POS_INIT(221), \ - _PyLong_POS_INIT(222), \ - _PyLong_POS_INIT(223), \ - _PyLong_POS_INIT(224), \ - _PyLong_POS_INIT(225), \ - _PyLong_POS_INIT(226), \ - _PyLong_POS_INIT(227), \ - _PyLong_POS_INIT(228), \ - _PyLong_POS_INIT(229), \ - _PyLong_POS_INIT(230), \ - _PyLong_POS_INIT(231), \ - _PyLong_POS_INIT(232), \ - _PyLong_POS_INIT(233), \ - _PyLong_POS_INIT(234), \ - _PyLong_POS_INIT(235), \ - _PyLong_POS_INIT(236), \ - _PyLong_POS_INIT(237), \ - _PyLong_POS_INIT(238), \ - _PyLong_POS_INIT(239), \ - _PyLong_POS_INIT(240), \ - _PyLong_POS_INIT(241), \ - _PyLong_POS_INIT(242), \ - _PyLong_POS_INIT(243), \ - _PyLong_POS_INIT(244), \ - _PyLong_POS_INIT(245), \ - _PyLong_POS_INIT(246), \ - _PyLong_POS_INIT(247), \ - _PyLong_POS_INIT(248), \ - _PyLong_POS_INIT(249), \ - _PyLong_POS_INIT(250), \ - _PyLong_POS_INIT(251), \ - _PyLong_POS_INIT(252), \ - _PyLong_POS_INIT(253), \ - _PyLong_POS_INIT(254), \ - _PyLong_POS_INIT(255), \ - _PyLong_POS_INIT(256), \ + _PyLong_DIGIT_INIT(-5), \ + _PyLong_DIGIT_INIT(-4), \ + _PyLong_DIGIT_INIT(-3), \ + _PyLong_DIGIT_INIT(-2), \ + _PyLong_DIGIT_INIT(-1), \ + _PyLong_DIGIT_INIT(0), \ + _PyLong_DIGIT_INIT(1), \ + _PyLong_DIGIT_INIT(2), \ + _PyLong_DIGIT_INIT(3), \ + _PyLong_DIGIT_INIT(4), \ + _PyLong_DIGIT_INIT(5), \ + _PyLong_DIGIT_INIT(6), \ + _PyLong_DIGIT_INIT(7), \ + _PyLong_DIGIT_INIT(8), \ + _PyLong_DIGIT_INIT(9), \ + _PyLong_DIGIT_INIT(10), \ + _PyLong_DIGIT_INIT(11), \ + _PyLong_DIGIT_INIT(12), \ + _PyLong_DIGIT_INIT(13), \ + _PyLong_DIGIT_INIT(14), \ + _PyLong_DIGIT_INIT(15), \ + _PyLong_DIGIT_INIT(16), \ + _PyLong_DIGIT_INIT(17), \ + _PyLong_DIGIT_INIT(18), \ + _PyLong_DIGIT_INIT(19), \ + _PyLong_DIGIT_INIT(20), \ + _PyLong_DIGIT_INIT(21), \ + _PyLong_DIGIT_INIT(22), \ + _PyLong_DIGIT_INIT(23), \ + _PyLong_DIGIT_INIT(24), \ + _PyLong_DIGIT_INIT(25), \ + _PyLong_DIGIT_INIT(26), \ + _PyLong_DIGIT_INIT(27), \ + _PyLong_DIGIT_INIT(28), \ + _PyLong_DIGIT_INIT(29), \ + _PyLong_DIGIT_INIT(30), \ + _PyLong_DIGIT_INIT(31), \ + _PyLong_DIGIT_INIT(32), \ + _PyLong_DIGIT_INIT(33), \ + _PyLong_DIGIT_INIT(34), \ + _PyLong_DIGIT_INIT(35), \ + _PyLong_DIGIT_INIT(36), \ + _PyLong_DIGIT_INIT(37), \ + _PyLong_DIGIT_INIT(38), \ + _PyLong_DIGIT_INIT(39), \ + _PyLong_DIGIT_INIT(40), \ + _PyLong_DIGIT_INIT(41), \ + _PyLong_DIGIT_INIT(42), \ + _PyLong_DIGIT_INIT(43), \ + _PyLong_DIGIT_INIT(44), \ + _PyLong_DIGIT_INIT(45), \ + _PyLong_DIGIT_INIT(46), \ + _PyLong_DIGIT_INIT(47), \ + _PyLong_DIGIT_INIT(48), \ + _PyLong_DIGIT_INIT(49), \ + _PyLong_DIGIT_INIT(50), \ + _PyLong_DIGIT_INIT(51), \ + _PyLong_DIGIT_INIT(52), \ + _PyLong_DIGIT_INIT(53), \ + _PyLong_DIGIT_INIT(54), \ + _PyLong_DIGIT_INIT(55), \ + _PyLong_DIGIT_INIT(56), \ + _PyLong_DIGIT_INIT(57), \ + _PyLong_DIGIT_INIT(58), \ + _PyLong_DIGIT_INIT(59), \ + _PyLong_DIGIT_INIT(60), \ + _PyLong_DIGIT_INIT(61), \ + _PyLong_DIGIT_INIT(62), \ + _PyLong_DIGIT_INIT(63), \ + _PyLong_DIGIT_INIT(64), \ + _PyLong_DIGIT_INIT(65), \ + _PyLong_DIGIT_INIT(66), \ + _PyLong_DIGIT_INIT(67), \ + _PyLong_DIGIT_INIT(68), \ + _PyLong_DIGIT_INIT(69), \ + _PyLong_DIGIT_INIT(70), \ + _PyLong_DIGIT_INIT(71), \ + _PyLong_DIGIT_INIT(72), \ + _PyLong_DIGIT_INIT(73), \ + _PyLong_DIGIT_INIT(74), \ + _PyLong_DIGIT_INIT(75), \ + _PyLong_DIGIT_INIT(76), \ + _PyLong_DIGIT_INIT(77), \ + _PyLong_DIGIT_INIT(78), \ + _PyLong_DIGIT_INIT(79), \ + _PyLong_DIGIT_INIT(80), \ + _PyLong_DIGIT_INIT(81), \ + _PyLong_DIGIT_INIT(82), \ + _PyLong_DIGIT_INIT(83), \ + _PyLong_DIGIT_INIT(84), \ + _PyLong_DIGIT_INIT(85), \ + _PyLong_DIGIT_INIT(86), \ + _PyLong_DIGIT_INIT(87), \ + _PyLong_DIGIT_INIT(88), \ + _PyLong_DIGIT_INIT(89), \ + _PyLong_DIGIT_INIT(90), \ + _PyLong_DIGIT_INIT(91), \ + _PyLong_DIGIT_INIT(92), \ + _PyLong_DIGIT_INIT(93), \ + _PyLong_DIGIT_INIT(94), \ + _PyLong_DIGIT_INIT(95), \ + _PyLong_DIGIT_INIT(96), \ + _PyLong_DIGIT_INIT(97), \ + _PyLong_DIGIT_INIT(98), \ + _PyLong_DIGIT_INIT(99), \ + _PyLong_DIGIT_INIT(100), \ + _PyLong_DIGIT_INIT(101), \ + _PyLong_DIGIT_INIT(102), \ + _PyLong_DIGIT_INIT(103), \ + _PyLong_DIGIT_INIT(104), \ + _PyLong_DIGIT_INIT(105), \ + _PyLong_DIGIT_INIT(106), \ + _PyLong_DIGIT_INIT(107), \ + _PyLong_DIGIT_INIT(108), \ + _PyLong_DIGIT_INIT(109), \ + _PyLong_DIGIT_INIT(110), \ + _PyLong_DIGIT_INIT(111), \ + _PyLong_DIGIT_INIT(112), \ + _PyLong_DIGIT_INIT(113), \ + _PyLong_DIGIT_INIT(114), \ + _PyLong_DIGIT_INIT(115), \ + _PyLong_DIGIT_INIT(116), \ + _PyLong_DIGIT_INIT(117), \ + _PyLong_DIGIT_INIT(118), \ + _PyLong_DIGIT_INIT(119), \ + _PyLong_DIGIT_INIT(120), \ + _PyLong_DIGIT_INIT(121), \ + _PyLong_DIGIT_INIT(122), \ + _PyLong_DIGIT_INIT(123), \ + _PyLong_DIGIT_INIT(124), \ + _PyLong_DIGIT_INIT(125), \ + _PyLong_DIGIT_INIT(126), \ + _PyLong_DIGIT_INIT(127), \ + _PyLong_DIGIT_INIT(128), \ + _PyLong_DIGIT_INIT(129), \ + _PyLong_DIGIT_INIT(130), \ + _PyLong_DIGIT_INIT(131), \ + _PyLong_DIGIT_INIT(132), \ + _PyLong_DIGIT_INIT(133), \ + _PyLong_DIGIT_INIT(134), \ + _PyLong_DIGIT_INIT(135), \ + _PyLong_DIGIT_INIT(136), \ + _PyLong_DIGIT_INIT(137), \ + _PyLong_DIGIT_INIT(138), \ + _PyLong_DIGIT_INIT(139), \ + _PyLong_DIGIT_INIT(140), \ + _PyLong_DIGIT_INIT(141), \ + _PyLong_DIGIT_INIT(142), \ + _PyLong_DIGIT_INIT(143), \ + _PyLong_DIGIT_INIT(144), \ + _PyLong_DIGIT_INIT(145), \ + _PyLong_DIGIT_INIT(146), \ + _PyLong_DIGIT_INIT(147), \ + _PyLong_DIGIT_INIT(148), \ + _PyLong_DIGIT_INIT(149), \ + _PyLong_DIGIT_INIT(150), \ + _PyLong_DIGIT_INIT(151), \ + _PyLong_DIGIT_INIT(152), \ + _PyLong_DIGIT_INIT(153), \ + _PyLong_DIGIT_INIT(154), \ + _PyLong_DIGIT_INIT(155), \ + _PyLong_DIGIT_INIT(156), \ + _PyLong_DIGIT_INIT(157), \ + _PyLong_DIGIT_INIT(158), \ + _PyLong_DIGIT_INIT(159), \ + _PyLong_DIGIT_INIT(160), \ + _PyLong_DIGIT_INIT(161), \ + _PyLong_DIGIT_INIT(162), \ + _PyLong_DIGIT_INIT(163), \ + _PyLong_DIGIT_INIT(164), \ + _PyLong_DIGIT_INIT(165), \ + _PyLong_DIGIT_INIT(166), \ + _PyLong_DIGIT_INIT(167), \ + _PyLong_DIGIT_INIT(168), \ + _PyLong_DIGIT_INIT(169), \ + _PyLong_DIGIT_INIT(170), \ + _PyLong_DIGIT_INIT(171), \ + _PyLong_DIGIT_INIT(172), \ + _PyLong_DIGIT_INIT(173), \ + _PyLong_DIGIT_INIT(174), \ + _PyLong_DIGIT_INIT(175), \ + _PyLong_DIGIT_INIT(176), \ + _PyLong_DIGIT_INIT(177), \ + _PyLong_DIGIT_INIT(178), \ + _PyLong_DIGIT_INIT(179), \ + _PyLong_DIGIT_INIT(180), \ + _PyLong_DIGIT_INIT(181), \ + _PyLong_DIGIT_INIT(182), \ + _PyLong_DIGIT_INIT(183), \ + _PyLong_DIGIT_INIT(184), \ + _PyLong_DIGIT_INIT(185), \ + _PyLong_DIGIT_INIT(186), \ + _PyLong_DIGIT_INIT(187), \ + _PyLong_DIGIT_INIT(188), \ + _PyLong_DIGIT_INIT(189), \ + _PyLong_DIGIT_INIT(190), \ + _PyLong_DIGIT_INIT(191), \ + _PyLong_DIGIT_INIT(192), \ + _PyLong_DIGIT_INIT(193), \ + _PyLong_DIGIT_INIT(194), \ + _PyLong_DIGIT_INIT(195), \ + _PyLong_DIGIT_INIT(196), \ + _PyLong_DIGIT_INIT(197), \ + _PyLong_DIGIT_INIT(198), \ + _PyLong_DIGIT_INIT(199), \ + _PyLong_DIGIT_INIT(200), \ + _PyLong_DIGIT_INIT(201), \ + _PyLong_DIGIT_INIT(202), \ + _PyLong_DIGIT_INIT(203), \ + _PyLong_DIGIT_INIT(204), \ + _PyLong_DIGIT_INIT(205), \ + _PyLong_DIGIT_INIT(206), \ + _PyLong_DIGIT_INIT(207), \ + _PyLong_DIGIT_INIT(208), \ + _PyLong_DIGIT_INIT(209), \ + _PyLong_DIGIT_INIT(210), \ + _PyLong_DIGIT_INIT(211), \ + _PyLong_DIGIT_INIT(212), \ + _PyLong_DIGIT_INIT(213), \ + _PyLong_DIGIT_INIT(214), \ + _PyLong_DIGIT_INIT(215), \ + _PyLong_DIGIT_INIT(216), \ + _PyLong_DIGIT_INIT(217), \ + _PyLong_DIGIT_INIT(218), \ + _PyLong_DIGIT_INIT(219), \ + _PyLong_DIGIT_INIT(220), \ + _PyLong_DIGIT_INIT(221), \ + _PyLong_DIGIT_INIT(222), \ + _PyLong_DIGIT_INIT(223), \ + _PyLong_DIGIT_INIT(224), \ + _PyLong_DIGIT_INIT(225), \ + _PyLong_DIGIT_INIT(226), \ + _PyLong_DIGIT_INIT(227), \ + _PyLong_DIGIT_INIT(228), \ + _PyLong_DIGIT_INIT(229), \ + _PyLong_DIGIT_INIT(230), \ + _PyLong_DIGIT_INIT(231), \ + _PyLong_DIGIT_INIT(232), \ + _PyLong_DIGIT_INIT(233), \ + _PyLong_DIGIT_INIT(234), \ + _PyLong_DIGIT_INIT(235), \ + _PyLong_DIGIT_INIT(236), \ + _PyLong_DIGIT_INIT(237), \ + _PyLong_DIGIT_INIT(238), \ + _PyLong_DIGIT_INIT(239), \ + _PyLong_DIGIT_INIT(240), \ + _PyLong_DIGIT_INIT(241), \ + _PyLong_DIGIT_INIT(242), \ + _PyLong_DIGIT_INIT(243), \ + _PyLong_DIGIT_INIT(244), \ + _PyLong_DIGIT_INIT(245), \ + _PyLong_DIGIT_INIT(246), \ + _PyLong_DIGIT_INIT(247), \ + _PyLong_DIGIT_INIT(248), \ + _PyLong_DIGIT_INIT(249), \ + _PyLong_DIGIT_INIT(250), \ + _PyLong_DIGIT_INIT(251), \ + _PyLong_DIGIT_INIT(252), \ + _PyLong_DIGIT_INIT(253), \ + _PyLong_DIGIT_INIT(254), \ + _PyLong_DIGIT_INIT(255), \ + _PyLong_DIGIT_INIT(256), \ }, \ }, \ } From c4c85e7880546c6e4b246de946eaf1b794b09bfe Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 16:57:41 -0700 Subject: [PATCH 10/12] Separate the global objects from the type-specific helpers. --- Include/internal/pycore_global_objects.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 83b78044d9bb46..4e530ab84c902b 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -32,6 +32,10 @@ extern "C" { } +/********************** + * the global objects * + **********************/ + // Only immutable objects should be considered runtime-global. // All others must be per-interpreter. From c017103ba1760ff080e3be093a038081d34f3260 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 16:58:00 -0700 Subject: [PATCH 11/12] Separate the int helpers. --- Include/internal/pycore_global_objects.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 4e530ab84c902b..6cae3bca6be45a 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -21,6 +21,8 @@ extern "C" { } +/* int objects */ + #define _PY_NSMALLPOSINTS 257 #define _PY_NSMALLNEGINTS 5 From f2821bd8475046a78c5ab3392b3a1d8e5a245b8b Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 13 Dec 2021 17:34:45 -0700 Subject: [PATCH 12/12] Do not clear _PyRuntime.global_objects in _PyRuntimeState_reset(). --- Include/internal/pycore_runtime.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index cb8b02b7b11891..725c859ea7853d 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -119,6 +119,8 @@ typedef struct pyruntimestate { struct _Py_unicode_runtime_ids unicode_ids; struct _Py_global_objects global_objects; + // If anything gets added after global_objects then + // _PyRuntimeState_reset() needs to get updated to clear it. } _PyRuntimeState; #define _PyRuntimeState_INIT \ @@ -131,7 +133,7 @@ static inline void _PyRuntimeState_reset(_PyRuntimeState *runtime) { /* Make it match _PyRuntimeState_INIT. */ - memset(runtime, 0, sizeof(*runtime)); + memset(runtime, 0, (size_t)&runtime->global_objects - (size_t)runtime); _Py_global_objects_reset(&runtime->global_objects); }