Skip to content

Commit

Permalink
Update pyo3 and numpy dependencies to 0.21 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner authored Apr 11, 2024
1 parent 854ec18 commit 821cc32
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 397 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.8.0] - 2024-04-11
### Packaging
- Updated `pyo3` and `numpy` dependencies to 0.21 and adjusted to the new `Bound` API.

## [0.7.0] - 2023-10-15
### Packaging
- Updated `pyo3` and `numpy` dependencies to 0.20.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quantity"
version = "0.7.0"
version = "0.8.0"
authors = ["Philipp Rehner <[email protected]>",
"Gernot Bauer <[email protected]>"]
edition = "2021"
Expand All @@ -25,8 +25,8 @@ serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
ang = "0.6"
regex = "1.5"
pyo3 = { version = "0.20", features = ["multiple-pymethods"], optional = true}
numpy = { version = "0.20", optional = true }
pyo3 = { version = "0.21", features = ["multiple-pymethods"], optional = true}
numpy = { version = "0.21", optional = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add this to your `Cargo.toml`:

```
[dependencies]
quantity = "0.6"
quantity = "0.8"
```

## Python package
Expand Down
4 changes: 2 additions & 2 deletions si-units/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "si-units"
version = "0.7.0"
version = "0.8.0"
authors = ["Philipp Rehner <[email protected]>",
"Gernot Bauer <[email protected]>"]
edition = "2021"
Expand All @@ -21,5 +21,5 @@ crate-type = ["cdylib"]
quantity = { path = "..", features = ["python"]}

[dependencies.pyo3]
version = "0.20"
version = "0.21"
features = ["extension-module", "abi3", "abi3-py37"]
4 changes: 2 additions & 2 deletions si-units/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use quantity::python::quantity;

/// Implementation of SI numbers.
#[pymodule]
pub fn si_units(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
quantity(_py, m)
pub fn si_units<'py>(py: Python<'py>, m: Bound<'py, PyModule>) -> PyResult<()> {
quantity(py, m)
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub trait Unit:
/// + [Methods for Scalar Quantities](#methods-for-scalar-quantities)
/// + [Methods for n-Dimensional Array Quantities](#methods-for-n-dimensional-array-quantities)
/// + [Methods for 1-Dimensional Array Quantities](#methods-for-1-dimensional-array-quantities)
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct Quantity<F, U> {
pub(crate) value: F,
pub(crate) unit: U,
Expand Down
46 changes: 12 additions & 34 deletions src/python/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,27 @@ impl PyAngle {
Ok(self.0.to_string())
}

fn __add__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
rhs.extract::<PyAngle>()
.map(|r| Ok(PyCell::new(py, Self(self.0 + r.0))?.to_object(py)))?
})
fn __add__(&self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}

fn __sub__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
rhs.extract::<PyAngle>()
.map(|r| Ok(PyCell::new(py, Self(self.0 - r.0))?.to_object(py)))?
})
fn __sub__(&self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}

fn __mul__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
rhs.extract::<f64>()
.map(|r| Ok(PyCell::new(py, Self(self.0 * r))?.to_object(py)))?
})
fn __mul__(&self, rhs: f64) -> Self {
Self(self.0 * rhs)
}

fn __rmul__(&self, lhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
lhs.extract::<f64>()
.map(|l| Ok(PyCell::new(py, Self(l * self.0))?.to_object(py)))?
})
fn __rmul__(&self, lhs: f64) -> Self {
Self(lhs * self.0)
}

fn __truediv__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
rhs.extract::<f64>()
.map(|r| Ok(PyCell::new(py, Self(self.0 / r))?.to_object(py)))?
})
fn __truediv__(&self, rhs: f64) -> Self {
Self(self.0 / rhs)
}

fn __rtruediv__(&self, lhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
lhs.extract::<f64>()
.map(|l| Ok(PyCell::new(py, Self(l / self.0))?.to_object(py)))?
})
}

fn __neg__(&self) -> PyResult<Self> {
Ok(Self(-self.0))
fn __neg__(&self) -> Self {
Self(-self.0)
}
}
224 changes: 107 additions & 117 deletions src/python/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ macro_rules! impl_array {

#[pymethods]
impl $struct {
fn __setstate__(&mut self, py: Python, state: PyObject) -> PyResult<()> {
state
.extract::<&PyBytes>(py)
.map(|s| self.0 = deserialize(s.as_bytes()).unwrap())
fn __setstate__(&mut self, state: &Bound<'_, PyBytes>) {
self.0 = deserialize(state.as_bytes()).unwrap();
}

fn __getstate__(&self, py: Python) -> PyObject {
PyBytes::new(py, &serialize(&self.0).unwrap()).to_object(py)
fn __getstate__<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
PyBytes::new_bound(py, &serialize(&self.0).unwrap())
}

pub fn sqrt(&self) -> Result<Self, QuantityError> {
Expand Down Expand Up @@ -71,117 +69,109 @@ macro_rules! impl_array {
Ok(self.0.to_string())
}

fn __add__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(r) = rhs.extract::<PySINumber>() {
return Ok(PyCell::new(py, Self(self.0.clone() + r.0))?.to_object(py));
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok(PyCell::new(py, Self(self.0.clone() + r.0))?.to_object(py));
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
}

fn __sub__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(r) = rhs.extract::<PySINumber>() {
return Ok(PyCell::new(py, Self(self.0.clone() - r.0))?.to_object(py));
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok(PyCell::new(py, Self(self.0.clone() - r.0))?.to_object(py));
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
}

fn __mul__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(r) = rhs.extract::<f64>() {
return Ok(PyCell::new(py, Self(self.0.clone() * r))?.to_object(py));
};
if let Ok(r) = rhs.extract::<PySINumber>() {
let result = self.0.clone() * r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray(py).to_object(py),
Err(_) => PyCell::new(py, Self(result))?.to_object(py),
});
};
if let Ok(r) = rhs.extract::<$numpy_array>() {
return Ok(PyCell::new(py, Self(self.0.clone() * r.to_owned_array()))?
.to_object(py));
};
if let Ok(r) = rhs.extract::<Self>() {
let result = self.0.clone() * r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray(py).to_object(py),
Err(_) => PyCell::new(py, Self(result))?.to_object(py),
});
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
}

fn __rmul__(&self, lhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(l) = lhs.extract::<f64>() {
return Ok(PyCell::new(py, Self(self.0.clone() * l))?.to_object(py));
};
if let Ok(l) = lhs.extract::<$numpy_array>() {
return Ok(PyCell::new(py, Self(l.to_owned_array() * self.0.clone()))?
.to_object(py));
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
}

fn __truediv__(&self, rhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(r) = rhs.extract::<f64>() {
return Ok(PyCell::new(py, Self(self.0.clone() / r))?.to_object(py));
};
if let Ok(r) = rhs.extract::<PySINumber>() {
let result = self.0.clone() / r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray(py).to_object(py),
Err(_) => PyCell::new(py, Self(result))?.to_object(py),
});
};
if let Ok(_) = rhs.extract::<PyCelsius>() {
return Ok((self.0.clone() / CELSIUS).to_pyarray(py).to_object(py));
};
if let Ok(r) = rhs.extract::<$numpy_array>() {
return Ok(PyCell::new(
py,
Self(self.0.clone() * (1.0 / r.to_owned_array())),
)?
.to_object(py));
};
if let Ok(r) = rhs.extract::<Self>() {
let result = self.0.clone() / r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray(py).to_object(py),
Err(_) => PyCell::new(py, Self(result))?.to_object(py),
});
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
}

fn __rtruediv__(&self, lhs: &PyAny) -> PyResult<PyObject> {
Python::with_gil(|py| {
if let Ok(l) = lhs.extract::<f64>() {
return Ok(PyCell::new(py, Self(l / self.0.clone()))?.to_object(py));
};
if let Ok(l) = lhs.extract::<$numpy_array>() {
return Ok(PyCell::new(
py,
Self(1.0 / self.0.clone() * l.to_owned_array()),
)?
.to_object(py));
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
})
fn __add__<'py>(&self, rhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(r) = rhs.extract::<PySINumber>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() + r.0))?.into_any());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() + r.0))?.into_any());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __sub__<'py>(&self, rhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(r) = rhs.extract::<PySINumber>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() - r.0))?.into_any());
};
if let Ok(r) = rhs.extract::<Self>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() - r.0))?.into_any());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __mul__<'py>(&self, rhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() * r))?.into_any());
};
if let Ok(r) = rhs.extract::<PySINumber>() {
let result = self.0.clone() * r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray_bound(rhs.py()).into_any(),
Err(_) => Bound::new(rhs.py(), Self(result))?.into_any(),
});
};
if let Ok(r) = rhs.extract::<$numpy_array>() {
return Ok(
Bound::new(rhs.py(), Self(self.0.clone() * r.to_owned_array()))?.into_any(),
);
};
if let Ok(r) = rhs.extract::<Self>() {
let result = self.0.clone() * r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray_bound(rhs.py()).into_any(),
Err(_) => Bound::new(rhs.py(), Self(result))?.into_any(),
});
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __rmul__<'py>(&self, lhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(l) = lhs.extract::<f64>() {
return Ok(Bound::new(lhs.py(), Self(self.0.clone() * l))?.into_any());
};
if let Ok(l) = lhs.extract::<$numpy_array>() {
return Ok(
Bound::new(lhs.py(), Self(l.to_owned_array() * self.0.clone()))?.into_any(),
);
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __truediv__<'py>(&self, rhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(r) = rhs.extract::<f64>() {
return Ok(Bound::new(rhs.py(), Self(self.0.clone() / r))?.into_any());
};
if let Ok(r) = rhs.extract::<PySINumber>() {
let result = self.0.clone() / r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray_bound(rhs.py()).into_any(),
Err(_) => Bound::new(rhs.py(), Self(result))?.into_any(),
});
};
if let Ok(_) = rhs.extract::<PyCelsius>() {
return Ok((self.0.clone() / CELSIUS)
.to_pyarray_bound(rhs.py())
.into_any());
};
if let Ok(r) = rhs.extract::<$numpy_array>() {
return Ok(Bound::new(
rhs.py(),
Self(self.0.clone() * (1.0 / r.to_owned_array())),
)?
.into_any());
};
if let Ok(r) = rhs.extract::<Self>() {
let result = self.0.clone() / r.0;
return Ok(match result.value() {
Ok(r) => r.to_pyarray_bound(rhs.py()).into_any(),
Err(_) => Bound::new(rhs.py(), Self(result))?.into_any(),
});
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __rtruediv__<'py>(&self, lhs: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
if let Ok(l) = lhs.extract::<f64>() {
return Ok(Bound::new(lhs.py(), Self(l / self.0.clone()))?.into_any());
};
if let Ok(l) = lhs.extract::<$numpy_array>() {
return Ok(Bound::new(
lhs.py(),
Self(1.0 / self.0.clone() * l.to_owned_array()),
)?
.into_any());
};
Err(PyErr::new::<PyTypeError, _>(format!("not implemented!")))
}

fn __pow__(&self, rhs: i32, _mod: Option<u32>) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const G: PySINumber = PySINumber(crate::si::G);
const RGAS: PySINumber = PySINumber(crate::si::RGAS);

#[pymodule]
pub fn quantity(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn quantity(_py: Python<'_>, m: Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;

m.add_class::<PySINumber>()?;
Expand Down
3 changes: 2 additions & 1 deletion src/python/siarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::si::*;
use crate::{QuantityError, Unit};
use bincode::{deserialize, serialize};
use ndarray::{arr1, Array};
use numpy::{PyReadonlyArray1, PyReadonlyArray2, PyReadonlyArray3, PyReadonlyArray4, ToPyArray};
use numpy::prelude::*;
use numpy::{PyReadonlyArray1, PyReadonlyArray2, PyReadonlyArray3, PyReadonlyArray4};
use pyo3::exceptions::{PyIndexError, PyTypeError};
use pyo3::prelude::*;
use pyo3::types::PyBytes;
Expand Down
Loading

0 comments on commit 821cc32

Please sign in to comment.