Skip to content

First version of C-API #3327

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ resolver = "2"
members = [
".", "ast", "bytecode", "common", "compiler", "compiler/porcelain",
"derive", "jit", "parser", "vm", "vm/pylib-crate", "stdlib", "wasm/lib",
"capi",
]

[features]
Expand All @@ -26,6 +27,7 @@ pylib = ["rustpython-vm/pylib"]
zlib = ["stdlib", "rustpython-stdlib/zlib"]
ssl = ["rustpython-stdlib/ssl"]
ssl-vendor = ["rustpython-stdlib/ssl-vendor"]
capi = ["rustpython-capi"]

[dependencies]
log = "0.4"
Expand All @@ -35,6 +37,7 @@ rustpython-compiler = { path = "compiler/porcelain", version = "0.1.1" }
rustpython-parser = { path = "parser", version = "0.1.1" }
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compile-parse"] }
rustpython-stdlib = {path = "stdlib", optional = true, default-features = false, features = ["compile-parse"]}
rustpython-capi = { path = "capi", optional = true, default-features = false }
dirs = { package = "dirs-next", version = "2.0.0" }
num-traits = "0.2.8"
cfg-if = "1.0"
Expand Down
10 changes: 10 additions & 0 deletions capi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rustpython-capi"
version = "0.1.2"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2.103"
rustpython-vm = { path = "../vm" }
3 changes: 3 additions & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod object;

use rustpython_vm as vm;
22 changes: 22 additions & 0 deletions capi/src/object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(non_snake_case)]
//! https://docs.python.org/3/c-api/object.html

use crate::vm::{PyObjectPtr, PyObjectRef, PyResult, _with_current_thread_vm};

fn unwrap_pyresult(r: PyResult) -> PyObjectRef {
// TODO: set PyErr
r.expect("TODO: PyErr handling")
}

fn into_raw(o: PyObjectRef) -> *const PyObjectPtr {
PyObjectRef::into_raw(o) as *const _
}

#[no_mangle]
pub unsafe extern "C" fn PyObject_Repr(o: PyObjectPtr) -> *const PyObjectPtr {
into_raw(unwrap_pyresult(
_with_current_thread_vm(&*o, |vm| o.repr(vm))
.expect("TODO: handle vm error")
.map(Into::into),
))
}
1 change: 1 addition & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub use rustpython_bytecode as bytecode;
pub use rustpython_common as common;
#[cfg(feature = "rustpython-compiler")]
pub use rustpython_compiler as compile;
pub use vm::thread::with_vm as _with_current_thread_vm;

#[doc(hidden)]
pub mod __exports {
Expand Down
47 changes: 44 additions & 3 deletions vm/src/pyobjectrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ impl<T> Drop for PyObject<T> {
}
}

type PyObjectRefInner = PyObject<Erased>;

/// The `PyObjectRef` is one of the most used types. It is a reference to a
/// python object. A single python object can have multiple references, and
/// this reference counting is accounted for by this type. Use the `.clone()`
Expand All @@ -130,13 +132,13 @@ impl<T> Drop for PyObject<T> {
#[derive(Clone)]
#[repr(transparent)]
pub struct PyObjectRef {
rc: PyRc<PyObject<Erased>>,
rc: PyRc<PyObjectRefInner>,
}

#[derive(Clone)]
#[repr(transparent)]
pub struct PyObjectWeak {
weak: PyWeak<PyObject<Erased>>,
weak: PyWeak<PyObjectRefInner>,
}

pub trait PyObjectWrap
Expand Down Expand Up @@ -175,7 +177,7 @@ impl PyObjectRef {

fn new<T: PyObjectPayload>(value: PyObject<T>) -> Self {
let inner = PyRc::into_raw(PyRc::new(value));
let rc = unsafe { PyRc::from_raw(inner as *const PyObject<Erased>) };
let rc = unsafe { PyRc::from_raw(inner as *const PyObjectRefInner) };
Self { rc }
}

Expand Down Expand Up @@ -306,6 +308,17 @@ impl PyObjectRef {
None
}
}

#[inline]
pub fn with_ptr<F, R>(&self, f: F) -> R
where
F: FnOnce(PyObjectPtr) -> R,
{
unsafe {
// SAFETY: self will be alive until f is done
f(PyObjectPtr::new(self))
}
}
}

impl AsRef<Self> for PyObjectRef {
Expand Down Expand Up @@ -620,6 +633,34 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef) {
(type_type, object_type)
}

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct PyObjectPtr {
obj: *mut PyObjectRefInner,
}

impl PyObjectPtr {
/// # Safety
///
/// `obj` *MUST* be alive until this ptr is destroyed.
/// Do not directly call this function without helper functions.
unsafe fn new(obj: &PyObjectRef) -> Self {
let obj = std::mem::transmute_copy(obj);
Self { obj }
}
}

impl Deref for PyObjectPtr {
type Target = PyObjectRef;

fn deref(&self) -> &Self::Target {
unsafe {
// SAFETY: only when PyObjectRef = PyRc<PyObjectRefInner>
std::mem::transmute(&self.obj)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down