Skip to content

gh-152298: Replace lazy import globals on resolve#152524

Draft
1sgtpepper wants to merge 14 commits into
python:mainfrom
1sgtpepper:gh-152298-lazy-resolve-minimal
Draft

gh-152298: Replace lazy import globals on resolve#152524
1sgtpepper wants to merge 14 commits into
python:mainfrom
1sgtpepper:gh-152298-lazy-resolve-minimal

Conversation

@1sgtpepper

@1sgtpepper 1sgtpepper commented Jun 28, 2026

Copy link
Copy Markdown

Fixes gh-152298.

LazyImportType.resolve() now caches successful lazy reification and finalizes the recorded original module global before the lazy import path releases the import lock. The original global is replaced only when the recorded key still points at the same lazy proxy, so aliases, deleted globals, and rebound globals are not retargeted.

This prevents resolve() and later normal global access, or competing reification callers, from importing the same lazy proxy more than once.

Regression tests cover the reported custom __import__ case, from-import bindings, rebound and deleted globals, copied proxy aliases, retry after failure, and repeated resolve() on copied proxies.

Validation:

  • git diff --check
  • python3 Tools/cases_generator/tier1_generator.py -o <tmp> Python/bytecodes.c and compared output with Python/generated_cases.c.h
  • python3 Tools/cases_generator/tier2_generator.py -o <tmp> Python/bytecodes.c and compared output with Python/executor_cases.c.h
  • python3 Tools/cases_generator/tier1_generator.py --test -o <tmp> Python/bytecodes.c and compared output with Modules/_testinternalcapi/test_cases.c.h
  • ./python -m test test_lazy_import

@python-cla-bot

python-cla-bot Bot commented Jul 4, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LazyImportType.resolve() does not replace the module global and can import twice

1 participant