-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add c-api error handling #7784
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
Add c-api error handling #7784
Changes from all commits
ff5c178
131bb0e
14f3d51
d1984b1
20e75be
e6cfbb1
e276364
9206893
4e88888
3e65cec
0441648
61f4387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,7 @@ Pyfunc | |
| pylifecycle | ||
| pymain | ||
| pyrepl | ||
| pystate | ||
| PYTHONTRACEMALLOC | ||
| PYTHONUTF8 | ||
| pythonw | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,6 +189,7 @@ pycodecs | |
| pycs | ||
| pydatetime | ||
| pyexpat | ||
| PYGILSTATE | ||
| pyio | ||
| pymain | ||
| PYTHONAPI | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,29 @@ | ||
| fn main() { | ||
| if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" { | ||
| println!("cargo:rerun-if-changed=logo.ico"); | ||
| let mut res = winresource::WindowsResource::new(); | ||
| if std::path::Path::new("logo.ico").exists() { | ||
| res.set_icon("logo.ico"); | ||
| } else { | ||
| println!("cargo:warning=logo.ico not found, skipping icon embedding"); | ||
| return; | ||
| let target = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); | ||
| let capi_enabled = std::env::var_os("CARGO_FEATURE_CAPI").is_some(); | ||
|
|
||
| match target.as_str() { | ||
| "linux" if capi_enabled => { | ||
| println!("cargo:rustc-link-arg-bin=rustpython=-Wl,--export-dynamic"); | ||
| } | ||
| res.compile() | ||
| .map_err(|e| { | ||
| println!("cargo:warning=Failed to compile Windows resources: {e}"); | ||
| }) | ||
| .ok(); | ||
| "macos" if capi_enabled => { | ||
| println!("cargo:rustc-link-arg-bin=rustpython=-Wl,-export_dynamic"); | ||
| } | ||
| "windows" => { | ||
| println!("cargo:rerun-if-changed=logo.ico"); | ||
| let mut res = winresource::WindowsResource::new(); | ||
| if std::path::Path::new("logo.ico").exists() { | ||
| res.set_icon("logo.ico"); | ||
| } else { | ||
| println!("cargo:warning=logo.ico not found, skipping icon embedding"); | ||
| return; | ||
| } | ||
| res.compile() | ||
| .map_err(|e| { | ||
| println!("cargo:warning=Failed to compile Windows resources: {e}"); | ||
| }) | ||
| .ok(); | ||
| } | ||
| _ => {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [env] | ||
| PYO3_CONFIG_FILE = { value = "pyo3-rustpython.config", relative = true } | ||
| PYO3_NO_PYTHON = { value = "1" } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [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 = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| rustpython-vm = { workspace = true, features = ["threading"] } | ||
| rustpython-stdlib = {workspace = true, features = ["threading"] } | ||
|
|
||
| [dev-dependencies] | ||
| pyo3 = { version = "0.28", features = ["auto-initialize", "abi3"] } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [package.metadata.cargo-shear] | ||
| # Not a direct dependency (yet), but we need to enable threading support in the stdlib. | ||
| ignored = ["rustpython-stdlib"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| implementation=CPython | ||
| version=3.14 | ||
| shared=true | ||
| abi3=true | ||
| suppress_build_script_link_lines=true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #![allow(clippy::missing_safety_doc)] | ||
|
|
||
| use crate::pyerrors::init_exception_statics; | ||
| use crate::pylifecycle::MAIN_INTERP; | ||
| 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) { | ||
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 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, Context, Py}; | ||
|
|
||
| const PY_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; | ||
| const PY_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25; | ||
| const PY_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; | ||
| const PY_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27; | ||
| const PY_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; | ||
| const PY_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29; | ||
| const PY_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30; | ||
| const PY_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; | ||
|
|
||
| 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 ctx = Context::genesis(); | ||
| let zoo = &ctx.types; | ||
| let exp_zoo = &ctx.exceptions; | ||
|
|
||
| let ty = unsafe { &*ptr }; | ||
| let mut flags = ty.slots.flags.bits(); | ||
|
|
||
| if ty.is_subtype(zoo.int_type) { | ||
| flags |= PY_TPFLAGS_LONG_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.list_type) { | ||
| flags |= PY_TPFLAGS_LIST_SUBCLASS | ||
| } | ||
| if ty.is_subtype(zoo.tuple_type) { | ||
| flags |= PY_TPFLAGS_TUPLE_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.bytes_type) { | ||
| flags |= PY_TPFLAGS_BYTES_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.str_type) { | ||
| flags |= PY_TPFLAGS_UNICODE_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.dict_type) { | ||
| flags |= PY_TPFLAGS_DICT_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(exp_zoo.base_exception_type) { | ||
| flags |= PY_TPFLAGS_BASE_EXC_SUBCLASS; | ||
| } | ||
| if ty.is_subtype(zoo.type_type) { | ||
| flags |= PY_TPFLAGS_TYPE_SUBCLASS; | ||
| } | ||
|
|
||
| flags | ||
| } | ||
|
Comment on lines
+32
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Find the underlying repr of slots.flags.
ast-grep --pattern 'pub struct PyTypeFlags: $_'
rg -nP --type=rust -C3 'bitflags!\s*\{[^}]*PyTypeFlags'
rg -nP --type=rust -C2 'pub flags:\s*PyTypeFlags'Repository: RustPython/RustPython Length of output: 331 🏁 Script executed: #!/bin/bash
# Find PyTypeFlags definition and its underlying type
rg -n "struct PyTypeFlags|bitflags.*PyTypeFlags" crates/vm/src/types/slot.rs -A 20 -B 2Repository: RustPython/RustPython Length of output: 994 🏁 Script executed: #!/bin/bash
# Find PY_TPFLAGS_* constants definition
rg -n "PY_TPFLAGS_LONG_SUBCLASS|PY_TPFLAGS_LIST_SUBCLASS" --type=rust -B 2 -A 2Repository: RustPython/RustPython Length of output: 932 🏁 Script executed: #!/bin/bash
# Check what type bits() returns and what c_ulong is in context
rg -n "c_ulong|c_uint" crates/capi/src/object.rs -B 2 -A 2 | head -40Repository: RustPython/RustPython Length of output: 1078 Add explicit cast of
Cast the result to let mut flags = ty.slots.flags.bits() as c_ulong;High bits are not lost since the constants use shifts 1 << 24 through 1 << 31 (within 32 bits), and the function return type is already 🤖 Prompt for AI Agents |
||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject { | ||
| with_vm(|vm| { | ||
| let ctx = &vm.ctx; | ||
| 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(), | ||
| _ => panic!("Invalid constant_id passed to Py_GetConstantBorrowed"), | ||
| } | ||
| .as_raw() | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.