Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable pyo3's gil-refs migration feature #420

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ num-complex = ">= 0.2, < 0.5"
num-integer = "0.1"
num-traits = "0.2"
ndarray = ">= 0.13, < 0.16"
pyo3 = { version = "0.21.0", default-features = false, features = ["gil-refs", "macros"] }
pyo3 = { version = "0.21.0", default-features = false, features = ["macros"] }
rustc-hash = "1.1"

[dev-dependencies]
pyo3 = { version = "0.21.0", default-features = false, features = ["auto-initialize", "gil-refs"] }
pyo3 = { version = "0.21.0", default-features = false, features = ["auto-initialize"] }
nalgebra = { version = "0.32", default-features = false, features = ["std"] }

[package.metadata.docs.rs]
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ numpy = "0.20"
```

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

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

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

let readonly = pyarray.readonly();
let slice = readonly.as_slice()?;
Expand Down
14 changes: 7 additions & 7 deletions examples/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::ops::Add;
use numpy::ndarray::{Array1, ArrayD, ArrayView1, ArrayViewD, ArrayViewMutD, Zip};
use numpy::{
datetime::{units, Timedelta},
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyReadonlyArray1, PyReadonlyArrayDyn,
PyReadwriteArray1, PyReadwriteArrayDyn,
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyArrayMethods, PyReadonlyArray1,
PyReadonlyArrayDyn, PyReadwriteArray1, PyReadwriteArrayDyn,
};
use pyo3::{
exceptions::PyIndexError,
pymodule,
types::{PyDict, PyModule},
types::{PyAnyMethods, PyDict, PyDictMethods, PyModule},
Bound, FromPyObject, PyAny, PyObject, PyResult, Python,
};

Expand Down Expand Up @@ -87,12 +87,12 @@ fn rust_ext<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {

// example of how to extract an array from a dictionary
#[pyfn(m)]
fn extract(d: &PyDict) -> f64 {
fn extract(d: &Bound<'_, PyDict>) -> f64 {
let x = d
.get_item("x")
.unwrap()
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

x.readonly().as_array().sum()
Expand All @@ -117,8 +117,8 @@ fn rust_ext<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
// covering the supported element types and dispatching into a generic implementation.
#[derive(FromPyObject)]
enum SupportedArray<'py> {
F64(&'py PyArray1<f64>),
I64(&'py PyArray1<i64>),
F64(Bound<'py, PyArray1<f64>>),
I64(Bound<'py, PyArray1<i64>>),
}

#[pyfn(m)]
Expand Down
37 changes: 18 additions & 19 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ impl<T, D> PyArray<T, D> {
/// # Safety
///
/// This is a wrapper around [`pyo3::FromPyPointer::from_owned_ptr_or_opt`] and inherits its safety contract.
#[deprecated(since = "0.21.0", note = "use Bound::from_owned_ptr() instead")]
pub unsafe fn from_owned_ptr<'py>(py: Python<'py>, ptr: *mut ffi::PyObject) -> &'py Self {
#[allow(deprecated)]
py.from_owned_ptr(ptr)
}

Expand All @@ -244,7 +246,9 @@ impl<T, D> PyArray<T, D> {
/// # Safety
///
/// This is a wrapper around [`pyo3::FromPyPointer::from_borrowed_ptr_or_opt`] and inherits its safety contract.
#[deprecated(since = "0.21.0", note = "use Bound::from_borrowed_ptr() instead")]
pub unsafe fn from_borrowed_ptr<'py>(py: Python<'py>, ptr: *mut ffi::PyObject) -> &'py Self {
#[allow(deprecated)]
py.from_borrowed_ptr(ptr)
}

Expand All @@ -258,7 +262,7 @@ impl<T, D> PyArray<T, D> {
impl<T: Element, D: Dimension> PyArray<T, D> {
fn extract<'a, 'py, E>(ob: &'a Bound<'py, PyAny>) -> Result<&'a Bound<'py, Self>, E>
where
E: From<DowncastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'a>>,
E: From<DowncastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'py>>,
{
// Check if the object is an array.
let array = unsafe {
Expand Down Expand Up @@ -786,14 +790,14 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
/// # Example
///
/// ```
/// use numpy::PyArray2;
/// use pyo3::Python;
/// use numpy::{PyArray2, PyArrayMethods};
/// use pyo3::{Python, types::PyAnyMethods};
///
/// Python::with_gil(|py| {
/// let pyarray= py
/// .eval("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
/// .eval_bound("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
/// .unwrap()
/// .downcast::<PyArray2<i64>>()
/// .downcast_into::<PyArray2<i64>>()
/// .unwrap();
///
/// assert_eq!(pyarray.to_vec().unwrap(), vec![0, 1, 2, 3]);
Expand Down Expand Up @@ -842,10 +846,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {

/// Get an immutable borrow of the NumPy array
pub fn try_readonly(&self) -> Result<PyReadonlyArray<'_, T, D>, BorrowError> {
// TODO: replace with `Borrowed::to_owned` once
// pyo3#3963 makes it into a release
let bound = &*self.as_borrowed();
PyReadonlyArray::try_new(bound.clone())
PyReadonlyArray::try_new(self.as_borrowed().to_owned())
}

/// Get an immutable borrow of the NumPy array
Expand All @@ -861,10 +862,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {

/// Get a mutable borrow of the NumPy array
pub fn try_readwrite(&self) -> Result<PyReadwriteArray<'_, T, D>, BorrowError> {
// TODO: replace with `Borrowed::to_owned` once
// pyo3#3963 makes it into a release
let bound = &*self.as_borrowed();
PyReadwriteArray::try_new(bound.clone())
PyReadwriteArray::try_new(self.as_borrowed().to_owned())
}

/// Get a mutable borrow of the NumPy array
Expand Down Expand Up @@ -1013,10 +1011,11 @@ impl<D: Dimension> PyArray<PyObject, D> {
///
/// ```
/// use ndarray::array;
/// use pyo3::{pyclass, Py, Python};
/// use pyo3::{pyclass, Py, Python, types::PyAnyMethods};
/// use numpy::{PyArray, PyArrayMethods};
///
/// #[pyclass]
/// # #[allow(dead_code)]
/// struct CustomElement {
/// foo: i32,
/// bar: f64,
Expand All @@ -1036,7 +1035,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
///
/// let pyarray = PyArray::from_owned_object_array_bound(py, array);
///
/// assert!(pyarray.readonly().as_array().get(0).unwrap().as_ref(py).is_instance_of::<CustomElement>());
/// assert!(pyarray.readonly().as_array().get(0).unwrap().bind(py).is_instance_of::<CustomElement>());
/// });
/// ```
pub fn from_owned_object_array_bound<T>(
Expand Down Expand Up @@ -1703,14 +1702,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
/// # Example
///
/// ```
/// use numpy::PyArray2;
/// use pyo3::Python;
/// use numpy::{PyArray2, PyArrayMethods};
/// use pyo3::{Python, types::PyAnyMethods};
///
/// Python::with_gil(|py| {
/// let pyarray= py
/// .eval("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
/// .eval_bound("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
/// .unwrap()
/// .downcast::<PyArray2<i64>>()
/// .downcast_into::<PyArray2<i64>>()
/// .unwrap();
///
/// assert_eq!(pyarray.to_vec().unwrap(), vec![0, 1, 2, 3]);
Expand Down
6 changes: 3 additions & 3 deletions src/array_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ where
.get_or_try_init(py, || {
get_array_module(py)?.getattr("asarray").map(Into::into)
})?
.as_ref(py);
.bind(py);

let kwargs = if C::VAL {
let kwargs = PyDict::new(py);
let kwargs = PyDict::new_bound(py);
kwargs.set_item(intern!(py, "dtype"), T::get_dtype_bound(py))?;
Some(kwargs)
} else {
None
};

let array = as_array.call((ob,), kwargs)?.extract()?;
let array = as_array.call((ob,), kwargs.as_ref())?.extract()?;
Ok(Self(array, PhantomData))
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/borrow/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,11 @@ mod tests {
array.as_ptr().cast::<c_void>()
);

let locals = [("view1", &view1)].into_py_dict(py);
let locals = [("view1", &view1)].into_py_dict_bound(py);
let view2 = py
.eval("view1[:,0]", None, Some(locals))
.eval_bound("view1[:,0]", None, Some(&locals))
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();
assert_ne!(
view2.as_ptr().cast::<c_void>(),
Expand Down Expand Up @@ -819,12 +819,12 @@ mod tests {
let array = PyArray::<f64, _>::zeros_bound(py, 10, false);
let base = base_address(py, array.as_array_ptr());

let locals = [("array", array)].into_py_dict(py);
let locals = [("array", array)].into_py_dict_bound(py);

let view1 = py
.eval("array[:5]", None, Some(locals))
.eval_bound("array[:5]", None, Some(&locals))
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

let key1 = borrow_key(view1.as_array_ptr());
Expand All @@ -842,9 +842,9 @@ mod tests {
}

let view2 = py
.eval("array[5:]", None, Some(locals))
.eval_bound("array[5:]", None, Some(&locals))
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

let key2 = borrow_key(view2.as_array_ptr());
Expand All @@ -865,9 +865,9 @@ mod tests {
}

let view3 = py
.eval("array[5:]", None, Some(locals))
.eval_bound("array[5:]", None, Some(&locals))
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

let key3 = borrow_key(view3.as_array_ptr());
Expand All @@ -891,9 +891,9 @@ mod tests {
}

let view4 = py
.eval("array[7:]", None, Some(locals))
.eval_bound("array[7:]", None, Some(&locals))
.unwrap()
.downcast::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

let key4 = borrow_key(view4.as_array_ptr());
Expand Down
36 changes: 18 additions & 18 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
//! # Example
//!
//! ```
//! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1};
//! use pyo3::Python;
//! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1, PyArrayMethods};
//! use pyo3::{Python, types::PyAnyMethods};
//! # use pyo3::types::PyDict;
//!
//! Python::with_gil(|py| {
//! # let locals = py
//! # .eval("{ 'np': __import__('numpy') }", None, None)
//! # .eval_bound("{ 'np': __import__('numpy') }", None, None)
//! # .unwrap()
//! # .downcast::<PyDict>()
//! # .downcast_into::<PyDict>()
//! # .unwrap();
//! #
//! let array = py
//! .eval(
//! .eval_bound(
//! "np.array([np.datetime64('2017-04-21')])",
//! None,
//! Some(locals),
//! Some(&locals),
//! )
//! .unwrap()
//! .downcast::<PyArray1<Datetime<units::Days>>>()
//! .downcast_into::<PyArray1<Datetime<units::Days>>>()
//! .unwrap();
//!
//! assert_eq!(
Expand All @@ -37,13 +37,13 @@
//! );
//!
//! let array = py
//! .eval(
//! .eval_bound(
//! "np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])",
//! None,
//! Some(locals),
//! Some(&locals),
//! )
//! .unwrap()
//! .downcast::<PyArray1<Timedelta<units::Days>>>()
//! .downcast_into::<PyArray1<Timedelta<units::Days>>>()
//! .unwrap();
//!
//! assert_eq!(
Expand Down Expand Up @@ -251,7 +251,7 @@ mod tests {

use pyo3::{
py_run,
types::{PyDict, PyModule},
types::{PyAnyMethods, PyDict, PyModule},
};

use crate::array::{PyArray1, PyArrayMethods};
Expand All @@ -260,19 +260,19 @@ mod tests {
fn from_python_to_rust() {
Python::with_gil(|py| {
let locals = py
.eval("{ 'np': __import__('numpy') }", None, None)
.eval_bound("{ 'np': __import__('numpy') }", None, None)
.unwrap()
.downcast::<PyDict>()
.downcast_into::<PyDict>()
.unwrap();

let array = py
.eval(
.eval_bound(
"np.array([np.datetime64('1970-01-01')])",
None,
Some(locals),
Some(&locals),
)
.unwrap()
.downcast::<PyArray1<Datetime<units::Days>>>()
.downcast_into::<PyArray1<Datetime<units::Days>>>()
.unwrap();

let value: i64 = array.get_owned(0).unwrap().into();
Expand All @@ -288,9 +288,9 @@ mod tests {
*array.readwrite().get_mut(0).unwrap() = Timedelta::<units::Minutes>::from(5);

let np = py
.eval("__import__('numpy')", None, None)
.eval_bound("__import__('numpy')", None, None)
.unwrap()
.downcast::<PyModule>()
.downcast_into::<PyModule>()
.unwrap();

py_run!(py, array np, "assert array.dtype == np.dtype('timedelta64[m]')");
Expand Down
Loading
Loading