Visitar URL original
Add the eager-runtime hook required by Python 3.15 importlib by youknowdot · Pull Request #8946 · RustPython/RustPython · GitHub
Skip to content

Add the eager-runtime hook required by Python 3.15 importlib - #8946

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:cpython-315-imp-lazy-attributes
Oct 5, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:cpython-315-imp-lazy-attributes

Conversation

@youknowdot

@youknowdot youknowdot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add _imp._set_lazy_attributes(modobj, name, /), the private hook required by CPython 3.15 importlib after module loading.

In CPython v3.15.0rc2, this hook discards name from the pending lazy-module registry. RustPython currently executes imports eagerly and has no pending lazy entries. The implementation therefore validates the arguments and preserves string-subclass hashing (including exceptions), without modifying the module object.

This is a small native prerequisite for the target/site/importlib/test-support migration. It does not implement PEP 810 lazy imports. When a real lazy-module registry is introduced, this hook must remove the corresponding pending entry.

Only _imp.rs and one regression snippet change. The target version, bundled libraries, compiler, sentinel builtin, and cached import callback remain unchanged. The callback adjustment stays with the importlib migration, whose hierarchical-lock changes are needed for the concurrent-import cases.

Validation

Hook-only branch at 29d5ba99196b4a41c48946ee90ab762d5affa739:

  • Release build with SQLite enabled: passed
  • Source-isolation probe confirms unchanged Python 3.14 target, branch-local site/importlib/test.support, and the original cached import callback; no sentinel or compile(module=...) code is present
  • New hook snippet passes on RustPython and exact CPython 3.15.0rc2, including invalid argument types, positional-only use, string subclasses, surrogate names, and hash exceptions
  • -m test test_site test_support test_importlib test_pkgutil test_import --timeout 180: 1,676 run, 113 skipped; all five suites successful
  • Full bounded snippet aggregate: 469 passed, 13 failed. All failures match the unchanged environment/stress cases: ten AF_UNIX socket permission failures, two chown errno differences, and one CPython 3.14 GC/import timeout. RustPython's new hook case and GC/import stress case pass
  • Configured commit hooks and diff checks pass

The identical hook code also passed full Rust validation in the broader native-import candidate e2f1ed22366d258f4352e5ffaccebefa0bbfe467: workspace 1,345 passed/18 ignored, separate C-API 115 passed/4 ignored, and both all-targets Clippy commands clean. Those full Rust aggregates were not rerun after dropping the separate cached-importer change; the smaller branch was independently rebuilt and runtime-tested as above. No full Python 3.15 compatibility claim is made.

The snippet follows the existing skip_if_unsupported convention: every assertion always runs on RustPython; only the CPython comparison below 3.15 skips this unavailable API. It also runs separately against exact CPython 3.15.0rc2, commit 435c9e5a798c99653e3ab64ce29baed0e4f3dfee.

AI assistance

OpenAI Codex assisted with implementation, CPython comparison, tests, review, and this description under maintainer direction. The runtime did not expose the exact model version. Reported checks are automated Linux x86_64 verification; no additional human-use or untested-platform validation is claimed.

Summary by CodeRabbit

  • New Features
    • Added support for _imp._set_lazy_attributes, allowing callers to set lazy-attribute metadata in eager-import environments. The operation returns None for string names and reports errors for invalid names or hashing failures.
  • Tests
    • Added coverage for accepted arguments, invalid name types, string subclasses, and propagated hashing errors.

Expose _imp._set_lazy_attributes(modobj, name, /), used by CPython 3.15 importlib after eager loading. Match its empty pending-lazy-module registry behavior, including name type and string-subclass hashing checks. This does not implement lazy imports or change the target version or bundled libraries.

Assisted-by: Codex:model-version-unavailable
@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 07f30dcb-e7ef-43e1-b6d2-b5aca17c06c6
📥 Commits

Reviewing files that changed from the base of the PR and between ada43c4 and b6acc6a.

📒 Files selected for processing (2)
  • crates/vm/src/stdlib/_imp.rs
  • extra_tests/snippets/stdlib_imp.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The _imp module adds _set_lazy_attributes, which hashes the supplied name and returns None on success. Tests cover argument handling and hash behavior for Python 3.15.

Changes

_imp lazy attribute compatibility

Layer / File(s) Summary
Add and test lazy attribute setter
crates/vm/src/stdlib/_imp.rs, extra_tests/snippets/stdlib_imp.py
_set_lazy_attributes hashes the name and returns success. Tests check argument handling, hashing, and propagated errors. The test is registered for Python 3.15.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to b6acc

This adds a small compatibility hook so CPython 3.15 importlib code can call into RustPython. It does not change existing import behavior. No merge-blocking risk is evident.

Security Architecture Review

Security architecture risk: ⚪ Minimal · up to b6acc

The new hook uses existing hashing behavior without itself changing module attributes or import state. The reviewed change introduces no material security risk or new authority boundary.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The added execution path is confined to the calling interpreter and caller-supplied hash behavior. The native body adds no filesystem, network, credential, cross-tenant, or persistent-state operation.

Trust Boundaries and Controls

  • inferred — A caller-controlled string subclass can run its hash implementation and raise exceptions. This is ordinary Python callback execution through existing hash dispatch, not a new privileged execution context or bypass of a module-authorization check.

Resilience and Maintainability Implications

  • inferred — The native hook creates no reservation, registry entry, or module mutation before or after hashing. Consequently, failure or re-entry cannot strand a hook-owned partial registration requiring cleanup. Hash callbacks may mutate their own reachable state; the hook does not promise to roll back those effects.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the addition of the eager-runtime hook for Python 3.15 importlib, which is the pull request’s main change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this also in CPython 3.15's Lib/test directory? If so, it will eventually be migrated over to RustPython when RustPython begins targeting CPython 3.15.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, need to be checked

Move the distinct direct-hook argument and hashing contracts into the existing _imp snippet and remove the separate file. Preserve every pre-existing assertion and the CPython version guard; canonical 3.15 lazy-import tests already cover feature-level registry and reification behavior.

Assisted-by: Codex:model-version-unavailable
@youknowone
youknowone marked this pull request as ready for review October 5, 2026 07:51
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@youknowone
youknowone enabled auto-merge (squash) October 5, 2026 07:51
@youknowone
youknowone merged commit 04d9990 into RustPython:main Oct 5, 2026
29 checks passed
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.

3 participants