Skip to content

Commit

Permalink
Inhibit __ipow__ to take Modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Mar 29, 2020
1 parent 25eda36 commit ac418ce
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions pyo3-derive-backend/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,9 @@ pub const NUM: Proto = Proto {
pyres: false,
proto: "pyo3::class::number::PyNumberIModProtocol",
},
MethodProto::Ternary {
MethodProto::Binary {
name: "__ipow__",
arg1: "Other",
arg2: "Modulo",
arg: "Other",
pyres: false,
proto: "pyo3::class::number::PyNumberIPowProtocol",
},
Expand Down
16 changes: 8 additions & 8 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,16 @@ macro_rules! py_ternary_reverse_num_func {
}};
}

// NOTE(kngwyu): Somehow __ipow__ causes SIGSEGV in Python < 3.8 when we extract arg2,
// so we ignore it. It's the same as what CPython does.
#[macro_export]
#[doc(hidden)]
macro_rules! py_ternary_self_func {
macro_rules! py_dummy_ternary_self_func {
($trait:ident, $class:ident :: $f:ident) => {{
unsafe extern "C" fn wrap<T>(
slf: *mut $crate::ffi::PyObject,
arg1: *mut $crate::ffi::PyObject,
arg2: *mut $crate::ffi::PyObject,
_arg2: *mut $crate::ffi::PyObject,
) -> *mut $crate::ffi::PyObject
where
T: for<'p> $trait<'p>,
Expand All @@ -318,20 +320,18 @@ macro_rules! py_ternary_self_func {

let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf_cell = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::PyAny>(arg2);
let result = call_mut!(slf_cell, $f, arg1, arg2);
let slf_ = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
let result = call_mut!(slf_, $f, arg);
match result {
Ok(_) => {
// Without this INCREF, SIGSEGV happens...
ffi::Py_INCREF(slf);
slf
}
Err(e) => e.restore_and_null(py),
}
}
Some(wrap::<T>)
Some(wrap::<$class>)
}};
}

Expand Down
5 changes: 2 additions & 3 deletions src/class/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub trait PyNumberProtocol<'p>: PyClass {
{
unimplemented!()
}
fn __ipow__(&'p mut self, other: Self::Other, modulo: Option<Self::Modulo>) -> Self::Result
fn __ipow__(&'p mut self, other: Self::Other) -> Self::Result
where
Self: PyNumberIPowProtocol<'p>,
{
Expand Down Expand Up @@ -544,7 +544,6 @@ pub trait PyNumberIDivmodProtocol<'p>: PyNumberProtocol<'p> {

pub trait PyNumberIPowProtocol<'p>: PyNumberProtocol<'p> {
type Other: FromPyObject<'p>;
type Modulo: FromPyObject<'p>;
type Result: Into<PyResult<()>>;
}

Expand Down Expand Up @@ -1280,7 +1279,7 @@ where
T: for<'p> PyNumberIPowProtocol<'p>,
{
fn nb_inplace_power() -> Option<ffi::ternaryfunc> {
py_ternary_self_func!(PyNumberIPowProtocol, T::__ipow__)
py_dummy_ternary_self_func!(PyNumberIPowProtocol, T::__ipow__)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl PyNumberProtocol for InPlaceOperations {
Ok(())
}

fn __ipow__(&mut self, other: u32, _mod: Option<u32>) -> PyResult<()> {
fn __ipow__(&mut self, other: u32) -> PyResult<()> {
self.value = self.value.pow(other);
Ok(())
}
Expand Down

0 comments on commit ac418ce

Please sign in to comment.