Skip to content

Commit

Permalink
Changes due to switching to pyo3 0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bauer committed Mar 8, 2022
1 parent 797526a commit f81a9af
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude = ["/.github/*", "*.ipynb", "./docs/*"]

[dependencies]
num-traits = "0.2"
pyo3 = { version = "0.15", optional = true }
pyo3 = { version = "0.16", optional = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion build_wheel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ crate-type = ["cdylib"]

[dependencies]
num-dual = { path = "..", features = ["python"]}
pyo3 = { version = "0.15", features = ["extension-module", "abi3", "abi3-py36", "multiple-pymethods"] }
pyo3 = { version = "0.16", features = ["extension-module", "abi3", "abi3-py37", "multiple-pymethods"] }
1 change: 0 additions & 1 deletion src/python/dual.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::*;
use pyo3::exceptions::PyTypeError;
use pyo3::number::PyNumberProtocol;
use pyo3::prelude::*;

#[pyclass(name = "Dual64")]
Expand Down
1 change: 0 additions & 1 deletion src/python/dual2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::dual::PyDual64;
use crate::*;
use pyo3::exceptions::PyTypeError;
use pyo3::number::PyNumberProtocol;
use pyo3::prelude::*;

#[pyclass(name = "Dual2_64")]
Expand Down
3 changes: 1 addition & 2 deletions src/python/dual3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::dual::PyDual64;
use crate::*;
use pyo3::exceptions::PyTypeError;
use pyo3::number::PyNumberProtocol;
use pyo3::prelude::*;

#[pyclass(name = "Dual3_64")]
Expand Down Expand Up @@ -72,7 +71,7 @@ impl_dual_num!(PyDual3Dual64, Dual3<Dual64, f64>, PyDual64);

#[pyfunction]
#[pyo3(text_signature = "(x)")]
fn derive3(x: &PyAny) -> PyResult<PyObject> {
pub fn derive3(x: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(x) = x.extract::<f64>() {
return Ok(
Expand Down
1 change: 0 additions & 1 deletion src/python/hyperdual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::dual::PyDual64;
use super::dual2::{PyDual2Dual64, PyDual2_64};
use crate::*;
use pyo3::exceptions::PyTypeError;
use pyo3::number::PyNumberProtocol;
use pyo3::prelude::*;

#[pyclass(name = "HyperDual64")]
Expand Down
46 changes: 23 additions & 23 deletions src/python/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ macro_rules! impl_dual_num {
}
}

#[pyproto]
impl PyNumberProtocol for $py_type_name {
fn __add__(lhs: PyRef<'p, Self>, rhs: &PyAny) -> PyResult<Self> {
#[pymethods]
impl $py_type_name {
fn __add__(&self, rhs: &PyAny) -> PyResult<Self> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok((lhs.0 + r).into());
return Ok((self.0 + r).into());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok((lhs.0 + r.0).into());
return Ok((self.0 + r.0).into());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}
Expand All @@ -233,12 +233,12 @@ macro_rules! impl_dual_num {
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __sub__(lhs: PyRef<'p, Self>, rhs: &PyAny) -> PyResult<Self> {
fn __sub__(&self, rhs: &PyAny) -> PyResult<Self> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok((lhs.0 - r).into());
return Ok((self.0 - r).into());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok((lhs.0 - r.0).into());
return Ok((self.0 - r.0).into());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}
Expand All @@ -250,12 +250,12 @@ macro_rules! impl_dual_num {
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __mul__(lhs: PyRef<'p, Self>, rhs: &PyAny) -> PyResult<Self> {
fn __mul__(&self, rhs: &PyAny) -> PyResult<Self> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok((lhs.0 * r).into());
return Ok((self.0 * r).into());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok((lhs.0 * r.0).into());
return Ok((self.0 * r.0).into());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}
Expand All @@ -267,12 +267,12 @@ macro_rules! impl_dual_num {
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __truediv__(lhs: PyRef<'p, Self>, rhs: &PyAny) -> PyResult<Self> {
fn __truediv__(&self, rhs: &PyAny) -> PyResult<Self> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok((lhs.0 / r).into());
return Ok((self.0 / r).into());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok((lhs.0 / r.0).into());
return Ok((self.0 / r.0).into());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}
Expand All @@ -284,15 +284,15 @@ macro_rules! impl_dual_num {
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __pow__(lhs: &PyAny, rhs: &PyAny, _mod: Option<u32>) -> PyResult<Self> {
if let (Ok(l), Ok(r)) = (lhs.extract::<Self>(), rhs.extract::<i32>()) {
return Ok(l.0.powi(r).into());
fn __pow__(&self, rhs: &PyAny, _mod: Option<u32>) -> PyResult<Self> {
if let Ok(r) = rhs.extract::<i32>() {
return Ok(self.0.powi(r).into());
};
if let (Ok(l), Ok(r)) = (lhs.extract::<Self>(), rhs.extract::<f64>()) {
return Ok(l.0.powf(r).into());
if let Ok(r) = rhs.extract::<f64>() {
return Ok(self.0.powf(r).into());
};
if let (Ok(l), Ok(r)) = (lhs.extract::<Self>(), rhs.extract::<Self>()) {
return Ok(l.0.powd(r.0).into());
if let Ok(r) = rhs.extract::<Self>() {
return Ok(self.0.powd(r.0).into());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}
Expand All @@ -302,8 +302,8 @@ macro_rules! impl_dual_num {
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for $py_type_name {
#[pymethods]
impl $py_type_name {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down
6 changes: 3 additions & 3 deletions src/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod dual2;
mod dual3;
mod hyperdual;

use dual::__pyo3_get_function_derive1;
use dual3::__pyo3_get_function_derive3;
use hyperdual::__pyo3_get_function_derive2;
use dual::derive1;
use dual3::derive3;
use hyperdual::derive2;

pub use dual::PyDual64;
pub use dual2::{PyDual2Dual64, PyDual2_64};
Expand Down

0 comments on commit f81a9af

Please sign in to comment.