Skip to content

Commit

Permalink
Use PyErr_GetRaisedException(), PyErr_SetRaisedException()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jul 7, 2023
1 parent 6babe0f commit 464d0b8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ fn raise_dumps_exception_fixed(msg: &str) -> *mut PyObject {
#[cold]
#[inline(never)]
#[cfg_attr(feature = "optimize", optimize(size))]
#[cfg(Py_3_12)]
fn raise_dumps_exception_dynamic(err: &String) -> *mut PyObject {
unsafe {
let cause_exc: *mut PyObject = PyErr_GetRaisedException();

let err_msg =
PyUnicode_FromStringAndSize(err.as_ptr() as *const c_char, err.len() as isize);
PyErr_SetObject(typeref::JsonEncodeError, err_msg);
Py_DECREF(err_msg);

if !cause_exc.is_null() {
let exc: *mut PyObject = PyErr_GetRaisedException();
PyException_SetCause(exc, cause_exc);
PyErr_SetRaisedException(exc);
}
};
null_mut()
}

#[cold]
#[inline(never)]
#[cfg_attr(feature = "optimize", optimize(size))]
#[cfg(not(Py_3_12))]
fn raise_dumps_exception_dynamic(err: &String) -> *mut PyObject {
unsafe {
let mut cause_tp: *mut PyObject = null_mut();
Expand Down

0 comments on commit 464d0b8

Please sign in to comment.