The sys.modules fast path at lines 3313-3331 runs before the base-module computation at 3333-3360 and looks up name directly in d->lz_from.
In the IMPORT_FROM chain generated for import a.b.c as x, however, d->lz_from does not appear to be the module the attribute should be taken from: on the first step it is the full dotted name, and on later steps it is one component short of the base.
@grgalex In the test please create a package structure to be imported, don't rely on importlib.metadata because that API can change. We should test the pattern, not something that happens to accidentally be an instance of it today.
Activity
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
johnslavik commented on Sep 16, 2026
Thanks for reporting the issue, I've reproduced it.
You've opened a PR in your fork repo, not in CPython.
grgalex commented on Sep 16, 2026
@johnslavik Thanks for reproducing!
Ahhh the PR on the fork references the issue so it appears here. Was testing some stuff!
I'll submit the associated PR once it's ready (soon!) :)
johnslavik commented on Sep 16, 2026
@grgalex In the test please create a package structure to be imported, don't rely on importlib.metadata because that API can change. We should test the pattern, not something that happens to accidentally be an instance of it today.
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
pythongh-157614: Make the lazy import subprocess test helper reusable
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
11 remaining items
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
pythongh-157614: Make the lazy import subprocess test helper reusable
pythongh-157614: Fix lazy import a.b as c binding a.b.b instead of th…
grgalex commented on Sep 16, 2026
@johnslavik I've opened the PR.
Next time I'll make sure not to link to the main issue during experimentation in the private fork to avoid polluting the issue with references!
StanFromIreland commented on Sep 16, 2026
Marking as a release-blocker to get Hugo's eyes on this. This seems pretty bad, and seems like a realistic scenario to hit in practice.
grgalex commented on Sep 18, 2026
Another bug found while investigating the present issue: #157757
hugovk commented on Sep 28, 2026
Fixed by #158092, removing release-blocker label. Keeping this issue open for now to decide if we want the test from #157626.