From 8a196337e2ae08d508fd8932d5d084b5783666c7 Mon Sep 17 00:00:00 2001 From: Albert N Date: Fri, 10 Oct 2025 15:03:25 +0000 Subject: [PATCH 1/2] gh-133160: fix in `Tools/build/deepfreeze.py` nsmallposints were still 256 --- Tools/build/consts_getter.py | 22 ++++++++++++++++++++++ Tools/build/deepfreeze.py | 5 ++++- Tools/build/generate_global_objects.py | 17 +++-------------- 3 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 Tools/build/consts_getter.py diff --git a/Tools/build/consts_getter.py b/Tools/build/consts_getter.py new file mode 100644 index 00000000000000..e26585748ac75d --- /dev/null +++ b/Tools/build/consts_getter.py @@ -0,0 +1,22 @@ +import os + +SCRIPT_NAME = 'Tools/build/consts_getter.py' +__file__ = os.path.abspath(__file__) +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) +INTERNAL = os.path.join(ROOT, 'Include', 'internal') + +def get_nsmallnegints_and_nsmallposints(): + nsmallposints = None + nsmallnegints = None + with open(os.path.join(INTERNAL, 'pycore_runtime_structs.h')) as infile: + for line in infile: + if line.startswith('#define _PY_NSMALLPOSINTS'): + nsmallposints = int(line.split()[-1]) + elif line.startswith('#define _PY_NSMALLNEGINTS'): + nsmallnegints = int(line.split()[-1]) + break + else: + raise NotImplementedError + assert nsmallposints + assert nsmallnegints + return nsmallnegints, nsmallposints diff --git a/Tools/build/deepfreeze.py b/Tools/build/deepfreeze.py index 2b9f03aebb6d7e..477c3d0f5b30d5 100644 --- a/Tools/build/deepfreeze.py +++ b/Tools/build/deepfreeze.py @@ -17,6 +17,7 @@ import time import types +import consts_getter import umarshal TYPE_CHECKING = False @@ -362,7 +363,9 @@ def _generate_int_for_bits(self, name: str, i: int, digit: int) -> None: self.write(f".ob_digit = {{ {ds} }},") def generate_int(self, name: str, i: int) -> str: - if -5 <= i <= 256: + nsmallnegints, nsmallposints = consts_getter.get_nsmallnegints_and_nsmallposints() + + if -nsmallnegints <= i <= nsmallposints: return f"(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + {i}]" if i >= 0: name = f"const_int_{i}" diff --git a/Tools/build/generate_global_objects.py b/Tools/build/generate_global_objects.py index 94905b3756d0d8..5b188e338bab36 100644 --- a/Tools/build/generate_global_objects.py +++ b/Tools/build/generate_global_objects.py @@ -3,6 +3,8 @@ import os.path import re +import consts_getter + SCRIPT_NAME = 'Tools/build/generate_global_objects.py' __file__ = os.path.abspath(__file__) ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) @@ -274,20 +276,7 @@ def generate_global_strings(identifiers, strings): def generate_runtime_init(identifiers, strings): - # First get some info from the declarations. - nsmallposints = None - nsmallnegints = None - with open(os.path.join(INTERNAL, 'pycore_runtime_structs.h')) as infile: - for line in infile: - if line.startswith('#define _PY_NSMALLPOSINTS'): - nsmallposints = int(line.split()[-1]) - elif line.startswith('#define _PY_NSMALLNEGINTS'): - nsmallnegints = int(line.split()[-1]) - break - else: - raise NotImplementedError - assert nsmallposints - assert nsmallnegints + nsmallnegints, nsmallposints = consts_getter.get_nsmallnegints_and_nsmallposints() # Then target the runtime initializer. filename = os.path.join(INTERNAL, 'pycore_runtime_init_generated.h') From 6355a5c50eda8bc710a62193bd9e3f4c61b5b90a Mon Sep 17 00:00:00 2001 From: abebus Date: Sun, 12 Oct 2025 15:24:53 +0300 Subject: [PATCH 2/2] mypy --- .github/workflows/mypy.yml | 1 + Tools/build/consts_getter.py | 2 +- Tools/build/mypy.ini | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml index 5d5d77f29f6eb1..fac0fa8aba3050 100644 --- a/.github/workflows/mypy.yml +++ b/.github/workflows/mypy.yml @@ -16,6 +16,7 @@ on: - "Tools/build/check_extension_modules.py" - "Tools/build/check_warnings.py" - "Tools/build/compute-changes.py" + - "Tools/build/consts_getter.py" - "Tools/build/deepfreeze.py" - "Tools/build/generate-build-details.py" - "Tools/build/generate_sbom.py" diff --git a/Tools/build/consts_getter.py b/Tools/build/consts_getter.py index e26585748ac75d..84b8c319aa4c0c 100644 --- a/Tools/build/consts_getter.py +++ b/Tools/build/consts_getter.py @@ -5,7 +5,7 @@ ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) INTERNAL = os.path.join(ROOT, 'Include', 'internal') -def get_nsmallnegints_and_nsmallposints(): +def get_nsmallnegints_and_nsmallposints() -> tuple[int, int]: nsmallposints = None nsmallnegints = None with open(os.path.join(INTERNAL, 'pycore_runtime_structs.h')) as infile: diff --git a/Tools/build/mypy.ini b/Tools/build/mypy.ini index 331bada6f47d2e..7d341afd1cd48b 100644 --- a/Tools/build/mypy.ini +++ b/Tools/build/mypy.ini @@ -6,6 +6,7 @@ files = Tools/build/check_extension_modules.py, Tools/build/check_warnings.py, Tools/build/compute-changes.py, + Tools/build/consts_getter.py, Tools/build/deepfreeze.py, Tools/build/generate-build-details.py, Tools/build/generate_sbom.py,