Skip to content

Add import support to c-api#7881

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-import
May 16, 2026
Merged

Add import support to c-api#7881
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-import

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented May 16, 2026

Add import support to the c-api. The test is currently disabled until we have enough api exposed to make it compile.

Summary by CodeRabbit

  • New Features
    • Added Python module import functionality to the C API, allowing import operations through the external C interface.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

📝 Walkthrough

Walkthrough

Introduces PyImport_Import, a new C-ABI FFI entry point for the RustPython capi that accepts a PyObject representing an import name, downcasts it to PyStr, and invokes the VM's import mechanism. The module is exported from the crate root with a disabled test included.

Changes

PyImport_Import C-ABI Export

Layer / File(s) Summary
PyImport_Import FFI function
crates/capi/src/import.rs
Implements the unsafe C-ABI exported PyImport_Import function that accepts a raw PyObject pointer, downcasts it to PyStr using the active VM, and calls vm.import(name, 0) to perform the import.
Module export and test integration
crates/capi/src/lib.rs, crates/capi/src/import.rs
Exports the new import module from lib.rs and adds a disabled test module validating basic import of sys.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through FFI dreams,
Importing with Pythonic schemes,
PyImport_Import takes its bow,
To let C and Rust mingle now! ✨

🚥 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 import support to c-api' directly summarizes the main change: introducing a new PyImport_Import C API function with supporting module exports.
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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/capi/src/import.rs`:
- Around line 5-8: The FFI function PyImport_Import dereferences the incoming
pointer name without a null check, causing UB; before the unsafe block that does
(&*name).try_downcast_ref::<PyStr>(vm) add a guard if name.is_null() { return
std::ptr::null_mut(); } (or set an appropriate Python error on the VM and then
return std::ptr::null_mut()) so the function returns a null pointer on invalid
input; update the PyImport_Import implementation (the with_vm closure and the
place where try_downcast_ref and vm.import are called) to early-return when name
is null rather than dereferencing it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 55deddf3-1835-45db-91d4-7de4cf550570

📥 Commits

Reviewing files that changed from the base of the PR and between 0871bc8 and c5008bb.

📒 Files selected for processing (2)
  • crates/capi/src/import.rs
  • crates/capi/src/lib.rs

Comment thread crates/capi/src/import.rs
Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

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

overall lgtm:)

Comment thread crates/capi/src/import.rs
Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

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

Just read the PR description...

@youknowone youknowone merged commit a364f86 into RustPython:main May 16, 2026
25 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-import branch May 16, 2026 18:29
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