diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index b282c3aa..975b0574 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -221,8 +221,7 @@ fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>) -> TokenStream { #body - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } diff --git a/pyo3-derive-backend/src/pymethod.rs b/pyo3-derive-backend/src/pymethod.rs index 1eade4e5..0449c204 100644 --- a/pyo3-derive-backend/src/pymethod.rs +++ b/pyo3-derive-backend/src/pymethod.rs @@ -115,8 +115,7 @@ fn impl_wrap_common( pyo3::derive_utils::IntoPyResult::into_py_result(#body) }; - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } else { @@ -138,8 +137,7 @@ fn impl_wrap_common( #body - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } @@ -169,8 +167,7 @@ pub fn impl_proto_wrap(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream { #body - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } @@ -237,8 +234,7 @@ pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream { #body - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } @@ -267,8 +263,7 @@ pub fn impl_wrap_static(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream { #body - pyo3::callback::cb_convert( - pyo3::callback::PyObjectCallbackConverter, _py, _result) + pyo3::callback::cb_obj_convert(_py, _result) } } } diff --git a/src/callback.rs b/src/callback.rs index 8061aeea..5abec052 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -10,53 +10,62 @@ use crate::{IntoPy, PyObject, Python}; use std::os::raw::c_int; use std::{isize, ptr}; -pub trait CallbackConverter { - type R; +/// Convert the result of callback function into the appropriate return value. +pub trait CallbackConverter { + type Source; + type Result; + const ERR_VALUE: Self::Result; - fn convert(s: S, p: Python) -> Self::R; - fn error_value() -> Self::R; -} - -pub struct PyObjectCallbackConverter; - -impl CallbackConverter for PyObjectCallbackConverter -where - S: IntoPy, -{ - type R = *mut ffi::PyObject; - - fn convert(val: S, py: Python) -> *mut ffi::PyObject { - val.into_py(py).into_ptr() - } + fn convert(s: Self::Source, py: Python) -> Self::Result; #[inline] - fn error_value() -> *mut ffi::PyObject { - ptr::null_mut() + fn convert_result(py: Python, value: PyResult) -> Self::Result { + match value { + Ok(val) => Self::convert(val, py), + Err(e) => { + e.restore(py); + Self::ERR_VALUE + } + } + } +} + +pub struct PyObjectCallbackConverter(pub std::marker::PhantomData); + +impl CallbackConverter for PyObjectCallbackConverter +where + T: IntoPy, +{ + type Source = T; + type Result = *mut ffi::PyObject; + const ERR_VALUE: Self::Result = ptr::null_mut(); + + fn convert(s: Self::Source, py: Python) -> Self::Result { + s.into_py(py).into_ptr() } } pub struct BoolCallbackConverter; -impl CallbackConverter for BoolCallbackConverter { - type R = c_int; +impl CallbackConverter for BoolCallbackConverter { + type Source = bool; + type Result = c_int; + const ERR_VALUE: Self::Result = -1; #[inline] - fn convert(val: bool, _py: Python) -> c_int { - val as c_int - } - - #[inline] - fn error_value() -> c_int { - -1 + fn convert(s: Self::Source, _py: Python) -> Self::Result { + s as c_int } } pub struct LenResultConverter; -impl CallbackConverter for LenResultConverter { - type R = isize; +impl CallbackConverter for LenResultConverter { + type Source = usize; + type Result = isize; + const ERR_VALUE: Self::Result = -1; - fn convert(val: usize, py: Python) -> isize { + fn convert(val: Self::Source, py: Python) -> Self::Result { if val <= (isize::MAX as usize) { val as isize } else { @@ -64,27 +73,19 @@ impl CallbackConverter for LenResultConverter { -1 } } - - #[inline] - fn error_value() -> isize { - -1 - } } pub struct UnitCallbackConverter; -impl CallbackConverter<()> for UnitCallbackConverter { - type R = c_int; +impl CallbackConverter for UnitCallbackConverter { + type Source = (); + type Result = c_int; + const ERR_VALUE: Self::Result = -1; #[inline] - fn convert(_: (), _: Python) -> c_int { + fn convert(_s: Self::Source, _py: Python) -> Self::Result { 0 } - - #[inline] - fn error_value() -> c_int { - -1 - } } pub trait WrappingCastTo { @@ -112,13 +113,15 @@ wrapping_cast!(i32, Py_hash_t); wrapping_cast!(isize, Py_hash_t); wrapping_cast!(i64, Py_hash_t); -pub struct HashConverter; +pub struct HashConverter(pub std::marker::PhantomData); -impl CallbackConverter for HashConverter +impl CallbackConverter for HashConverter where T: WrappingCastTo, { - type R = Py_hash_t; + type Source = T; + type Result = Py_hash_t; + const ERR_VALUE: Self::Result = -1; #[inline] fn convert(val: T, _py: Python) -> Py_hash_t { @@ -129,23 +132,30 @@ where hash } } +} - #[inline] - fn error_value() -> Py_hash_t { - -1 - } +// Short hands methods for macros +#[inline] +pub fn cb_convert(_c: C, py: Python, value: PyResult) -> C::Result +where + C: CallbackConverter, +{ + C::convert_result(py, value) } #[inline] -pub unsafe fn cb_convert(_c: C, py: Python, value: PyResult) -> C::R -where - C: CallbackConverter, -{ - match value { - Ok(val) => C::convert(val, py), - Err(e) => { - e.restore(py); - C::error_value() - } - } +pub fn cb_obj_convert>( + py: Python, + value: PyResult, +) -> as CallbackConverter>::Result { + PyObjectCallbackConverter::::convert_result(py, value) +} + +#[inline] +pub unsafe fn cb_err(_c: C, py: Python, err: impl Into) -> C::Result +where + C: CallbackConverter, +{ + err.into().restore(py); + C::ERR_VALUE } diff --git a/src/class/basic.rs b/src/class/basic.rs index 5d8e60e5..cb920aee 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -233,8 +233,13 @@ where let slf = py.from_borrowed_ptr::>(slf); let arg = py.from_borrowed_ptr::(arg); - let result = call_ref!(slf, __getattr__, arg); - crate::callback::cb_convert(PyObjectCallbackConverter, py, result) + call_ref_with_converter!( + slf, + PyObjectCallbackConverter::(std::marker::PhantomData), + py, + __getattr__, + arg + ) } Some(wrap::) } @@ -355,8 +360,7 @@ where py_unary_func!( PyObjectStrProtocol, T::__str__, - ::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -380,8 +384,7 @@ where py_unary_func!( PyObjectReprProtocol, T::__repr__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -444,8 +447,7 @@ where py_unary_func!( PyObjectHashProtocol, T::__hash__, - isize, - HashConverter, + HashConverter::(std::marker::PhantomData), ffi::Py_hash_t ) } @@ -470,7 +472,6 @@ where py_unary_func!( PyObjectBoolProtocol, T::__bool__, - bool, BoolCallbackConverter, c_int ) diff --git a/src/class/descr.rs b/src/class/descr.rs index 80313dfe..61c2e51c 100644 --- a/src/class/descr.rs +++ b/src/class/descr.rs @@ -87,8 +87,7 @@ where py_ternary_func!( PyDescrGetProtocol, T::__get__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -109,13 +108,7 @@ where T: for<'p> PyDescrSetProtocol<'p>, { fn tp_descr_set() -> Option { - py_ternary_func!( - PyDescrSetProtocol, - T::__set__, - (), - UnitCallbackConverter, - c_int - ) + py_ternary_func!(PyDescrSetProtocol, T::__set__, UnitCallbackConverter, c_int) } } diff --git a/src/class/iter.rs b/src/class/iter.rs index d347b65d..c6d69361 100644 --- a/src/class/iter.rs +++ b/src/class/iter.rs @@ -80,8 +80,7 @@ where py_unary_refmut_func!( PyIterIterProtocol, T::__iter__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -108,21 +107,22 @@ where py_unary_refmut_func!( PyIterNextProtocol, T::__next__, - Option, - IterNextConverter + IterNextConverter::(std::marker::PhantomData) ) } } -struct IterNextConverter; +struct IterNextConverter(std::marker::PhantomData); -impl CallbackConverter> for IterNextConverter +impl CallbackConverter for IterNextConverter where T: IntoPy, { - type R = *mut ffi::PyObject; + type Source = Option; + type Result = *mut ffi::PyObject; + const ERR_VALUE: Self::Result = ptr::null_mut(); - fn convert(val: Option, py: Python) -> *mut ffi::PyObject { + fn convert(val: Self::Source, py: Python) -> Self::Result { match val { Some(val) => val.into_py(py).into_ptr(), None => unsafe { @@ -131,9 +131,4 @@ where }, } } - - #[inline] - fn error_value() -> *mut ffi::PyObject { - ptr::null_mut() - } } diff --git a/src/class/macros.rs b/src/class/macros.rs index 3ba31880..124860d0 100644 --- a/src/class/macros.rs +++ b/src/class/macros.rs @@ -3,24 +3,23 @@ #[macro_export] #[doc(hidden)] macro_rules! py_unary_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => { + ($trait:ident, $class:ident :: $f:ident, $conv: expr) => { + py_unary_func!($trait, $class::$f, $conv, *mut $crate::ffi::PyObject); + }; + // Use call_ref! by default + ($trait:ident, $class:ident :: $f:ident, $conv: expr, $ret_type:ty) => { py_unary_func!( $trait, $class::$f, - $res_type, $conv, - *mut $crate::ffi::PyObject + $ret_type, + call_ref_with_converter ); }; - // Use call_ref! by default - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr, $ret_type:ty) => { - py_unary_func!($trait, $class::$f, $res_type, $conv, $ret_type, call_ref); - }; - ($trait:ident, + ($trait: ident, $class:ident :: $f:ident, - $res_type:ty, - $conv:expr, - $ret_type:ty, + $conv: expr, + $ret_type: ty, $call: ident ) => {{ unsafe extern "C" fn wrap(slf: *mut $crate::ffi::PyObject) -> $ret_type @@ -30,8 +29,7 @@ macro_rules! py_unary_func { let py = $crate::Python::assume_gil_acquired(); let _pool = $crate::GILPool::new(py); let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); - let res = $call!(slf, $f); - $crate::callback::cb_convert($conv, py, res.map(|x| x)) + $call!(slf, $conv, py, $f) } Some(wrap::<$class>) }}; @@ -40,7 +38,7 @@ macro_rules! py_unary_func { #[macro_export] #[doc(hidden)] macro_rules! py_unary_refmut_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => {{ unsafe extern "C" fn wrap(slf: *mut $crate::ffi::PyObject) -> *mut $crate::ffi::PyObject where T: for<'p> $trait<'p>, @@ -76,20 +74,14 @@ macro_rules! py_len_func { #[macro_export] #[doc(hidden)] macro_rules! py_binary_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => { - py_binary_func!( - $trait, - $class::$f, - $res_type, - $conv, - *mut $crate::ffi::PyObject - ) + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => { + py_binary_func!($trait, $class::$f, $conv, *mut $crate::ffi::PyObject) }; // Use call_ref! by default - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr, $return:ty) => {{ - py_binary_func!($trait, $class::$f, $res_type, $conv, $return, call_ref) + ($trait:ident, $class:ident :: $f:ident, $conv:expr, $return:ty) => {{ + py_binary_func!($trait, $class::$f, $conv, $return, call_ref_with_converter) }}; - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr, $return:ty, $call:ident) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr, $return:ty, $call:ident) => {{ unsafe extern "C" fn wrap(slf: *mut ffi::PyObject, arg: *mut ffi::PyObject) -> $return where T: for<'p> $trait<'p>, @@ -99,8 +91,7 @@ macro_rules! py_binary_func { let _pool = $crate::GILPool::new(py); let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg); - let result = $call!(slf, $f, arg); - $crate::callback::cb_convert($conv, py, result) + $call!(slf, $conv, py, $f, arg) } Some(wrap::<$class>) }}; @@ -109,7 +100,7 @@ macro_rules! py_binary_func { #[macro_export] #[doc(hidden)] macro_rules! py_binary_num_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => {{ unsafe extern "C" fn wrap( lhs: *mut ffi::PyObject, rhs: *mut ffi::PyObject, @@ -137,7 +128,7 @@ macro_rules! py_binary_num_func { } // NOTE(kngwyu): -// Now(2020 2/9) This macro is used only for inplace operation so I used call_refmut here. +// Now(2020 2/9) This macro is used only for inplace operation so I used call_mut here. #[macro_export] #[doc(hidden)] macro_rules! py_binary_self_func { @@ -155,7 +146,7 @@ macro_rules! py_binary_self_func { let _pool = $crate::GILPool::new(py); let slf_ = py.from_borrowed_ptr::<$crate::PyCell>(slf); let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg); - let result = call_refmut!(slf_, $f, arg); + let result = call_mut!(slf_, $f, arg); match result { Ok(_) => { ffi::Py_INCREF(slf); @@ -172,16 +163,15 @@ macro_rules! py_binary_self_func { #[doc(hidden)] macro_rules! py_ssizearg_func { // Use call_ref! by default - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => { + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => { py_ssizearg_func!( $trait, $class::$f, - $res_type, $conv, - call_ref + call_ref_with_converter ) }; - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr, $call:ident) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr, $call:ident) => {{ unsafe extern "C" fn wrap( slf: *mut ffi::PyObject, arg: $crate::ffi::Py_ssize_t, @@ -192,8 +182,7 @@ macro_rules! py_ssizearg_func { let py = $crate::Python::assume_gil_acquired(); let _pool = $crate::GILPool::new(py); let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); - let result = $call!(slf, $f ; arg.into()); - $crate::callback::cb_convert($conv, py, result) + $call!(slf, $conv, py, $f ;arg.into()) } Some(wrap::<$class>) }}; @@ -202,16 +191,10 @@ macro_rules! py_ssizearg_func { #[macro_export] #[doc(hidden)] macro_rules! py_ternary_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => { - py_ternary_func!( - $trait, - $class::$f, - $res_type, - $conv, - *mut $crate::ffi::PyObject - ); + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => { + py_ternary_func!($trait, $class::$f, $conv, *mut $crate::ffi::PyObject); }; - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr, $return_type:ty) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr, $return_type:ty) => {{ unsafe extern "C" fn wrap( slf: *mut $crate::ffi::PyObject, arg1: *mut $crate::ffi::PyObject, @@ -228,8 +211,7 @@ macro_rules! py_ternary_func { let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1); let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2); - let result = call_ref!(slf, $f, arg1, arg2); - $crate::callback::cb_convert($conv, py, result) + call_ref_with_converter!(slf, $conv, py, $f, arg1, arg2) } Some(wrap::) @@ -239,7 +221,7 @@ macro_rules! py_ternary_func { #[macro_export] #[doc(hidden)] macro_rules! py_ternary_num_func { - ($trait:ident, $class:ident :: $f:ident, $res_type:ty, $conv:expr) => {{ + ($trait:ident, $class:ident :: $f:ident, $conv:expr) => {{ unsafe extern "C" fn wrap( arg1: *mut $crate::ffi::PyObject, arg2: *mut $crate::ffi::PyObject, @@ -292,7 +274,7 @@ macro_rules! py_ternary_self_func { let slf_cell = py.from_borrowed_ptr::<$crate::PyCell>(slf); let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1); let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2); - let result = call_refmut!(slf_cell, $f, arg1, arg2); + let result = call_mut!(slf_cell, $f, arg1, arg2); match result { Ok(_) => slf, Err(e) => e.restore_and_null(py), @@ -328,7 +310,7 @@ macro_rules! py_func_set { } else { let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name); let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value); - call_refmut!(slf, $fn_set, name, value) + call_mut!(slf, $fn_set, name, value) }; match result { Ok(_) => 0, @@ -359,7 +341,7 @@ macro_rules! py_func_del { let slf = py.from_borrowed_ptr::<$crate::PyCell>(slf); let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name); - call_refmut!(slf, $fn_del, name) + call_mut!(slf, $fn_del, name) } else { Err(PyErr::new::( "Subscript assignment not supported", @@ -393,10 +375,10 @@ macro_rules! py_func_set_del { let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name); let result = if value.is_null() { - call_refmut!(slf, $fn_del, name) + call_mut!(slf, $fn_del, name) } else { let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value); - call_refmut!(slf, $fn_set, name, value) + call_mut!(slf, $fn_set, name, value) }; match result { Ok(_) => 0, @@ -407,33 +389,48 @@ macro_rules! py_func_set_del { }}; } -// Utitlities for extract arguments + call method for PyCell -macro_rules! call_ref { - ($slf: expr, $fn: ident $(; $args: expr)*) => { - match $slf.try_borrow_unguarded() { - Ok(slf) => slf.$fn($($args,)*).into(), - Err(e) => Err(e.into()), - } - }; - ($slf: expr, $fn: ident, $raw_arg: expr $(,$raw_args: expr)* $(; $args: expr)*) => { +macro_rules! _call_impl { + ($slf: ident, $fn: ident $(; $args: expr)*) => { $slf.$fn($($args,)*).into() }; + ($slf: ident, $fn: ident, $raw_arg: expr $(,$raw_args: expr)* $(; $args: expr)*) => { match $raw_arg.extract() { - Ok(arg) => call_ref!($slf, $fn $(,$raw_args)* $(;$args)* ;arg), + Ok(arg) => _call_impl!($slf, $fn $(,$raw_args)* $(;$args)* ;arg), Err(e) => Err(e.into()), } }; } -macro_rules! call_refmut { - ($slf: expr, $fn: ident $(; $args: expr)*) => { - match $slf.try_borrow_mut_unguarded() { - Ok(slf) => slf.$fn($($args,)*).into(), - Err(e) => Err(e.into()), - } - }; - ($slf: expr, $fn: ident, $raw_arg: expr $(,$raw_args: expr)* $(; $args: expr)*) => { - match $raw_arg.extract() { - Ok(arg) => call_refmut!($slf, $fn $(,$raw_args)* $(;$args)* ;arg), +macro_rules! call_ref { + ($slf: expr, $fn: ident $(,$raw_args: expr)* $(; $args: expr)*) => { + match $slf.try_borrow() { + Ok(slf) => _call_impl!(slf, $fn $(,$raw_args)* $(;$args)*), Err(e) => Err(e.into()), } }; } + +macro_rules! call_ref_with_converter { + ($slf: expr, $conv: expr, $py: ident, $fn: ident $(,$raw_args: expr)* $(; $args: expr)*) => { + match $slf.try_borrow() { + Ok(slf) => $crate::callback::cb_convert($conv, $py, _call_impl!(slf, $fn $(,$raw_args)* $(;$args)*)), + Err(e) => $crate::callback::cb_err($conv, $py, e) + } + }; +} + +macro_rules! call_mut { + ($slf: expr, $fn: ident $(,$raw_args: expr)* $(; $args: expr)*) => { + match $slf.try_borrow_mut() { + Ok(mut slf) => _call_impl!(slf, $fn $(,$raw_args)* $(;$args)*), + Err(e) => Err(e.into()), + } + }; +} + +macro_rules! call_mut_with_converter { + ($slf: expr, $conv: expr, $py: ident, $fn: ident $(,$raw_args: expr)* $(; $args: expr)*) => { + match $slf.try_borrow_mut() { + Ok(mut slf) => $crate::callback::cb_convert($conv, $py, _call_impl!(slf, $fn $(,$raw_args)* $(;$args)*)), + Err(e) => $crate::callback::cb_err($conv, $py, e) + } + }; +} diff --git a/src/class/mapping.rs b/src/class/mapping.rs index cccc6a22..39373134 100644 --- a/src/class/mapping.rs +++ b/src/class/mapping.rs @@ -167,8 +167,7 @@ where py_binary_func!( PyMappingGetItemProtocol, T::__getitem__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } diff --git a/src/class/number.rs b/src/class/number.rs index f44ef5b8..1be54f54 100644 --- a/src/class/number.rs +++ b/src/class/number.rs @@ -764,8 +764,7 @@ where py_binary_num_func!( PyNumberAddProtocol, T::__add__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -791,8 +790,7 @@ where py_binary_num_func!( PyNumberSubProtocol, T::__sub__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -818,8 +816,7 @@ where py_binary_num_func!( PyNumberMulProtocol, T::__mul__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -845,8 +842,7 @@ where py_binary_num_func!( PyNumberMatmulProtocol, T::__matmul__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -872,8 +868,7 @@ where py_binary_num_func!( PyNumberTruedivProtocol, T::__truediv__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -899,8 +894,7 @@ where py_binary_num_func!( PyNumberFloordivProtocol, T::__floordiv__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -926,8 +920,7 @@ where py_binary_num_func!( PyNumberModProtocol, T::__mod__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -953,8 +946,7 @@ where py_binary_num_func!( PyNumberDivmodProtocol, T::__divmod__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -980,8 +972,7 @@ where py_ternary_num_func!( PyNumberPowProtocol, T::__pow__, - ::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1007,8 +998,7 @@ where py_binary_num_func!( PyNumberLShiftProtocol, T::__lshift__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1034,8 +1024,7 @@ where py_binary_num_func!( PyNumberRShiftProtocol, T::__rshift__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1061,8 +1050,7 @@ where py_binary_num_func!( PyNumberAndProtocol, T::__and__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1088,8 +1076,7 @@ where py_binary_num_func!( PyNumberXorProtocol, T::__xor__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1115,8 +1102,7 @@ where py_binary_num_func!( PyNumberOrProtocol, T::__or__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1620,8 +1606,7 @@ where py_unary_func!( PyNumberNegProtocol, T::__neg__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1647,8 +1632,7 @@ where py_unary_func!( PyNumberPosProtocol, T::__pos__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1674,8 +1658,7 @@ where py_unary_func!( PyNumberAbsProtocol, T::__abs__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1701,8 +1684,7 @@ where py_unary_func!( PyNumberInvertProtocol, T::__invert__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1728,8 +1710,7 @@ where py_unary_func!( PyNumberIntProtocol, T::__int__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1755,8 +1736,7 @@ where py_unary_func!( PyNumberFloatProtocol, T::__float__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -1782,8 +1762,7 @@ where py_unary_func!( PyNumberIndexProtocol, T::__index__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } diff --git a/src/class/pyasync.rs b/src/class/pyasync.rs index 90a42d83..842219ca 100644 --- a/src/class/pyasync.rs +++ b/src/class/pyasync.rs @@ -153,8 +153,7 @@ where py_unary_func!( PyAsyncAwaitProtocol, T::__await__, - ::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -181,8 +180,7 @@ where py_unary_func!( PyAsyncAiterProtocol, T::__aiter__, - ::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -206,17 +204,20 @@ mod anext { use crate::IntoPyPointer; use crate::Python; use crate::{ffi, IntoPy, PyObject}; + use std::marker::PhantomData; use std::ptr; - pub struct IterANextResultConverter; + struct IterANextResultConverter(PhantomData); - impl CallbackConverter> for IterANextResultConverter + impl CallbackConverter for IterANextResultConverter where T: IntoPy, { - type R = *mut ffi::PyObject; + type Source = Option; + type Result = *mut ffi::PyObject; + const ERR_VALUE: Self::Result = ptr::null_mut(); - fn convert(val: Option, py: Python) -> *mut ffi::PyObject { + fn convert(val: Self::Source, py: Python) -> Self::Result { match val { Some(val) => val.into_py(py).into_ptr(), None => unsafe { @@ -225,11 +226,6 @@ mod anext { }, } } - - #[inline] - fn error_value() -> *mut ffi::PyObject { - ptr::null_mut() - } } impl PyAsyncAnextProtocolImpl for T @@ -241,10 +237,9 @@ mod anext { py_unary_func!( PyAsyncAnextProtocol, T::__anext__, - Option, - IterANextResultConverter, + IterANextResultConverter::(std::marker::PhantomData), *mut crate::ffi::PyObject, - call_refmut + call_mut_with_converter ) } } diff --git a/src/class/sequence.rs b/src/class/sequence.rs index 643b4d21..79716e84 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -202,8 +202,7 @@ where py_ssizearg_func!( PySequenceGetItemProtocol, T::__getitem__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -283,10 +282,7 @@ mod sq_ass_item_impl { match result { Ok(_) => 0, - Err(e) => { - e.restore(py); - -1 - } + Err(e) => e.restore_and_minus1(py), } } Some(wrap::) @@ -334,10 +330,7 @@ mod sq_ass_item_impl { match result { Ok(_) => 0, - Err(e) => { - e.restore(py); - -1 - } + Err(e) => e.restore_and_minus1(py), } } Some(wrap::) @@ -375,7 +368,7 @@ mod sq_ass_item_impl { let slf = py.from_borrowed_ptr::>(slf); let result = if value.is_null() { - call_refmut!(slf, __delitem__; key.into()) + call_mut!(slf, __delitem__; key.into()) } else { let value = py.from_borrowed_ptr::(value); match value.extract() { @@ -388,10 +381,7 @@ mod sq_ass_item_impl { }; match result { Ok(_) => 0, - Err(e) => { - e.restore(py); - -1 - } + Err(e) => e.restore_and_minus1(py), } } Some(wrap::) @@ -420,7 +410,6 @@ where py_binary_func!( PySequenceContainsProtocol, T::__contains__, - bool, BoolCallbackConverter, c_int ) @@ -448,8 +437,7 @@ where py_binary_func!( PySequenceConcatProtocol, T::__concat__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -475,8 +463,7 @@ where py_ssizearg_func!( PySequenceRepeatProtocol, T::__repeat__, - T::Success, - PyObjectCallbackConverter + PyObjectCallbackConverter::(std::marker::PhantomData) ) } } @@ -502,10 +489,9 @@ where py_binary_func!( PySequenceInplaceConcatProtocol, T::__inplace_concat__, - T, - PyObjectCallbackConverter, + PyObjectCallbackConverter::(std::marker::PhantomData), *mut crate::ffi::PyObject, - call_refmut + call_mut_with_converter ) } } @@ -531,9 +517,8 @@ where py_ssizearg_func!( PySequenceInplaceRepeatProtocol, T::__inplace_repeat__, - T, - PyObjectCallbackConverter, - call_refmut + PyObjectCallbackConverter::(std::marker::PhantomData), + call_mut_with_converter ) } }