summaryrefslogtreecommitdiff
path: root/Tools/scripts/freeze_modules.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-10-28 15:04:33 -0600
committerGitHub <noreply@github.com>2021-10-28 15:04:33 -0600
commit074fa5750640a067d9894c69378a00ceecc3b948 (patch)
tree9635b7a42f48ca862af8dfc3fd4721bc1218bb17 /Tools/scripts/freeze_modules.py
parent66e6b3dcd3bbab06feeff2cbaf8aade7b6223d6c (diff)
downloadcpython-git-074fa5750640a067d9894c69378a00ceecc3b948.tar.gz
bpo-45395: Make custom frozen modules additions instead of replacements. (gh-28778)
Currently custom modules (the array set on PyImport_FrozenModules) replace all the frozen stdlib modules. That can be problematic and is unlikely to be what the user wants. This change treats the custom frozen modules as additions instead. They take precedence over all other frozen modules except for those needed to bootstrap the import system. If the "code" field of an entry in the custom array is NULL then that frozen module is treated as disabled, which allows a custom entry to disable a frozen stdlib module. This change allows us to get rid of is_essential_frozen_module() and simplifies the logic for which frozen modules should be ignored. https://bugs.python.org/issue45395
Diffstat (limited to 'Tools/scripts/freeze_modules.py')
-rw-r--r--Tools/scripts/freeze_modules.py63
1 files changed, 44 insertions, 19 deletions
diff --git a/Tools/scripts/freeze_modules.py b/Tools/scripts/freeze_modules.py
index 5c7eee4295..36142625ca 100644
--- a/Tools/scripts/freeze_modules.py
+++ b/Tools/scripts/freeze_modules.py
@@ -60,6 +60,7 @@ PCBUILD_FILTERS = os.path.join(ROOT_DIR, 'PCbuild', '_freeze_module.vcxproj.filt
OS_PATH = 'ntpath' if os.name == 'nt' else 'posixpath'
# These are modules that get frozen.
+TESTS_SECTION = 'Test module'
FROZEN = [
# See parse_frozen_spec() for the format.
# In cases where the frozenid is duplicated, the first one is re-used.
@@ -94,7 +95,7 @@ FROZEN = [
'site',
'stat',
]),
- ('Test module', [
+ (TESTS_SECTION, [
'__hello__',
'__hello__ : __hello_alias__',
'__hello__ : <__phello_alias__>',
@@ -103,7 +104,7 @@ FROZEN = [
f'frozen_only : __hello_only__ = {FROZEN_ONLY}',
]),
]
-ESSENTIAL = {
+BOOTSTRAP = {
'importlib._bootstrap',
'importlib._bootstrap_external',
'zipimport',
@@ -527,16 +528,24 @@ def regen_frozen(modules):
header = relpath_for_posix_display(src.frozenfile, parentdir)
headerlines.append(f'#include "{header}"')
- deflines = []
+ bootstraplines = []
+ stdliblines = []
+ testlines = []
aliaslines = []
indent = ' '
lastsection = None
for mod in modules:
- if mod.section != lastsection:
- if lastsection is not None:
- deflines.append('')
- deflines.append(f'/* {mod.section} */')
- lastsection = mod.section
+ if mod.frozenid in BOOTSTRAP:
+ lines = bootstraplines
+ elif mod.section == TESTS_SECTION:
+ lines = testlines
+ else:
+ lines = stdliblines
+ if mod.section != lastsection:
+ if lastsection is not None:
+ lines.append('')
+ lines.append(f'/* {mod.section} */')
+ lastsection = mod.section
symbol = mod.symbol
pkg = '-' if mod.ispkg else ''
@@ -544,11 +553,11 @@ def regen_frozen(modules):
) % (mod.name, symbol, pkg, symbol)
# TODO: Consider not folding lines
if len(line) < 80:
- deflines.append(line)
+ lines.append(line)
else:
line1, _, line2 = line.rpartition(' ')
- deflines.append(line1)
- deflines.append(indent + line2)
+ lines.append(line1)
+ lines.append(indent + line2)
if mod.isalias:
if not mod.orig:
@@ -559,11 +568,13 @@ def regen_frozen(modules):
entry = '{"%s", "%s"},' % (mod.name, mod.orig)
aliaslines.append(indent + entry)
- if not deflines[0]:
- del deflines[0]
- for i, line in enumerate(deflines):
- if line:
- deflines[i] = indent + line
+ for lines in (bootstraplines, stdliblines, testlines):
+ # TODO: Is this necessary any more?
+ if not lines[0]:
+ del lines[0]
+ for i, line in enumerate(lines):
+ if line:
+ lines[i] = indent + line
print(f'# Updating {os.path.relpath(FROZEN_FILE)}')
with updating_file_with_tmpfile(FROZEN_FILE) as (infile, outfile):
@@ -579,9 +590,23 @@ def regen_frozen(modules):
)
lines = replace_block(
lines,
- "static const struct _frozen _PyImport_FrozenModules[] =",
- "/* modules sentinel */",
- deflines,
+ "static const struct _frozen bootstrap_modules[] =",
+ "/* bootstrap sentinel */",
+ bootstraplines,
+ FROZEN_FILE,
+ )
+ lines = replace_block(
+ lines,
+ "static const struct _frozen stdlib_modules[] =",
+ "/* stdlib sentinel */",
+ stdliblines,
+ FROZEN_FILE,
+ )
+ lines = replace_block(
+ lines,
+ "static const struct _frozen test_modules[] =",
+ "/* test sentinel */",
+ testlines,
FROZEN_FILE,
)
lines = replace_block(