Pull Request Overview
- Opened on September 18, 2026
- Status Open
- Commit count 0
Total Delta
Open Days
Test Delta
How long has this pull request spent in each phase of its lifecycle?
Data pending calculation for pull request
gh-152298: Reify each lazy import at most once
The bug
A lazy import proxy kept no record of having been reified, so every reification ran __import__ again. LazyImportType.resolve() followed by a plain load of the global still bound to that proxy imported twice and bound two different objects. The same happened for a proxy copied into another namespace, and for LOAD_FROM_DICT_OR_GLOBALS, which reifies without writing the result back. A single lazy binding could reach four __import__ calls.
The fix
Cache the resolved object on the proxy and hand it back on later reifications, under the import lock that already serialises this path. Rebinding the global instead is not possible: a proxy has no unique binding, because reification keys off the value's type at each load rather than off the name it was stored under. A failed reification is not cached, so the next access retries the import.
Testing
Output against the repro in the linked issue.
Before:
resolved: value-1
global after resolve: lazy_import
direct: value-2
global after direct: module
calls: [(1, 'target_module', '__main__', None), (2, 'target_module', '__main__', None)]
After:
resolved: value-1
global after resolve: lazy_import
direct: value-1
global after direct: module
calls: [(1, 'target_module', '__main__', None)]
cc: @DinoV, @pablogsal
- Issue: gh-152298
<!-- /gh-issue-number -->
Comments Threads Pending Resolution
Resolved Comment Threads
No resolved comments have been left on this PR.