8000 Identify if the aliased module is a package or not. · python/cpython@7d23290 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d23290

Browse files
Identify if the aliased module is a package or not.
1 parent 223a786 commit 7d23290

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Lib/test/test_importlib/frozen/test_finder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_module(self):
9595
'__phello__.ham.__init__',
9696
]
9797
for name in modules:
98-
origname = name.rpartition('.')[0]
98+
origname = '<' + name.rpartition('.')[0]
9999
filename = resolve_stdlib_file(name)
100100
with self.subTest(f'{name} -> {origname}'):
101101
spec = self.find(name)

Python/frozen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ static const struct _module_alias aliases[] = {
113113
{"__hello_alias__", "__hello__"},
114114
{"__phello_alias__", "__hello__"},
115115
{"__phello_alias__.spam", "__hello__"},
116-
{"__phello__.__init__", "__phello__"},
117-
{"__phello__.ham.__init__", "__phello__.ham"},
118-
{"__hello_only__", ""},
116+
{"__phello__.__init__", "<__phello__"},
117+
{"__phello__.ham.__init__", "<__phello__.ham"},
118+
{"__hello_only__", NULL},
119119
{0, 0} /* aliases sentinel */
120120
};
121121
const struct _module_alias *_PyImport_FrozenAliases = aliases;

Tools/scripts/freeze_modules.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ def symbol(self):
274274
name = self.frozenid.replace('.', '_')
275275
return '_Py_M__' + name
276276

277+
@property
278+
def ispkg(self):
279+
if not self.pyfile:
280+
return False
281+
elif self.frozenid.endswith('.__init__'):
282+
return False
283+
else:
284+
return os.path.basename(self.pyfile) == '__init__.py'
285+
277286

278287
def resolve_frozen_file(frozenid, destdir=MODULES_DIR):
279288
"""Return the filename corresponding to the given frozen ID.
@@ -541,7 +550,12 @@ def regen_frozen(modules):
541550
deflines.append(indent + line2)
542551

543552
if mod.isalias:
544-
entry = '{"%s", "%s"},' % (mod.name, mod.orig or "")
553+
if not mod.orig:
554+
entry = '{"%s", NULL},' % (mod.name,)
555+
elif mod.source.ispkg:
556+
entry = '{"%s", "<%s"},' % (mod.name, mod.orig)
557+
else:
558+
entry = '{"%s", "%s"},' % (mod.name, mod.orig)
545559
aliaslines.append(indent + entry)
546560

547561
if not deflines[0]:

0 commit comments

Comments
 (0)
0