We were unable to construct the commit group for this pull request: None of the pull request's commits have been successfully processed as yet.

gh-152298: Reify each lazy import at most once #157730

Open
brittanyrey opened 10:49pm on September 17, 2026 wants to merge 0 commits into python/cpython main from
b-gh152298-lazy-resolve

Pull Request Overview

  • Opened on September 18, 2026
  • Status Open
  • Commit count 0

Total Delta

0 Total Diff Delta

Open Days

Open 0 weekdays

Test Delta

0 Diff Delta in Test Files
Breakdown by Phase

How long has this pull request spent in each phase of its lifecycle?

Data pending calculation for pull request

Author avatar

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.