Skip to content

Commit

Permalink
Move nb_bool under PyObjectProtocol again
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Jun 13, 2020
1 parent ac2c51c commit f322771
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
12 changes: 6 additions & 6 deletions pyo3-derive-backend/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ pub const OBJECT: Proto = Proto {
pyres: true,
proto: "pyo3::class::basic::PyObjectRichcmpProtocol",
},
MethodProto::Unary {
name: "__bool__",
pyres: false,
proto: "pyo3::class::basic::PyObjectBoolProtocol",
},
],
py_methods: &[
PyMethod::new("__format__", "pyo3::class::basic::FormatProtocolImpl"),
Expand All @@ -142,6 +147,7 @@ pub const OBJECT: Proto = Proto {
},
SlotSetter::new(&["__setattr__"], "set_setattr"),
SlotSetter::new(&["__delattr__"], "set_delattr"),
SlotSetter::new(&["__bool__"], "set_bool"),
],
};

Expand Down Expand Up @@ -784,11 +790,6 @@ pub const NUM: Proto = Proto {
pyres: true,
proto: "pyo3::class::number::PyNumberRoundProtocol",
},
MethodProto::Unary {
name: "__bool__",
pyres: false,
proto: "pyo3::class::number::PyNumberBoolProtocol",
},
],
py_methods: &[
PyMethod::coexist("__radd__", "pyo3::class::number::PyNumberRAddProtocolImpl"),
Expand Down Expand Up @@ -867,7 +868,6 @@ pub const NUM: Proto = Proto {
SlotSetter::new(&["__neg__"], "set_neg"),
SlotSetter::new(&["__pos__"], "set_pos"),
SlotSetter::new(&["__abs__"], "set_abs"),
SlotSetter::new(&["__bool__"], "set_bool"),
SlotSetter::new(&["__invert__"], "set_invert"),
SlotSetter::new(&["__rdivmod__"], "set_rdivmod"),
SlotSetter {
Expand Down
13 changes: 13 additions & 0 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub trait PyObjectProtocol<'p>: PyClass {
{
unimplemented!()
}
fn __bool__(&'p self) -> Self::Result
where
Self: PyObjectBoolProtocol<'p>,
{
unimplemented!()
}
}

pub trait PyObjectGetAttrProtocol<'p>: PyObjectProtocol<'p> {
Expand Down Expand Up @@ -144,6 +150,7 @@ pub struct PyObjectMethods {
pub tp_getattro: Option<ffi::getattrofunc>,
pub tp_richcompare: Option<ffi::richcmpfunc>,
pub tp_setattro: Option<ffi::setattrofunc>,
pub nb_bool: Option<ffi::inquiry>,
}

impl PyObjectMethods {
Expand Down Expand Up @@ -215,6 +222,12 @@ impl PyObjectMethods {
__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>
Expand Down
22 changes: 5 additions & 17 deletions src/class/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use crate::err::PyResult;
use crate::{ffi, FromPyObject, IntoPy, PyClass, PyObject};
use std::os::raw::c_int;

/// Number interface
#[allow(unused_variables)]
Expand Down Expand Up @@ -314,12 +313,6 @@ pub trait PyNumberProtocol<'p>: PyClass {
{
unimplemented!()
}
fn __bool__(&'p self) -> Self::Result
where
Self: PyNumberBoolProtocol<'p>,
{
unimplemented!()
}
}

pub trait PyNumberAddProtocol<'p>: PyNumberProtocol<'p> {
Expand Down Expand Up @@ -622,11 +615,12 @@ pub trait PyNumberIndexProtocol<'p>: PyNumberProtocol<'p> {
type Result: Into<PyResult<Self::Success>>;
}

pub trait PyNumberBoolProtocol<'p>: PyNumberProtocol<'p> {
type Result: Into<PyResult<bool>>;
}

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)
where
T: for<'p> PyNumberAddProtocol<'p>,
Expand Down Expand Up @@ -711,12 +705,6 @@ impl ffi::PyNumberMethods {
{
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)
where
T: for<'p> PyNumberInvertProtocol<'p>,
Expand Down
10 changes: 9 additions & 1 deletion src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,21 @@ where
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
if let Some(basic) = T::basic_methods() {
unsafe { basic.as_ref() }.update_typeobj(type_object);
nb_bool = unsafe { basic.as_ref() }.nb_bool;
}

// 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
type_object.tp_as_mapping = T::mapping_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
// sequence methods
Expand Down
4 changes: 0 additions & 4 deletions tests/test_dunder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ impl PyObjectProtocol for Comparisons {
fn __hash__(&self) -> PyResult<isize> {
Ok(self.val as isize)
}
}

#[pyproto]
impl pyo3::class::PyNumberProtocol for Comparisons {
fn __bool__(&self) -> PyResult<bool> {
Ok(self.val != 0)
}
Expand Down

0 comments on commit f322771

Please sign in to comment.