Skip to content

Commit 999ab89

Browse files
pythongh-133059: Fix Tools/build/deepfreeze.py for new nsmallposints (python#139906)
1 parent 869bb69 commit 999ab89

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

.github/workflows/mypy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616
- "Tools/build/check_extension_modules.py"
1717
- "Tools/build/check_warnings.py"
1818
- "Tools/build/compute-changes.py"
19+
- "Tools/build/consts_getter.py"
1920
- "Tools/build/deepfreeze.py"
2021
- "Tools/build/generate-build-details.py"
2122
- "Tools/build/generate_sbom.py"

Tools/build/consts_getter.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pathlib import Path
2+
3+
ROOT = Path(__file__).resolve().parents[2]
4+
INTERNAL = ROOT / "Include" / "internal"
5+
6+
def get_nsmallnegints_and_nsmallposints() -> tuple[int, int]:
7+
nsmallposints = None
8+
nsmallnegints = None
9+
with open(INTERNAL / "pycore_runtime_structs.h") as infile:
10+
for line in infile:
11+
if line.startswith("#define _PY_NSMALLPOSINTS"):
12+
nsmallposints = int(line.split()[-1])
13+
elif line.startswith("#define _PY_NSMALLNEGINTS"):
14+
nsmallnegints = int(line.split()[-1])
15+
break
16+
else:
17+
raise NotImplementedError
18+
assert nsmallposints
19+
assert nsmallnegints
20+
return nsmallnegints, nsmallposints

Tools/build/deepfreeze.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import time
1818
import types
1919

20+
import consts_getter
2021
import umarshal
2122

2223
TYPE_CHECKING = False
@@ -362,7 +363,9 @@ def _generate_int_for_bits(self, name: str, i: int, digit: int) -> None:
362363
self.write(f".ob_digit = {{ {ds} }},")
363364

364365
def generate_int(self, name: str, i: int) -> str:
365-
if -5 <= i <= 256:
366+
nsmallnegints, nsmallposints = consts_getter.get_nsmallnegints_and_nsmallposints()
367+
368+
if -nsmallnegints <= i <= nsmallposints:
366369
return f"(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + {i}]"
367370
if i >= 0:
368371
name = f"const_int_{i}"

Tools/build/generate_global_objects.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os.path
44
import re
55

6+
import consts_getter
7+
68
SCRIPT_NAME = 'Tools/build/generate_global_objects.py'
79
__file__ = os.path.abspath(__file__)
810
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
@@ -274,20 +276,7 @@ def generate_global_strings(identifiers, strings):
274276

275277

276278
def generate_runtime_init(identifiers, strings):
277-
# First get some info from the declarations.
278-
nsmallposints = None
279-
nsmallnegints = None
280-
with open(os.path.join(INTERNAL, 'pycore_runtime_structs.h')) as infile:
281-
for line in infile:
282-
if line.startswith('#define _PY_NSMALLPOSINTS'):
283-
nsmallposints = int(line.split()[-1])
284-
elif line.startswith('#define _PY_NSMALLNEGINTS'):
285-
nsmallnegints = int(line.split()[-1])
286-
break
287-
else:
288-
raise NotImplementedError
289-
assert nsmallposints
290-
assert nsmallnegints
279+
nsmallnegints, nsmallposints = consts_getter.get_nsmallnegints_and_nsmallposints()
291280

292281
# Then target the runtime initializer.
293282
filename = os.path.join(INTERNAL, 'pycore_runtime_init_generated.h')

Tools/build/mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ files =
66
Tools/build/check_extension_modules.py,
77
Tools/build/check_warnings.py,
88
Tools/build/compute-changes.py,
9+
Tools/build/consts_getter.py,
910
Tools/build/deepfreeze.py,
1011
Tools/build/generate-build-details.py,
1112
Tools/build/generate_sbom.py,

0 commit comments

Comments
 (0)