-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add minimal C-api implementation that builds with Pyo3 #7562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bschoenmaeckers
wants to merge
12
commits into
RustPython:main
Choose a base branch
from
bschoenmaeckers:c-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
54156f6
export c api
bschoenmaeckers 2250a1c
type mapping v1
bschoenmaeckers bc96211
Type mapping v2
bschoenmaeckers 46307f6
Implement type flags
bschoenmaeckers b08d41a
Implement `PyUnicode_FromStringAndSize`
bschoenmaeckers 34c13f4
Do not use `C-unwind`
bschoenmaeckers 24b2c63
implement `PyLong_AsLong`
bschoenmaeckers 384b378
Add `with_vm` helper
bschoenmaeckers f49e859
Move `PyThreadState` to `pystate.rs`
bschoenmaeckers e18eef7
Basic multi-threading support
bschoenmaeckers 565bf5e
Cleanup
bschoenmaeckers 85b2928
Attach thread in `Py_InitializeEx`
bschoenmaeckers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "rustpython-capi" | ||
| description = "Minimal CPython C-API compatibility exports for RustPython" | ||
| version.workspace = true | ||
| authors.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| repository.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [lib] | ||
| crate-type = ["staticlib"] | ||
|
|
||
| [dependencies] | ||
| rustpython-vm = { workspace = true, features = ["threading"]} | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| use core::ffi::c_char; | ||
| use core::ptr; | ||
|
|
||
| use crate::PyObject; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyBytes_Size(_bytes: *mut PyObject) -> isize { | ||
| crate::log_stub("PyBytes_Size"); | ||
| 0 | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyBytes_AsString(_bytes: *mut PyObject) -> *mut c_char { | ||
| crate::log_stub("PyBytes_AsString"); | ||
| ptr::null_mut() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use core::ptr; | ||
|
|
||
| use crate::PyObject; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyImport_Import(_name: *mut PyObject) -> *mut PyObject { | ||
| crate::log_stub("PyImport_Import"); | ||
| ptr::null_mut() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| pub use rustpython_vm::PyObject; | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| pub mod bytesobject; | ||
| pub mod import; | ||
| pub mod longobject; | ||
| pub mod object; | ||
| pub mod pyerrors; | ||
| pub mod pylifecycle; | ||
| pub mod pystate; | ||
| pub mod refcount; | ||
| pub mod traceback; | ||
| pub mod tupleobject; | ||
| pub mod unicodeobject; | ||
|
|
||
| #[inline] | ||
| pub(crate) fn log_stub(name: &str) { | ||
| eprintln!("[rustpython-capi stub] {name} called"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use core::ffi::{c_long, c_longlong, c_ulong, c_ulonglong}; | ||
| use core::ptr; | ||
|
|
||
| use crate::PyObject; | ||
| use crate::pystate::with_vm; | ||
| use rustpython_vm::PyObjectRef; | ||
| use rustpython_vm::builtins::PyInt; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromLong(value: c_long) -> *mut PyObject { | ||
| with_vm(|vm| { | ||
| let obj: PyObjectRef = vm.ctx.new_int(value).into(); | ||
| obj.into_raw().as_ptr() | ||
| }) | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromLongLong(_value: c_longlong) -> *mut PyObject { | ||
| crate::log_stub("PyLong_FromLongLong"); | ||
| ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromSsize_t(_value: isize) -> *mut PyObject { | ||
| crate::log_stub("PyLong_FromSsize_t"); | ||
| ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromSize_t(_value: usize) -> *mut PyObject { | ||
| crate::log_stub("PyLong_FromSize_t"); | ||
| ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromUnsignedLong(_value: c_ulong) -> *mut PyObject { | ||
| crate::log_stub("PyLong_FromUnsignedLong"); | ||
| ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_FromUnsignedLongLong(_value: c_ulonglong) -> *mut PyObject { | ||
| crate::log_stub("PyLong_FromUnsignedLongLong"); | ||
| ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyLong_AsLong(obj: *mut PyObject) -> c_long { | ||
| if obj.is_null() { | ||
| panic!("PyLong_AsLong called with null object"); | ||
| } | ||
|
|
||
| with_vm(|_vm| { | ||
| // SAFETY: non-null checked above; caller promises a valid PyObject pointer. | ||
| let obj_ref = unsafe { &*obj }; | ||
| let int_obj = obj_ref | ||
| .downcast_ref::<PyInt>() | ||
| .expect("PyLong_AsLong currently only accepts int instances"); | ||
|
|
||
| int_obj | ||
| .as_bigint() | ||
| .try_into() | ||
| .expect("PyLong_AsLong: value out of range for c_long") | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| use core::ffi::c_ulong; | ||
|
|
||
| use crate::PyObject; | ||
| use rustpython_vm::builtins::PyType; | ||
| use rustpython_vm::{AsObject, Context, Py}; | ||
| use std::sync::LazyLock; | ||
|
|
||
| pub struct PyTypeObject { | ||
| ty: LazyLock<&'static Py<PyType>>, | ||
| flags: c_ulong, | ||
| } | ||
|
|
||
| impl PyTypeObject { | ||
| const fn new(f: fn() -> &'static Py<PyType>, flags: c_ulong) -> PyTypeObject { | ||
| PyTypeObject { | ||
| ty: LazyLock::new(f), | ||
| flags, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const PY_TPFLAGS_HAVE_STACKLESS_EXTENSION: c_ulong = 0; | ||
| const PY_TPFLAGS_HAVE_VERSION_TAG: c_ulong = 1 << 18; | ||
| const PY_TPFLAGS_DEFAULT: c_ulong = | ||
| PY_TPFLAGS_HAVE_STACKLESS_EXTENSION | PY_TPFLAGS_HAVE_VERSION_TAG; | ||
| const PY_TPFLAGS_IMMUTABLETYPE: c_ulong = 1 << 8; | ||
| const PY_TPFLAGS_BASETYPE: c_ulong = 1 << 10; | ||
| const PY_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; | ||
| const PY_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; | ||
| const PY_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; | ||
| const PY_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub static mut PyType_Type: PyTypeObject = PyTypeObject::new( | ||
| || { | ||
| let zoo = &Context::genesis().types; | ||
| zoo.type_type | ||
| }, | ||
| PY_TPFLAGS_DEFAULT | PY_TPFLAGS_IMMUTABLETYPE | PY_TPFLAGS_BASETYPE | PY_TPFLAGS_TYPE_SUBCLASS, | ||
| ); | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub static mut PyLong_Type: PyTypeObject = PyTypeObject::new( | ||
| || { | ||
| let zoo = &Context::genesis().types; | ||
| zoo.int_type | ||
| }, | ||
| PY_TPFLAGS_DEFAULT | PY_TPFLAGS_IMMUTABLETYPE | PY_TPFLAGS_BASETYPE | PY_TPFLAGS_LONG_SUBCLASS, | ||
| ); | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub static mut PyTuple_Type: PyTypeObject = PyTypeObject::new( | ||
| || { | ||
| let zoo = &Context::genesis().types; | ||
| zoo.tuple_type | ||
| }, | ||
| PY_TPFLAGS_DEFAULT | PY_TPFLAGS_IMMUTABLETYPE | PY_TPFLAGS_BASETYPE | PY_TPFLAGS_TUPLE_SUBCLASS, | ||
| ); | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub static mut PyUnicode_Type: PyTypeObject = PyTypeObject::new( | ||
| || { | ||
| let zoo = &Context::genesis().types; | ||
| zoo.str_type | ||
| }, | ||
| PY_TPFLAGS_DEFAULT | ||
| | PY_TPFLAGS_IMMUTABLETYPE | ||
| | PY_TPFLAGS_BASETYPE | ||
| | PY_TPFLAGS_UNICODE_SUBCLASS, | ||
| ); | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn Py_TYPE(op: *mut PyObject) -> *mut PyTypeObject { | ||
| if op.is_null() { | ||
| return std::ptr::null_mut(); | ||
| } | ||
|
|
||
| unsafe { | ||
| let ty = (*op).class(); | ||
| if ty.is(*PyType_Type.ty) { | ||
| &raw mut PyType_Type | ||
| } else if ty.is(*PyLong_Type.ty) { | ||
| &raw mut PyLong_Type | ||
| } else if ty.is(*PyTuple_Type.ty) { | ||
| &raw mut PyTuple_Type | ||
| } else if ty.is(*PyUnicode_Type.ty) { | ||
| &raw mut PyUnicode_Type | ||
| } else { | ||
| todo!("Unsupported type: {:?}", ty.name()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| #[allow(clippy::not_unsafe_ptr_arg_deref)] | ||
| pub extern "C" fn PyType_GetFlags(ty: *mut PyTypeObject) -> c_ulong { | ||
| if ty.is_null() { | ||
| panic!("PyType_GetFlags called with null type pointer"); | ||
| } | ||
|
|
||
| // SAFETY: caller guarantees this is a valid exported type object pointer. | ||
| unsafe { (*ty).flags } | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyType_GetName(_ty: *mut PyTypeObject) -> *mut PyObject { | ||
| crate::log_stub("PyType_GetName"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyType_GetQualName(_ty: *mut PyTypeObject) -> *mut PyObject { | ||
| crate::log_stub("PyType_GetQualName"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyObject_CallNoArgs(_callable: *mut PyObject) -> *mut PyObject { | ||
| crate::log_stub("PyObject_CallNoArgs"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyObject_GetAttr(_obj: *mut PyObject, _name: *mut PyObject) -> *mut PyObject { | ||
| crate::log_stub("PyObject_GetAttr"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyObject_Repr(_obj: *mut PyObject) -> *mut PyObject { | ||
| crate::log_stub("PyObject_Repr"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyObject_Str(_obj: *mut PyObject) -> *mut PyObject { | ||
| crate::log_stub("PyObject_Str"); | ||
| std::ptr::null_mut() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn Py_GetConstantBorrowed(_constant_id: core::ffi::c_uint) -> *mut PyObject { | ||
| crate::log_stub("Py_GetConstantBorrowed"); | ||
| std::ptr::null_mut() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause significant performance draw down.
PyType also already has flags inside.
Will you create all the new type wrapper for every static types? That will not work.