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
This commit is contained in:
Eric Snow 2021-10-28 15:04:33 -06:00 committed by GitHub
parent 66e6b3dcd3
commit 074fa57506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 219 additions and 129 deletions

View file

@ -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(