use crate::err::{PyErr, PyResult};
use crate::exceptions::PyOverflowError;
use crate::ffi::{self, Py_hash_t};
use crate::IntoPyPointer;
use crate::{IntoPy, PyObject, Python};
use std::isize;
use std::os::raw::c_int;
pub trait PyCallbackOutput: Copy {
const ERR_VALUE: Self;
}
impl PyCallbackOutput for *mut ffi::PyObject {
const ERR_VALUE: Self = std::ptr::null_mut();
}
impl PyCallbackOutput for std::os::raw::c_int {
const ERR_VALUE: Self = -1;
}
impl PyCallbackOutput for ffi::Py_ssize_t {
const ERR_VALUE: Self = -1;
}
impl PyCallbackOutput for () {
const ERR_VALUE: Self = ();
}
pub trait IntoPyCallbackOutput<Target> {
fn convert(self, py: Python) -> PyResult<Target>;
}
impl<T, E, U> IntoPyCallbackOutput<U> for Result<T, E>
where
T: IntoPyCallbackOutput<U>,
E: Into<PyErr>,
{
fn convert(self, py: Python) -> PyResult<U> {
self.map_err(Into::into).and_then(|t| t.convert(py))
}
}
impl<T> IntoPyCallbackOutput<*mut ffi::PyObject> for T
where
T: IntoPy<PyObject>,
{
fn convert(self, py: Python) -> PyResult<*mut ffi::PyObject> {
Ok(self.into_py(py).into_ptr())
}
}
impl IntoPyCallbackOutput<Self> for *mut ffi::PyObject {
fn convert(self, _: Python) -> PyResult<Self> {
Ok(self)
}
}
impl IntoPyCallbackOutput<std::os::raw::c_int> for () {
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
Ok(0)
}
}
impl IntoPyCallbackOutput<std::os::raw::c_int> for bool {
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
Ok(self as c_int)
}
}
impl IntoPyCallbackOutput<()> for () {
fn convert(self, _: Python) -> PyResult<()> {
Ok(())
}
}
impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
#[inline]
fn convert(self, _py: Python) -> PyResult<ffi::Py_ssize_t> {
if self <= (isize::MAX as usize) {
Ok(self as isize)
} else {
Err(PyOverflowError::new_err(()))
}
}
}
impl IntoPyCallbackOutput<bool> for bool {
fn convert(self, _: Python) -> PyResult<bool> {
Ok(self)
}
}
impl IntoPyCallbackOutput<usize> for usize {
fn convert(self, _: Python) -> PyResult<usize> {
Ok(self)
}
}
impl<T> IntoPyCallbackOutput<PyObject> for T
where
T: IntoPy<PyObject>,
{
fn convert(self, py: Python) -> PyResult<PyObject> {
Ok(self.into_py(py))
}
}
pub trait WrappingCastTo<T> {
fn wrapping_cast(self) -> T;
}
macro_rules! wrapping_cast {
($from:ty, $to:ty) => {
impl WrappingCastTo<$to> for $from {
#[inline]
fn wrapping_cast(self) -> $to {
self as $to
}
}
};
}
wrapping_cast!(u8, Py_hash_t);
wrapping_cast!(u16, Py_hash_t);
wrapping_cast!(u32, Py_hash_t);
wrapping_cast!(usize, Py_hash_t);
wrapping_cast!(u64, Py_hash_t);
wrapping_cast!(i8, Py_hash_t);
wrapping_cast!(i16, Py_hash_t);
wrapping_cast!(i32, Py_hash_t);
wrapping_cast!(isize, Py_hash_t);
wrapping_cast!(i64, Py_hash_t);
pub struct HashCallbackOutput(Py_hash_t);
impl IntoPyCallbackOutput<Py_hash_t> for HashCallbackOutput {
#[inline]
fn convert(self, _py: Python) -> PyResult<Py_hash_t> {
let hash = self.0;
if hash == -1 {
Ok(-2)
} else {
Ok(hash)
}
}
}
impl<T> IntoPyCallbackOutput<HashCallbackOutput> for T
where
T: WrappingCastTo<Py_hash_t>,
{
#[inline]
fn convert(self, _py: Python) -> PyResult<HashCallbackOutput> {
Ok(HashCallbackOutput(self.wrapping_cast()))
}
}
#[doc(hidden)]
#[inline]
pub fn convert<T, U>(py: Python, value: T) -> PyResult<U>
where
T: IntoPyCallbackOutput<U>,
{
value.convert(py)
}
#[doc(hidden)]
#[inline]
pub fn callback_error<T>() -> T
where
T: PyCallbackOutput,
{
T::ERR_VALUE
}
#[doc(hidden)]
#[macro_export]
macro_rules! callback_body {
($py:ident, $body:expr) => {{
$crate::callback_body_without_convert!($py, $crate::callback::convert($py, $body))
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! callback_body_without_convert {
($py:ident, $body:expr) => {{
let pool = $crate::GILPool::new();
let unwind_safe_py = std::panic::AssertUnwindSafe(pool.python());
let result = match std::panic::catch_unwind(move || -> $crate::PyResult<_> {
let $py = *unwind_safe_py;
$body
}) {
Ok(result) => result,
Err(e) => {
if let Some(string) = e.downcast_ref::<String>() {
Err($crate::panic::PanicException::new_err((string.clone(),)))
} else if let Some(s) = e.downcast_ref::<&str>() {
Err($crate::panic::PanicException::new_err((s.to_string(),)))
} else {
Err($crate::panic::PanicException::new_err((
"panic from Rust code",
)))
}
}
};
result.unwrap_or_else(|e| {
e.restore(pool.python());
$crate::callback::callback_error()
})
}};
}