Move nb_bool under PyObjectProtocol again
This commit is contained in:
parent
ac2c51c774
commit
f32277163a
|
@ -123,6 +123,11 @@ pub const OBJECT: Proto = Proto {
|
||||||
pyres: true,
|
pyres: true,
|
||||||
proto: "pyo3::class::basic::PyObjectRichcmpProtocol",
|
proto: "pyo3::class::basic::PyObjectRichcmpProtocol",
|
||||||
},
|
},
|
||||||
|
MethodProto::Unary {
|
||||||
|
name: "__bool__",
|
||||||
|
pyres: false,
|
||||||
|
proto: "pyo3::class::basic::PyObjectBoolProtocol",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
py_methods: &[
|
py_methods: &[
|
||||||
PyMethod::new("__format__", "pyo3::class::basic::FormatProtocolImpl"),
|
PyMethod::new("__format__", "pyo3::class::basic::FormatProtocolImpl"),
|
||||||
|
@ -142,6 +147,7 @@ pub const OBJECT: Proto = Proto {
|
||||||
},
|
},
|
||||||
SlotSetter::new(&["__setattr__"], "set_setattr"),
|
SlotSetter::new(&["__setattr__"], "set_setattr"),
|
||||||
SlotSetter::new(&["__delattr__"], "set_delattr"),
|
SlotSetter::new(&["__delattr__"], "set_delattr"),
|
||||||
|
SlotSetter::new(&["__bool__"], "set_bool"),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -784,11 +790,6 @@ pub const NUM: Proto = Proto {
|
||||||
pyres: true,
|
pyres: true,
|
||||||
proto: "pyo3::class::number::PyNumberRoundProtocol",
|
proto: "pyo3::class::number::PyNumberRoundProtocol",
|
||||||
},
|
},
|
||||||
MethodProto::Unary {
|
|
||||||
name: "__bool__",
|
|
||||||
pyres: false,
|
|
||||||
proto: "pyo3::class::number::PyNumberBoolProtocol",
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
py_methods: &[
|
py_methods: &[
|
||||||
PyMethod::coexist("__radd__", "pyo3::class::number::PyNumberRAddProtocolImpl"),
|
PyMethod::coexist("__radd__", "pyo3::class::number::PyNumberRAddProtocolImpl"),
|
||||||
|
@ -867,7 +868,6 @@ pub const NUM: Proto = Proto {
|
||||||
SlotSetter::new(&["__neg__"], "set_neg"),
|
SlotSetter::new(&["__neg__"], "set_neg"),
|
||||||
SlotSetter::new(&["__pos__"], "set_pos"),
|
SlotSetter::new(&["__pos__"], "set_pos"),
|
||||||
SlotSetter::new(&["__abs__"], "set_abs"),
|
SlotSetter::new(&["__abs__"], "set_abs"),
|
||||||
SlotSetter::new(&["__bool__"], "set_bool"),
|
|
||||||
SlotSetter::new(&["__invert__"], "set_invert"),
|
SlotSetter::new(&["__invert__"], "set_invert"),
|
||||||
SlotSetter::new(&["__rdivmod__"], "set_rdivmod"),
|
SlotSetter::new(&["__rdivmod__"], "set_rdivmod"),
|
||||||
SlotSetter {
|
SlotSetter {
|
||||||
|
|
|
@ -90,6 +90,12 @@ pub trait PyObjectProtocol<'p>: PyClass {
|
||||||
{
|
{
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
fn __bool__(&'p self) -> Self::Result
|
||||||
|
where
|
||||||
|
Self: PyObjectBoolProtocol<'p>,
|
||||||
|
{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PyObjectGetAttrProtocol<'p>: PyObjectProtocol<'p> {
|
pub trait PyObjectGetAttrProtocol<'p>: PyObjectProtocol<'p> {
|
||||||
|
@ -144,6 +150,7 @@ pub struct PyObjectMethods {
|
||||||
pub tp_getattro: Option<ffi::getattrofunc>,
|
pub tp_getattro: Option<ffi::getattrofunc>,
|
||||||
pub tp_richcompare: Option<ffi::richcmpfunc>,
|
pub tp_richcompare: Option<ffi::richcmpfunc>,
|
||||||
pub tp_setattro: Option<ffi::setattrofunc>,
|
pub tp_setattro: Option<ffi::setattrofunc>,
|
||||||
|
pub nb_bool: Option<ffi::inquiry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PyObjectMethods {
|
impl PyObjectMethods {
|
||||||
|
@ -215,6 +222,12 @@ impl PyObjectMethods {
|
||||||
__delattr__
|
__delattr__
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
pub fn set_bool<T>(&mut self)
|
||||||
|
where
|
||||||
|
T: for<'p> PyObjectBoolProtocol<'p>,
|
||||||
|
{
|
||||||
|
self.nb_bool = py_unary_func!(PyObjectBoolProtocol, T::__bool__, c_int);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tp_getattro<T>() -> Option<ffi::binaryfunc>
|
fn tp_getattro<T>() -> Option<ffi::binaryfunc>
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
use crate::err::PyResult;
|
use crate::err::PyResult;
|
||||||
use crate::{ffi, FromPyObject, IntoPy, PyClass, PyObject};
|
use crate::{ffi, FromPyObject, IntoPy, PyClass, PyObject};
|
||||||
use std::os::raw::c_int;
|
|
||||||
|
|
||||||
/// Number interface
|
/// Number interface
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
|
@ -314,12 +313,6 @@ pub trait PyNumberProtocol<'p>: PyClass {
|
||||||
{
|
{
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
fn __bool__(&'p self) -> Self::Result
|
|
||||||
where
|
|
||||||
Self: PyNumberBoolProtocol<'p>,
|
|
||||||
{
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PyNumberAddProtocol<'p>: PyNumberProtocol<'p> {
|
pub trait PyNumberAddProtocol<'p>: PyNumberProtocol<'p> {
|
||||||
|
@ -622,11 +615,12 @@ pub trait PyNumberIndexProtocol<'p>: PyNumberProtocol<'p> {
|
||||||
type Result: Into<PyResult<Self::Success>>;
|
type Result: Into<PyResult<Self::Success>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PyNumberBoolProtocol<'p>: PyNumberProtocol<'p> {
|
|
||||||
type Result: Into<PyResult<bool>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ffi::PyNumberMethods {
|
impl ffi::PyNumberMethods {
|
||||||
|
pub(crate) fn from_nb_bool(nb_bool: ffi::inquiry) -> *mut Self {
|
||||||
|
let mut nm = ffi::PyNumberMethods_INIT;
|
||||||
|
nm.nb_bool = Some(nb_bool);
|
||||||
|
Box::into_raw(Box::new(nm))
|
||||||
|
}
|
||||||
pub fn set_add<T>(&mut self)
|
pub fn set_add<T>(&mut self)
|
||||||
where
|
where
|
||||||
T: for<'p> PyNumberAddProtocol<'p>,
|
T: for<'p> PyNumberAddProtocol<'p>,
|
||||||
|
@ -711,12 +705,6 @@ impl ffi::PyNumberMethods {
|
||||||
{
|
{
|
||||||
self.nb_absolute = py_unary_func!(PyNumberAbsProtocol, T::__abs__);
|
self.nb_absolute = py_unary_func!(PyNumberAbsProtocol, T::__abs__);
|
||||||
}
|
}
|
||||||
pub fn set_bool<T>(&mut self)
|
|
||||||
where
|
|
||||||
T: for<'p> PyNumberBoolProtocol<'p>,
|
|
||||||
{
|
|
||||||
self.nb_bool = py_unary_func!(PyNumberBoolProtocol, T::__bool__, c_int);
|
|
||||||
}
|
|
||||||
pub fn set_invert<T>(&mut self)
|
pub fn set_invert<T>(&mut self)
|
||||||
where
|
where
|
||||||
T: for<'p> PyNumberInvertProtocol<'p>,
|
T: for<'p> PyNumberInvertProtocol<'p>,
|
||||||
|
|
|
@ -152,13 +152,21 @@ where
|
||||||
unsafe { iter.as_ref() }.update_typeobj(type_object);
|
unsafe { iter.as_ref() }.update_typeobj(type_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nb_bool is a part of PyObjectProtocol, but should be placed under tp_as_number
|
||||||
|
let mut nb_bool = None;
|
||||||
// basic methods
|
// basic methods
|
||||||
if let Some(basic) = T::basic_methods() {
|
if let Some(basic) = T::basic_methods() {
|
||||||
unsafe { basic.as_ref() }.update_typeobj(type_object);
|
unsafe { basic.as_ref() }.update_typeobj(type_object);
|
||||||
|
nb_bool = unsafe { basic.as_ref() }.nb_bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
// number methods
|
// number methods
|
||||||
type_object.tp_as_number = T::number_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
|
type_object.tp_as_number = T::number_methods()
|
||||||
|
.map(|mut p| {
|
||||||
|
unsafe { p.as_mut() }.nb_bool = nb_bool;
|
||||||
|
p.as_ptr()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| nb_bool.map_or_else(ptr::null_mut, ffi::PyNumberMethods::from_nb_bool));
|
||||||
// mapping methods
|
// mapping methods
|
||||||
type_object.tp_as_mapping = T::mapping_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
|
type_object.tp_as_mapping = T::mapping_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
|
||||||
// sequence methods
|
// sequence methods
|
||||||
|
|
|
@ -121,10 +121,6 @@ impl PyObjectProtocol for Comparisons {
|
||||||
fn __hash__(&self) -> PyResult<isize> {
|
fn __hash__(&self) -> PyResult<isize> {
|
||||||
Ok(self.val as isize)
|
Ok(self.val as isize)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[pyproto]
|
|
||||||
impl pyo3::class::PyNumberProtocol for Comparisons {
|
|
||||||
fn __bool__(&self) -> PyResult<bool> {
|
fn __bool__(&self) -> PyResult<bool> {
|
||||||
Ok(self.val != 0)
|
Ok(self.val != 0)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue