gh-155695: Remove resolved names from sys.lazy_modules more consistently - #157714
Conversation
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
@pablogsal Addressed comments. |
|
cc @encukou |
|
Thanks! The initializing-module case is covered now. I left one follow-up comment about the new |
Names only left sys.lazy_modules through _imp._set_lazy_attributes(), which the import machinery calls from _find_and_load_unlocked(). Two cases never reached it, so their names were recorded and then kept forever: - A lazy import of a module already in sys.modules. _find_and_load() returns early, so nothing ever discards the name. Do not record it in the first place. - The "pkg.attr" entry for `lazy from pkg import attr`. The import machinery only discards module names, and attr is often not a module. Discard it when the lazy object is reified, where the name is already known and has been resolved either way. Submodules that are not yet loaded are still tracked: loading a package does not load its submodules, so those imports can still fire. Names whose reification failed also stay tracked, since the import can still happen.
A module is in sys.modules while its body runs, so lazy_modules_add() counted it as loaded and skipped recording the name. If the body then raises, the module is removed from sys.modules again and the lazy import is left pending under no name at all. Check __spec__._initializing so such a module does not count as loaded. A name added while a module initializes is still discarded once the import completes, since _set_lazy_attributes() runs after the body.
374d85f to
44ebbaa
Compare
|
🤖 New build scheduled with the buildbot fleet by @hugovk for commit ad36b56 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157714%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit ad36b56 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157714%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
|
Added a rebase @encukou |
|
Thank you! You got roughly to the same place I did, but faster as I keep double-checking. Enjoy your PTO! Should I push into this PR, or do you want to continue working on it? |
encukou
left a comment
There was a problem hiding this comment.
LGTM. I left some suggestions at brittanyrey#1; feel free to merge or cherry-pick if those look good.
The PR can be slimmed down by removing the _PyImport_DiscardLazyModule indirection, but keeping all uses of LAZY_MODULES in import.c has its own advantages.
|
🤖 New build scheduled with the buildbot fleet by @hugovk for commit 95198ab 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F157714%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
| } | ||
| assert(obj == NULL || !PyLazyImport_CheckExact(obj)); | ||
| if (obj != NULL) { | ||
| PyObject *name = lazy_import_name(lz); |
There was a problem hiding this comment.
If #158521 is merged first, this will change to lazy_import_path since only the root will be resolved here:
| PyObject *name = lazy_import_name(lz); | |
| PyObject *name = lazy_import_path(lz); |
|
Buildbot failures are pre-existing/unrelated. I've pushed my suggestions here, except human-friendly display name which will be obsolete if #158521 is merged. |
|
Thank you @encukou! |
|
Thanks @brittanyrey for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-158607 is a backport of this pull request to the 3.15 branch. |
…nsistently (GH-157714) (#158607) gh-155695: Remove resolved names from sys.lazy_modules more consistently (GH-157714) (cherry picked from commit be87a85) Co-authored-by: Brittany Reynoso <breynoso@meta.com> Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
summary
Improve
sys.lazy_modulesto address the following issues:sys.lazy_modules.lazy from pkg import attris cleaned up after reification.perf
End-to-end (hyperfine,
--warmup 50, 1000 runs; 400 for reify-only)-X lazy_imports=allapp