mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
bpo-45020: Identify which frozen modules are actually aliases. (gh-28655)
In the list of generated frozen modules at the top of Tools/scripts/freeze_modules.py, you will find that some of the modules have a different name than the module (or .py file) that is actually frozen. Let's call each case an "alias". Aliases do not come into play until we get to the (generated) list of modules in Python/frozen.c. (The tool for freezing modules, Programs/_freeze_module, is only concerned with the source file, not the module it will be used for.) Knowledge of which frozen modules are aliases (and the identity of the original module) normally isn't important. However, this information is valuable when we go to set __file__ on frozen stdlib modules. This change updates Tools/scripts/freeze_modules.py to map aliases to the original module name (or None if not a stdlib module) in Python/frozen.c. We also add a helper function in Python/import.c to look up a frozen module's alias and add the result of that function to the frozen info returned from find_frozen(). https://bugs.python.org/issue45020
This commit is contained in:
parent
444429142c
commit
08285d563e
10 changed files with 225 additions and 37 deletions
|
@ -274,6 +274,15 @@ class FrozenSource(namedtuple('FrozenSource', 'id pyfile frozenfile')):
|
|||
name = self.frozenid.replace('.', '_')
|
||||
return '_Py_M__' + name
|
||||
|
||||
@property
|
||||
def ispkg(self):
|
||||
if not self.pyfile:
|
||||
return False
|
||||
elif self.frozenid.endswith('.__init__'):
|
||||
return False
|
||||
else:
|
||||
return os.path.basename(self.pyfile) == '__init__.py'
|
||||
|
||||
|
||||
def resolve_frozen_file(frozenid, destdir=MODULES_DIR):
|
||||
"""Return the filename corresponding to the given frozen ID.
|
||||
|
@ -305,6 +314,17 @@ class FrozenModule(namedtuple('FrozenModule', 'name ispkg section source')):
|
|||
def modname(self):
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def orig(self):
|
||||
return self.source.modname
|
||||
|
||||
@property
|
||||
def isalias(self):
|
||||
orig = self.source.modname
|
||||
if not orig:
|
||||
return True
|
||||
return self.name != orig
|
||||
|
||||
def summarize(self):
|
||||
source = self.source.modname
|
||||
if source:
|
||||
|
@ -507,6 +527,7 @@ def regen_frozen(modules):
|
|||
headerlines.append(f'#include "{header}"')
|
||||
|
||||
deflines = []
|
||||
aliaslines = []
|
||||
indent = ' '
|
||||
lastsection = None
|
||||
for mod in modules:
|
||||
|
@ -528,6 +549,15 @@ def regen_frozen(modules):
|
|||
deflines.append(line1)
|
||||
deflines.append(indent + line2)
|
||||
|
||||
if mod.isalias:
|
||||
if not mod.orig:
|
||||
entry = '{"%s", NULL},' % (mod.name,)
|
||||
elif mod.source.ispkg:
|
||||
entry = '{"%s", "<%s"},' % (mod.name, mod.orig)
|
||||
else:
|
||||
entry = '{"%s", "%s"},' % (mod.name, mod.orig)
|
||||
aliaslines.append(indent + entry)
|
||||
|
||||
if not deflines[0]:
|
||||
del deflines[0]
|
||||
for i, line in enumerate(deflines):
|
||||
|
@ -549,10 +579,17 @@ def regen_frozen(modules):
|
|||
lines = replace_block(
|
||||
lines,
|
||||
"static const struct _frozen _PyImport_FrozenModules[] =",
|
||||
"/* sentinel */",
|
||||
"/* modules sentinel */",
|
||||
deflines,
|
||||
FROZEN_FILE,
|
||||
)
|
||||
lines = replace_block(
|
||||
lines,
|
||||
"const struct _module_alias aliases[] =",
|
||||
"/* aliases sentinel */",
|
||||
aliaslines,
|
||||
FROZEN_FILE,
|
||||
)
|
||||
outfile.writelines(lines)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue