Skip to content

Add minimal C-api implementation that builds with Pyo3 - #7562

Draft
bschoenmaeckers wants to merge 201 commits into
RustPython:mainfrom
bschoenmaeckers:c-api
Draft

bschoenmaeckers wants to merge 201 commits into
RustPython:mainfrom
bschoenmaeckers:c-api

Conversation

@bschoenmaeckers

Copy link
Copy Markdown
Contributor

This is my shot at implementing a minimal Cpython compatible C-api. I've implemented the bare minimum to get the included Pyo3 example running where most of the api is stubbed. I'm not familiar with the rest of the RustPython code base so let me know what you think and where I did stupid things.

Please take extra care reviewing the pylifecycle.rs & pystate.rs files where I try to setup the RustPython interpreter.

xref #5604

@coderabbitai

coderabbitai Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
✨ 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.

@youknowone

Copy link
Copy Markdown
Member

Could you try with hpy if you dont mind? We are looking for sustainable way

@bschoenmaeckers

Copy link
Copy Markdown
Contributor Author

Could you try with hpy if you dont mind? We are looking for sustainable way

That would require significant changes in codebase that are using the CPython api spec. I would like to explore the possibility to use the ab3/abi3t api. Would you be willing to accept that?

@youknowone youknowone left a comment

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.

This way will not work.

Just in case, the code can be generated, but I hope we carefully review the decisions. unlikely-to-happen decisions must be reviewed and justified by document(comment) the decisions.
If you are not familiar enough to the project to decide good way, please start from a smaller and simpler issue.

We only need abi3t, because we are compatible to free-threading.

We have to minimize the surface of C API. if HPy helps it, we need HPy.

Ideally c api must be very thin wrappers to RustPython features. "code" must not be included in this crate.

Comment thread crates/capi/src/object.rs Outdated
Comment thread crates/capi/src/pystate.rs Outdated
@bschoenmaeckers

Copy link
Copy Markdown
Contributor Author

Ideally c api must be very thin wrappers to RustPython features. "code" must not be included in this crate.

I wanted to create as little of modificaties to the existing code base without significant motivation. But I definitely agree with you.

Comment thread crates/capi/src/pylifecycle.rs
@bschoenmaeckers
bschoenmaeckers force-pushed the c-api branch 4 times, most recently from 039e64e to 9d6aa5c Compare July 7, 2026 19:51
@codspeed

codspeed Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 67.16%

⚡ 2 improved benchmarks
✅ 64 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
gc_collect.py[rustpython] 2,407 ms 994.3 ms ×2.4
gc_traversal.py[rustpython] 827.7 ms 717.1 ms +15.42%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing bschoenmaeckers:c-api (4b642b2) with main (da69430)

Open in CodSpeed

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.

4 participants