summaryrefslogtreecommitdiff
path: root/Tools
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-10-05 11:26:37 -0600
committerGitHub <noreply@github.com>2021-10-05 11:26:37 -0600
commit08285d563e64c179a56ab2f952345b3dbcdb54f3 (patch)
tree7bc95d1865dcb6c29f835044e678fd9ebbe85904 /Tools
parent444429142c88b7e22117a6e14c95d1fbdd638c60 (diff)
downloadcpython-git-08285d563e64c179a56ab2f952345b3dbcdb54f3.tar.gz
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
Diffstat (limited to 'Tools')
-rw-r--r--Tools/scripts/freeze_modules.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/Tools/scripts/freeze_modules.py b/Tools/scripts/freeze_modules.py
index 6091d831a8..36e284100e 100644
--- a/Tools/scripts/freeze_modules.py
+++ b/Tools/scripts/freeze_modules.py
@@ -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)