Skip to content
Merged
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
1 change: 1 addition & 0 deletions crates/capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod pyerrors;
pub mod pylifecycle;
pub mod pystate;
pub mod refcount;
pub mod traceback;
pub mod tupleobject;
pub mod unicodeobject;
mod util;
Expand Down
51 changes: 49 additions & 2 deletions crates/capi/src/pyerrors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::PyObject;
use crate::pystate::with_vm;
use crate::object::define_py_check;
use crate::{PyObject, pystate::with_vm};
use core::convert::Infallible;
use core::ffi::{CStr, c_char, c_int};
use core::ptr::NonNull;
Expand Down Expand Up @@ -96,6 +96,8 @@ define_exception_statics! {
PyExc_EncodingWarning => encoding_warning,
}

define_py_check!(fn PyExceptionInstance_Check, exceptions.base_exception_type);

#[unsafe(no_mangle)]
pub extern "C" fn PyErr_Occurred() -> *mut PyObject {
with_vm(|vm| {
Expand Down Expand Up @@ -191,6 +193,15 @@ pub unsafe extern "C" fn PyErr_WriteUnraisable(obj: *mut PyObject) {
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyExceptionClass_Check(obj: *mut PyObject) -> c_int {
with_vm(|vm| unsafe {
obj.as_ref()
.and_then(|obj| obj.downcast_ref::<PyType>())
.is_some_and(|ty| ty.is_subtype(vm.ctx.exceptions.base_exception_type))
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyErr_NewException(
name: *const c_char,
Expand Down Expand Up @@ -252,6 +263,42 @@ pub unsafe extern "C" fn PyErr_GivenExceptionMatches(
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyException_GetTraceback(exc: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let exc = unsafe { &*exc }.try_downcast_ref::<PyBaseException>(vm)?;
let tb = exc
.__traceback__()
.map(|tb| tb.into_object().into_raw().as_ptr())
.unwrap_or_default();
Ok(tb)
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyException_GetCause(exc: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let exc = unsafe { &*exc }.try_downcast_ref::<PyBaseException>(vm)?;
let cause = exc
.__cause__()
.map(|cause| cause.into_object().into_raw().as_ptr())
.unwrap_or_default();
Ok(cause)
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyException_GetContext(exc: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let exc = unsafe { &*exc }.try_downcast_ref::<PyBaseException>(vm)?;
let context = exc
.__context__()
.map(|context| context.into_object().into_raw().as_ptr())
.unwrap_or_default();
Ok(context)
})
}
Comment thread
bschoenmaeckers marked this conversation as resolved.

#[cfg(test)]
mod tests {
use pyo3::exceptions::PyTypeError;
Expand Down
24 changes: 24 additions & 0 deletions crates/capi/src/traceback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::PyObject;
use crate::object::define_py_check;
use crate::pystate::with_vm;
use core::ffi::c_int;
use rustpython_vm::function::{FuncArgs, KwArgs};

define_py_check!(exact fn PyTraceBack_Check, types.traceback_type);

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyTraceBack_Print(tb: *mut PyObject, file: *mut PyObject) -> c_int {
with_vm(|vm| {
let tb = unsafe { &*tb };
let file = unsafe { &*file };
let tb_module = vm.import("traceback", 0)?;
let print_tb = tb_module.get_attr("print_tb", vm)?;

let kwargs: KwArgs = [("file".to_string(), file.to_owned())]
.into_iter()
.collect();
print_tb.call(FuncArgs::new(vec![tb.to_owned()], kwargs), vm)?;

Ok(())
})
}
Loading