Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions Tools/build/consts_getter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

SCRIPT_NAME = 'Tools/build/consts_getter.py'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, I think it's not. it's only used in scripts that generate .c files to put a comment on top to indicate that those file were generated by this particular script

will remove

__file__ = os.path.abspath(__file__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you redefine __file__?

ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about builds that are performed in a different folder? It is possible to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these constants were stolen from Tools/build/generate_global_objects.py, so in this case I think it will be more than enough not to override __file__ and just specify a valid path to Include's

INTERNAL = os.path.join(ROOT, 'Include', 'internal')

def get_nsmallnegints_and_nsmallposints() -> tuple[int, int]:
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
5 changes: 4 additions & 1 deletion Tools/build/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import time
import types

import consts_getter
import umarshal

TYPE_CHECKING = False
Expand Down Expand Up @@ -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}"
Expand Down
17 changes: 3 additions & 14 deletions Tools/build/generate_global_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))
Expand Down Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions Tools/build/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading