Skip to content

gh-158140: Resolve lazily imported sibling submodules independently - #158521

Draft
pablogsal wants to merge 9 commits into
python:mainfrom
pablogsal:fix-158140-independent-submodules
Draft

pablogsal wants to merge 9 commits into
python:mainfrom
pablogsal:fix-158140-independent-submodules

Conversation

@pablogsal

@pablogsal pablogsal commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #158140. With lazy import pkg.a followed by lazy import pkg.b, accessing pkg.a currently imports pkg.b first. Resolve the package and each pending child independently, while preserving assigned package attributes, import hooks, invalid-import errors and retries after failed imports.

This is stacked on #158282 at ad34faea95935c9cedfb940930e3c377322a4e09. Its commits are included until that PR lands. The fix-only diff shows the changes for this issue.

Keep the placeholder structure private to lazyimportobject.c. Move the
resolution and attribute lookup code there so it can use the structure
without exposing its fields to the eval loop or import code.
Let importlib handle module locks instead of holding the global import
lock while resolving a placeholder. Keep active placeholders in a set on
the thread and remove them on every exit, including allocation failures.

Use the same cycle and recursion checks when a hook returns a placeholder.
Keep the source references alive until all attribute lookups finish.
Keep an existing cause or context when resolution fails. Add the import
location as a note in that case, without adding the same note twice.
Errors without an existing chain still get the declaration as their cause.
Only reuse a concrete attribute from a module that has finished loading.
Leave lazy attributes for resolution so module hooks still get a chance.
Check the module again after reading its spec, and keep interrupts visible.
Normalize the fromlist before calling the filter and check its items
before registering imports. Use one cleanup path and the existing filter
accessor. Pass the same five arguments to custom lazy hooks as to __import__.
Resolve lazy __getattr__ and __dir__ hooks before calling them. Treat a
hook already being resolved as unavailable so it can import a sibling.

Bind a loaded child before removing its pending entry. Recheck the module
dict when another thread may have completed the load. Reuse modules already
in sys.modules during package cycles, and simplify child registration.
Remember the namespace where lookup found the placeholder and use one
helper to resolve and replace it. Only replace a binding that still holds
the same placeholder, preserving assignments and deletions during import.

Check and replace ordinary dict entries atomically. Keep the mapping
protocol for custom namespaces and allow reads from readonly namespaces.
Reuse the global lookup helper in the eval loop and remove the duplicate.
Loading a child can replace the parent binding with the module before we fetch the imported attribute. Keep the module returned by the normal importer so the shared helper can replace it in its actual parent namespace. Other values and deletions still prevent replacement, and custom hooks keep control of their assignments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

Accessing one lazily imported submodule (lazy import pkg.a) also imports a later imported pkg.b

2 participants