Skip to content

Add stdlib to c-api#8133

Open
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:capi-stdlib
Open

Add stdlib to c-api#8133
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:capi-stdlib

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Enhanced Python interpreter initialization with improved standard library module discovery and automatic module path configuration
    • Better module accessibility and runtime reliability during interpreter operations
  • Tests

    • Added test coverage validating standard library module imports work correctly
  • Chores

    • Updated core dependencies with additional feature flags for improved module support
    • Refined build configuration for better overall system stability

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 854e8f09-43e8-4092-b43e-f70fda9cf006

📥 Commits

Reviewing files that changed from the base of the PR and between fe2a7db and 85c7e2f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • crates/capi/Cargo.toml
  • crates/capi/src/import.rs
  • crates/capi/src/pylifecycle.rs

📝 Walkthrough

Walkthrough

The C API crate gains rustpython-pylib as a direct dependency and enables importlib/host_env features on rustpython-vm. Py_InitializeEx is updated to use the interpreter builder pattern, registering native stdlib modules and configuring stdlib_dir and module_search_paths from rustpython_pylib::LIB_PATH. A disabled unit test for stdlib module import is also added.

Changes

C API stdlib initialization

Layer / File(s) Summary
Cargo dependency additions
crates/capi/Cargo.toml
Adds importlib and host_env feature flags to rustpython-vm and introduces rustpython-pylib as a new direct dependency.
Builder-based interpreter init with stdlib path setup
crates/capi/src/pylifecycle.rs
Imports PyRc and replaces Interpreter::with_init in Py_InitializeEx with a builder flow that registers native module definitions and installs an init_hook setting stdlib_dir and inserting rustpython_pylib::LIB_PATH at index 0 of module_search_paths.
Disabled stdlib import test
crates/capi/src/import.rs
Adds import_stdlib test function inside the existing #[cfg(false)] test module that attaches to the Python runtime and imports the types module.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • RustPython/RustPython#7648: Modifies the same Py_InitializeEx initialization lifecycle in pylifecycle.rs, adding the C-API singleton and lifecycle hooks that this PR builds on.
  • RustPython/RustPython#7787: Also extends Py_InitializeEx in pylifecycle.rs with first-init setup logic (C-API exception statics), directly adjacent to the changed region.
  • RustPython/RustPython#7881: Modifies crates/capi/src/import.rs by adding PyImport_Import, the same file where this PR adds the disabled stdlib import test.

Suggested reviewers

  • ShaharNaveh
  • youknowone

Poem

🐇 Hippity hoppity, paths are now set,
LIB_PATH is added so modules are met.
The builder hops in where with_init once stood,
Native modules registered, all working as they should.
The stdlib is findable — hooray, that's good! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add stdlib to c-api' directly describes the main objective of the pull request, which adds standard library support to the C API across configuration, initialization, and testing.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

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.

2 participants