Skip to content

Commit

Permalink
update pyo3 to 0.23 (#457)
Browse files Browse the repository at this point in the history
* remove `gil-ref` feature

* initial migration to pyo3 0.23

* fix all deprecations

* reintroduce names with `_bound` suffix

* switch from `ToPyObject` to `IntoPyObject`

* bump to pyo3 0.23 release

* fix doc-tests

* add changelog
  • Loading branch information
Icxolu authored Nov 22, 2024
1 parent 9eaa51f commit c386c8e
Show file tree
Hide file tree
Showing 32 changed files with 839 additions and 2,097 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog
- v0.23.0
- Drop support for PyPy 3.7 and 3.8. ([#470](https://github.com/PyO3/rust-numpy/pull/470))
- Require `Element: Sync` as part of the free-threading support in PyO3 0.23 ([#469](https://github.com/PyO3/rust-numpy/pull/469))
- Bump PyO3 dependency to v0.23.0 ([[#457](https://github.com/PyO3/rust-numpy/pull/457)])
- removed the `gil-refs` feature
- reintroduced function names without `_bound` suffix + deprecating the old names
- switched to `IntoPyObject` as trait bound

- v0.22.1
- Fix building on 32-bit Windows. ([#463](https://github.com/PyO3/rust-numpy/pull/463))
- Add `PyReadwriteArray::make_nonwriteable`. ([#462](https://github.com/PyO3/rust-numpy/pull/462))
Expand Down
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "numpy"
version = "0.22.1"
version = "0.23.0-dev"
authors = [
"The rust-numpy Project Developers",
"PyO3 Project and Contributors <https://github.com/PyO3>"
Expand All @@ -22,15 +22,12 @@ num-complex = ">= 0.2, < 0.5"
num-integer = "0.1"
num-traits = "0.2"
ndarray = ">= 0.15, < 0.17"
pyo3 = { version = "0.22.0", default-features = false, features = ["macros"] }
pyo3 = { version = "0.23.0", default-features = false, features = ["macros"] }
rustc-hash = "1.1"

[dev-dependencies]
pyo3 = { version = "0.22.0", default-features = false, features = ["auto-initialize"] }
pyo3 = { version = "0.23.0", default-features = false, features = ["auto-initialize"] }
nalgebra = { version = ">=0.30, <0.34", default-features = false, features = ["std"] }

[package.metadata.docs.rs]
all-features = true

[features]
gil-refs = ["pyo3/gil-refs"]
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn rust_ext<'py>(_py: Python<'py>, m: &Bound<'py, PyModule>) -> PyResult<()> {
let x = x.as_array();
let y = y.as_array();
let z = axpy(a, x, y);
z.into_pyarray_bound(py)
z.into_pyarray(py)
}

// wrapper of `mult`
Expand Down Expand Up @@ -99,15 +99,15 @@ numpy = "0.22"

```rust
use numpy::{PyArray1, PyArrayMethods};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python, ffi::c_str};

fn main() -> PyResult<()> {
Python::with_gil(|py| {
let np = py.import_bound("numpy")?;
let locals = [("np", np)].into_py_dict_bound(py);
let np = py.import("numpy")?;
let locals = [("np", np)].into_py_dict(py)?;

let pyarray = py
.eval_bound("np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&locals), None)?
.eval(c_str!("np.absolute(np.array([-1, -2, -3], dtype='int32'))"), Some(&locals), None)?
.downcast_into::<PyArray1<i32>>()?;

let readonly = pyarray.readonly();
Expand Down
20 changes: 10 additions & 10 deletions benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use test::{black_box, Bencher};
use std::ops::Range;

use numpy::{PyArray1, PyArray2, PyArray3};
use pyo3::{types::PyAnyMethods, Bound, Python, ToPyObject};
use pyo3::{types::PyAnyMethods, Bound, Python};

#[bench]
fn extract_success(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();
let any = PyArray2::<f64>::zeros(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any)
Expand All @@ -24,7 +24,7 @@ fn extract_success(bencher: &mut Bencher) {
#[bench]
fn extract_failure(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();
let any = PyArray2::<f64>::zeros(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any)
Expand All @@ -37,7 +37,7 @@ fn extract_failure(bencher: &mut Bencher) {
#[bench]
fn downcast_success(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();
let any = PyArray2::<f64>::zeros(py, (10, 10), false).into_any();

bencher.iter(|| black_box(&any).downcast::<PyArray2<f64>>().unwrap());
});
Expand All @@ -46,7 +46,7 @@ fn downcast_success(bencher: &mut Bencher) {
#[bench]
fn downcast_failure(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();
let any = PyArray2::<f64>::zeros(py, (10, 10), false).into_any();

bencher.iter(|| black_box(&any).downcast::<PyArray2<f64>>().unwrap_err());
});
Expand All @@ -67,7 +67,7 @@ fn from_iter(bencher: &mut Bencher, size: usize) {
bencher.iter(|| {
let iter = black_box(Iter(0..size));

PyArray1::from_iter_bound(py, iter)
PyArray1::from_iter(py, iter)
});
});
}
Expand All @@ -94,7 +94,7 @@ fn from_slice(bencher: &mut Bencher, size: usize) {
bencher.iter(|| {
let slice = black_box(&vec);

PyArray1::from_slice_bound(py, slice)
PyArray1::from_slice(py, slice)
});
});
}
Expand All @@ -121,7 +121,7 @@ fn from_object_slice(bencher: &mut Bencher, size: usize) {
bencher.iter(|| {
let slice = black_box(&vec);

PyArray1::from_slice_bound(py, slice)
PyArray1::from_slice(py, slice)
});
});
}
Expand All @@ -148,7 +148,7 @@ fn from_vec2(bencher: &mut Bencher, size: usize) {
bencher.iter(|| {
let vec2 = black_box(&vec2);

PyArray2::from_vec2_bound(py, vec2).unwrap()
PyArray2::from_vec2(py, vec2).unwrap()
});
});
}
Expand All @@ -175,7 +175,7 @@ fn from_vec3(bencher: &mut Bencher, size: usize) {
bencher.iter(|| {
let vec3 = black_box(&vec3);

PyArray3::from_vec3_bound(py, vec3).unwrap()
PyArray3::from_vec3(py, vec3).unwrap()
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions benches/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pyo3::Python;
#[bench]
fn initial_shared_borrow(bencher: &mut Bencher) {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros_bound(py, (6, 5, 4, 3, 2, 1), false);
let array = PyArray::<f64, _>::zeros(py, (6, 5, 4, 3, 2, 1), false);

bencher.iter(|| {
let array = black_box(&array);
Expand All @@ -22,7 +22,7 @@ fn initial_shared_borrow(bencher: &mut Bencher) {
#[bench]
fn additional_shared_borrow(bencher: &mut Bencher) {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros_bound(py, (6, 5, 4, 3, 2, 1), false);
let array = PyArray::<f64, _>::zeros(py, (6, 5, 4, 3, 2, 1), false);

let _shared = (0..128).map(|_| array.readonly()).collect::<Vec<_>>();

Expand All @@ -37,7 +37,7 @@ fn additional_shared_borrow(bencher: &mut Bencher) {
#[bench]
fn exclusive_borrow(bencher: &mut Bencher) {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros_bound(py, (6, 5, 4, 3, 2, 1), false);
let array = PyArray::<f64, _>::zeros(py, (6, 5, 4, 3, 2, 1), false);

bencher.iter(|| {
let array = black_box(&array);
Expand Down
73 changes: 14 additions & 59 deletions examples/linalg/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_linalg"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.0", features = ["extension-module"] }
pyo3 = { version = "0.23.0", features = ["extension-module"] }
numpy = { path = "../.." }
ndarray-linalg = { version = "0.14.1", features = ["openblas-system"] }

Expand Down
2 changes: 1 addition & 1 deletion examples/linalg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn rust_linalg<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
let y = x
.inv()
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
Ok(y.into_pyarray_bound(py))
Ok(y.into_pyarray(py))
}
Ok(())
}
2 changes: 1 addition & 1 deletion examples/parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_parallel"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.0", features = ["extension-module", "multiple-pymethods"] }
pyo3 = { version = "0.23.0", features = ["extension-module", "multiple-pymethods"] }
numpy = { path = "../.." }
ndarray = { version = "0.16", features = ["rayon", "blas"] }
blas-src = { version = "0.8", features = ["openblas"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn rust_parallel<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
let x = x.as_array();
let y = y.as_array();
let z = Zip::from(x.rows()).par_map_collect(|row| row.dot(&y));
z.into_pyarray_bound(py)
z.into_pyarray(py)
}
Ok(())
}
2 changes: 1 addition & 1 deletion examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_ext"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22.0", features = ["extension-module", "abi3-py37"] }
pyo3 = { version = "0.23.0", features = ["extension-module", "abi3-py37"] }
numpy = { path = "../.." }

[workspace]
Loading

0 comments on commit c386c8e

Please sign in to comment.