Skip to content

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

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

Add minimal C-api implementation that builds with Pyo3#7562
bschoenmaeckers wants to merge 160 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
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 5, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: da2a53cf-b4df-4d20-877c-6046500e8d6e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ 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.

@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?

Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

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
Comment thread crates/capi/Cargo.toml
pyo3 = { workspace = true, features = ["auto-initialize", "abi3"] }

[features]
# Enable PyObject_CallMethodObjArgs variadic function, which is only available on nightly Rust.
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.

Suggested change
# Enable PyObject_CallMethodObjArgs variadic function, which is only available on nightly Rust.
# Enable PyObject_CallMethodObjArgs variadic function, which is only available on nightly Rust.
# TODO: remove this when the `c_variadic` feature is stabilized (https://github.com/rust-lang/rust/issues/44930)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As this will be stabilised very soon. I was planning to removing this feature entirely before merging this.

@bschoenmaeckers
Copy link
Copy Markdown
Contributor Author

@youknowone Small update on the current process:

The basic API surface has now been implemented and merged. This allows us to implement and run tests for the most commonly used Python types and functions.

What remains is the implementation of the less commonly used abi3 functions, along with module and type creation support. The former should be relatively straightforward and can be implemented by me, someone else, or even an AI agent. The latter is more challenging.

Current TODO list of unimplemented C API functions in PyO3

Click here for the full list of straightforward TODO functions

Unimplemented C API functions

Mapping source: pyo3-ffi/src/*.rs, which mirrors the CPython header split used by the C API.

abstract.h

RustPython C API target: crates/capi/src/abstract_.rs

  • PyIter_Check
  • PyIter_NextItem
  • PyIter_Send
  • PyMapping_Items
  • PyMapping_Keys
  • PyMapping_Size
  • PyMapping_Values
  • PyNumber_Add
  • PyNumber_Lshift
  • PyNumber_Or
  • PyNumber_Rshift
  • PyNumber_Subtract
  • PyObject_GetIter
  • PyObject_Size
  • PySequence_Check
  • PySequence_Concat
  • PySequence_Count
  • PySequence_DelItem
  • PySequence_DelSlice
  • PySequence_GetItem
  • PySequence_GetSlice
  • PySequence_InPlaceConcat
  • PySequence_InPlaceRepeat
  • PySequence_Index
  • PySequence_List
  • PySequence_Repeat
  • PySequence_SetItem
  • PySequence_SetSlice
  • PySequence_Size
  • PySequence_Tuple

bytearrayobject.h

RustPython C API target: crates/capi/src/bytearrayobject.rs (not present yet)

  • PyByteArray_AsString
  • PyByteArray_Check
  • PyByteArray_FromObject
  • PyByteArray_FromStringAndSize
  • PyByteArray_Resize
  • PyByteArray_Size

descrobject.h

RustPython C API target: crates/capi/src/descrobject.rs (not present yet)

  • PyDictProxy_New

dictobject.h

RustPython C API target: crates/capi/src/dictobject.rs

  • PyDict_Contains
  • PyDict_Copy
  • PyDict_DelItem
  • PyDict_Items
  • PyDict_Keys
  • PyDict_Merge
  • PyDict_MergeFromSeq2
  • PyDict_Update
  • PyDict_Values

genericaliasobject.h

RustPython C API target: crates/capi/src/genericaliasobject.rs (not present yet)

  • Py_GenericAlias

import.h

RustPython C API target: crates/capi/src/import.rs

  • PyImport_ExecCodeModuleEx

listobject.h

RustPython C API target: crates/capi/src/listobject.rs

  • PyList_AsTuple
  • PyList_GetSlice
  • PyList_SetSlice
  • PyList_Sort

longobject.h

RustPython C API target: crates/capi/src/longobject.rs

  • PyLong_AsUnsignedLongLongMask

moduleobject.h

RustPython C API target: crates/capi/src/moduleobject.rs

  • PyModule_GetFilenameObject
  • PyModule_NewObject

object.h

RustPython C API target: crates/capi/src/object.rs

  • PyCallable_Check
  • PyObject_ClearWeakRefs
  • PyObject_Dir
  • PyObject_GenericGetAttr
  • PyObject_GetOptionalAttr
  • PyObject_RichCompare
  • PyType_GetModuleName

osmodule.h

RustPython C API target: crates/capi/src/osmodule.rs (not present yet)

  • PyOS_FSPath

pybuffer.h

RustPython C API target: crates/capi/src/pybuffer.rs (not present yet)

  • PyBuffer_FromContiguous
  • PyBuffer_GetPointer
  • PyBuffer_IsContiguous
  • PyBuffer_Release
  • PyBuffer_ToContiguous
  • PyObject_GetBuffer

pyerrors.h

RustPython C API target: crates/capi/src/pyerrors.rs

  • PyException_SetContext
  • PyUnicodeDecodeError_Create

pystate.h

RustPython C API target: crates/capi/src/pystate.rs

  • PyInterpreterState_Get
  • PyInterpreterState_GetID

setobject.h

RustPython C API target: crates/capi/src/setobject.rs (not present yet)

  • PyFrozenSet_Check
  • PyFrozenSet_New
  • PySet_Add
  • PySet_Check
  • PySet_Clear
  • PySet_Contains
  • PySet_Discard
  • PySet_New
  • PySet_Pop
  • PySet_Size

sliceobject.h

RustPython C API target: crates/capi/src/sliceobject.rs (not present yet)

  • PySlice_AdjustIndices
  • PySlice_New
  • PySlice_Unpack

unicodeobject.h

RustPython C API target: crates/capi/src/unicodeobject.rs

  • PyUnicode_AsUTF8String
  • PyUnicode_DecodeFSDefaultAndSize
  • PyUnicode_EncodeFSDefault
  • PyUnicode_FromEncodedObject

warnings.h

RustPython C API target: crates/capi/src/warnings.rs (not present yet)

  • PyErr_WarnEx
  • PyErr_WarnExplicit

weakrefobject.h

RustPython C API target: crates/capi/src/weakrefobject.rs (not present yet)

  • PyWeakref_CheckProxy
  • PyWeakref_CheckRef
  • PyWeakref_GetRef
  • PyWeakref_NewProxy
  • PyWeakref_NewRef

Module and type creation

The abi3 APIs currently available in Python 3.14 are not compatible with abi3t and expose too much of CPython's internal implementation details. In this MR, I hacked together a working solution, but it is not something we would want to merge.

Python 3.15 will introduce new abi3 APIs that are much more compatible with RustPython and should allow us to implement this functionality in a robust and maintainable way.

Please see PEP 820 and PEP 793 for the APIs we will need to implement.

@youknowone
Copy link
Copy Markdown
Member

@JamesClarke7283 would you like to take the straight-forward abi3 functions using AI?

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