Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions .cspell.dict/cpython.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pybuilddir
pycore
pyinner
pydecimal
pyerrors
Pyfunc
pylifecycle
pymain
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ jobs:
uses: ./.github/actions/install-macos-deps

- name: run rust tests
run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }}
run: cargo test --workspace --exclude rustpython-capi ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }}
Comment thread
bschoenmaeckers marked this conversation as resolved.
Resolved
env:
INSTA_WORKSPACE_ROOT: ${{ github.workspace }}

- name: run c-api tests
working-directory: crates/capi
run: cargo test

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
- run: cargo doc --locked
if: runner.os == 'Linux'

Expand Down
16 changes: 5 additions & 11 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ lto = "thin"

[patch.crates-io]
parking_lot_core = { git = "https://github.com/youknowone/parking_lot", branch = "rustpython" }
pyo3-ffi = { git = "https://github.com/PyO3/pyo3" }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
# REDOX START, Uncomment when you want to compile/check with redoxer
# REDOX END

Expand Down Expand Up @@ -256,7 +257,7 @@ pkcs8 = "0.10"
proc-macro2 = "1.0.105"
psm = "0.1"
pymath = { version = "0.2.0", features = ["mul_add", "malachite-bigint", "complex"] }
pyo3 = "0.28"
pyo3 = { git = "https://github.com/PyO3/pyo3" }
quote = "1.0.45"
radium = "1.1.1"
rand = "0.9"
Expand Down
16 changes: 15 additions & 1 deletion crates/capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
#![allow(clippy::missing_safety_doc)]

use crate::pyerrors::init_exception_statics;
use crate::pylifecycle::MAIN_INTERP;
use rustpython_vm::Interpreter;
pub use rustpython_vm::PyObject;
use rustpython_vm::{Context, Interpreter};
use std::sync::MutexGuard;

extern crate alloc;

pub mod object;
pub mod pyerrors;
pub mod pylifecycle;
pub mod pystate;
pub mod refcount;
mod util;

/// Get main interpreter of this process. Will be None if it has not been initialized yet.
pub fn get_main_interpreter() -> MutexGuard<'static, Option<Interpreter>> {
MAIN_INTERP
.lock()
.expect("Failed to lock interpreter mutex")
}

/// Set the main interpreter of this process. This method will panic when there is already an
/// interpreter set.
pub fn set_main_interpreter(interpreter: Interpreter) {
Comment thread
bschoenmaeckers marked this conversation as resolved.
Resolved
let mut interp = get_main_interpreter();
assert!(interp.is_none(), "Main interpreter is already set");
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
unsafe { init_exception_statics(&Context::genesis().exceptions) };
*interp = Some(interpreter);
}
48 changes: 48 additions & 0 deletions crates/capi/src/object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::PyObject;
use crate::pystate::with_vm;
use core::ffi::{c_int, c_uint, c_ulong};
use rustpython_vm::builtins::PyType;
use rustpython_vm::{AsObject, Py};

pub type PyTypeObject = Py<PyType>;

#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject {
unsafe { (*op).class() }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_IS_TYPE(op: *mut PyObject, ty: *mut PyTypeObject) -> c_int {
with_vm(|_vm| {
let obj = unsafe { &*op };
let ty = unsafe { &*ty };
obj.class().is(ty)
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyType_GetFlags(ptr: *const PyTypeObject) -> c_ulong {
let ty = unsafe { &*ptr };
ty.slots.flags.bits() as u32 as c_ulong
}

#[unsafe(no_mangle)]
pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject {
with_vm(|vm| {
let ctx = &vm.ctx;
let constant = match constant_id {
0 => ctx.none.as_object(),
1 => ctx.false_value.as_object(),
2 => ctx.true_value.as_object(),
3 => ctx.ellipsis.as_object(),
4 => ctx.not_implemented.as_object(),
_ => {
return Err(
vm.new_system_error("Invalid constant ID passed to Py_GetConstantBorrowed")
);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
.as_raw();
Ok(constant)
})
}
Loading
Loading