Skip to content

Commit f322771

Browse files
committed
Move nb_bool under PyObjectProtocol again
1 parent ac2c51c commit f322771

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

pyo3-derive-backend/src/defs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ pub const OBJECT: Proto = Proto {
123123
pyres: true,
124124
proto: "pyo3::class::basic::PyObjectRichcmpProtocol",
125125
},
126+
MethodProto::Unary {
127+
name: "__bool__",
128+
pyres: false,
129+
proto: "pyo3::class::basic::PyObjectBoolProtocol",
130+
},
126131
],
127132
py_methods: &[
128133
PyMethod::new("__format__", "pyo3::class::basic::FormatProtocolImpl"),
@@ -142,6 +147,7 @@ pub const OBJECT: Proto = Proto {
142147
},
143148
SlotSetter::new(&["__setattr__"], "set_setattr"),
144149
SlotSetter::new(&["__delattr__"], "set_delattr"),
150+
SlotSetter::new(&["__bool__"], "set_bool"),
145151
],
146152
};
147153

@@ -784,11 +790,6 @@ pub const NUM: Proto = Proto {
784790
pyres: true,
785791
proto: "pyo3::class::number::PyNumberRoundProtocol",
786792
},
787-
MethodProto::Unary {
788-
name: "__bool__",
789-
pyres: false,
790-
proto: "pyo3::class::number::PyNumberBoolProtocol",
791-
},
792793
],
793794
py_methods: &[
794795
PyMethod::coexist("__radd__", "pyo3::class::number::PyNumberRAddProtocolImpl"),
@@ -867,7 +868,6 @@ pub const NUM: Proto = Proto {
867868
SlotSetter::new(&["__neg__"], "set_neg"),
868869
SlotSetter::new(&["__pos__"], "set_pos"),
869870
SlotSetter::new(&["__abs__"], "set_abs"),
870-
SlotSetter::new(&["__bool__"], "set_bool"),
871871
SlotSetter::new(&["__invert__"], "set_invert"),
872872
SlotSetter::new(&["__rdivmod__"], "set_rdivmod"),
873873
SlotSetter {

src/class/basic.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ pub trait PyObjectProtocol<'p>: PyClass {
9090
{
9191
unimplemented!()
9292
}
93+
fn __bool__(&'p self) -> Self::Result
94+
where
95+
Self: PyObjectBoolProtocol<'p>,
96+
{
97+
unimplemented!()
98+
}
9399
}
94100

95101
pub trait PyObjectGetAttrProtocol<'p>: PyObjectProtocol<'p> {
@@ -144,6 +150,7 @@ pub struct PyObjectMethods {
144150
pub tp_getattro: Option<ffi::getattrofunc>,
145151
pub tp_richcompare: Option<ffi::richcmpfunc>,
146152
pub tp_setattro: Option<ffi::setattrofunc>,
153+
pub nb_bool: Option<ffi::inquiry>,
147154
}
148155

149156
impl PyObjectMethods {
@@ -215,6 +222,12 @@ impl PyObjectMethods {
215222
__delattr__
216223
)
217224
}
225+
pub fn set_bool<T>(&mut self)
226+
where
227+
T: for<'p> PyObjectBoolProtocol<'p>,
228+
{
229+
self.nb_bool = py_unary_func!(PyObjectBoolProtocol, T::__bool__, c_int);
230+
}
218231
}
219232

220233
fn tp_getattro<T>() -> Option<ffi::binaryfunc>

src/class/number.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
use crate::err::PyResult;
77
use crate::{ffi, FromPyObject, IntoPy, PyClass, PyObject};
8-
use std::os::raw::c_int;
98

109
/// Number interface
1110
#[allow(unused_variables)]
@@ -314,12 +313,6 @@ pub trait PyNumberProtocol<'p>: PyClass {
314313
{
315314
unimplemented!()
316315
}
317-
fn __bool__(&'p self) -> Self::Result
318-
where
319-
Self: PyNumberBoolProtocol<'p>,
320-
{
321-
unimplemented!()
322-
}
323316
}
324317

325318
pub trait PyNumberAddProtocol<'p>: PyNumberProtocol<'p> {
@@ -622,11 +615,12 @@ pub trait PyNumberIndexProtocol<'p>: PyNumberProtocol<'p> {
622615
type Result: Into<PyResult<Self::Success>>;
623616
}
624617

625-
pub trait PyNumberBoolProtocol<'p>: PyNumberProtocol<'p> {
626-
type Result: Into<PyResult<bool>>;
627-
}
628-
629618
impl ffi::PyNumberMethods {
619+
pub(crate) fn from_nb_bool(nb_bool: ffi::inquiry) -> *mut Self {
620+
let mut nm = ffi::PyNumberMethods_INIT;
621+
nm.nb_bool = Some(nb_bool);
622+
Box::into_raw(Box::new(nm))
623+
}
630624
pub fn set_add<T>(&mut self)
631625
where
632626
T: for<'p> PyNumberAddProtocol<'p>,
@@ -711,12 +705,6 @@ impl ffi::PyNumberMethods {
711705
{
712706
self.nb_absolute = py_unary_func!(PyNumberAbsProtocol, T::__abs__);
713707
}
714-
pub fn set_bool<T>(&mut self)
715-
where
716-
T: for<'p> PyNumberBoolProtocol<'p>,
717-
{
718-
self.nb_bool = py_unary_func!(PyNumberBoolProtocol, T::__bool__, c_int);
719-
}
720708
pub fn set_invert<T>(&mut self)
721709
where
722710
T: for<'p> PyNumberInvertProtocol<'p>,

src/pyclass.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,21 @@ where
152152
unsafe { iter.as_ref() }.update_typeobj(type_object);
153153
}
154154

155+
// nb_bool is a part of PyObjectProtocol, but should be placed under tp_as_number
156+
let mut nb_bool = None;
155157
// basic methods
156158
if let Some(basic) = T::basic_methods() {
157159
unsafe { basic.as_ref() }.update_typeobj(type_object);
160+
nb_bool = unsafe { basic.as_ref() }.nb_bool;
158161
}
159162

160163
// number methods
161-
type_object.tp_as_number = T::number_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
164+
type_object.tp_as_number = T::number_methods()
165+
.map(|mut p| {
166+
unsafe { p.as_mut() }.nb_bool = nb_bool;
167+
p.as_ptr()
168+
})
169+
.unwrap_or_else(|| nb_bool.map_or_else(ptr::null_mut, ffi::PyNumberMethods::from_nb_bool));
162170
// mapping methods
163171
type_object.tp_as_mapping = T::mapping_methods().map_or_else(ptr::null_mut, |p| p.as_ptr());
164172
// sequence methods

tests/test_dunder.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ impl PyObjectProtocol for Comparisons {
121121
fn __hash__(&self) -> PyResult<isize> {
122122
Ok(self.val as isize)
123123
}
124-
}
125-
126-
#[pyproto]
127-
impl pyo3::class::PyNumberProtocol for Comparisons {
128124
fn __bool__(&self) -> PyResult<bool> {
129125
Ok(self.val != 0)
130126
}

0 commit comments

Comments
 (0)