Skip to content

Commit 0b39d09

Browse files
Icxoluadamreichold
authored andcommitted
disable gil-refs feature
1 parent 30f66f4 commit 0b39d09

File tree

12 files changed

+181
-170
lines changed

12 files changed

+181
-170
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ num-complex = ">= 0.2, < 0.5"
2222
num-integer = "0.1"
2323
num-traits = "0.2"
2424
ndarray = ">= 0.13, < 0.16"
25-
pyo3 = { version = "0.21.0", default-features = false, features = ["gil-refs", "macros"] }
25+
pyo3 = { version = "0.21.0", default-features = false, features = ["macros"] }
2626
rustc-hash = "1.1"
2727

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

3232
[package.metadata.docs.rs]

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ numpy = "0.20"
9898
```
9999

100100
```rust
101-
use numpy::PyArray1;
102-
use pyo3::{types::IntoPyDict, PyResult, Python};
101+
use numpy::{PyArray1, PyArrayMethods};
102+
use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python};
103103

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

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

113113
let readonly = pyarray.readonly();
114114
let slice = readonly.as_slice()?;

examples/simple/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use std::ops::Add;
33
use numpy::ndarray::{Array1, ArrayD, ArrayView1, ArrayViewD, ArrayViewMutD, Zip};
44
use numpy::{
55
datetime::{units, Timedelta},
6-
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyReadonlyArray1, PyReadonlyArrayDyn,
7-
PyReadwriteArray1, PyReadwriteArrayDyn,
6+
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyArrayMethods, PyReadonlyArray1,
7+
PyReadonlyArrayDyn, PyReadwriteArray1, PyReadwriteArrayDyn,
88
};
99
use pyo3::{
1010
exceptions::PyIndexError,
1111
pymodule,
12-
types::{PyDict, PyModule},
12+
types::{PyAnyMethods, PyDict, PyDictMethods, PyModule},
1313
Bound, FromPyObject, PyAny, PyObject, PyResult, Python,
1414
};
1515

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

8888
// example of how to extract an array from a dictionary
8989
#[pyfn(m)]
90-
fn extract(d: &PyDict) -> f64 {
90+
fn extract(d: &Bound<'_, PyDict>) -> f64 {
9191
let x = d
9292
.get_item("x")
9393
.unwrap()
9494
.unwrap()
95-
.downcast::<PyArray1<f64>>()
95+
.downcast_into::<PyArray1<f64>>()
9696
.unwrap();
9797

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

124124
#[pyfn(m)]

src/array.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ impl<T, D> PyArray<T, D> {
235235
/// # Safety
236236
///
237237
/// This is a wrapper around [`pyo3::FromPyPointer::from_owned_ptr_or_opt`] and inherits its safety contract.
238+
#[deprecated(since = "0.21.0", note = "use Bound::from_owned_ptr() instead")]
238239
pub unsafe fn from_owned_ptr<'py>(py: Python<'py>, ptr: *mut ffi::PyObject) -> &'py Self {
240+
#[allow(deprecated)]
239241
py.from_owned_ptr(ptr)
240242
}
241243

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

@@ -258,7 +262,7 @@ impl<T, D> PyArray<T, D> {
258262
impl<T: Element, D: Dimension> PyArray<T, D> {
259263
fn extract<'a, 'py, E>(ob: &'a Bound<'py, PyAny>) -> Result<&'a Bound<'py, Self>, E>
260264
where
261-
E: From<DowncastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'a>>,
265+
E: From<DowncastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'py>>,
262266
{
263267
// Check if the object is an array.
264268
let array = unsafe {
@@ -786,14 +790,14 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
786790
/// # Example
787791
///
788792
/// ```
789-
/// use numpy::PyArray2;
790-
/// use pyo3::Python;
793+
/// use numpy::{PyArray2, PyArrayMethods};
794+
/// use pyo3::{Python, types::PyAnyMethods};
791795
///
792796
/// Python::with_gil(|py| {
793797
/// let pyarray= py
794-
/// .eval("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
798+
/// .eval_bound("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
795799
/// .unwrap()
796-
/// .downcast::<PyArray2<i64>>()
800+
/// .downcast_into::<PyArray2<i64>>()
797801
/// .unwrap();
798802
///
799803
/// assert_eq!(pyarray.to_vec().unwrap(), vec![0, 1, 2, 3]);
@@ -842,10 +846,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
842846

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

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

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

870868
/// Get a mutable borrow of the NumPy array
@@ -1013,10 +1011,11 @@ impl<D: Dimension> PyArray<PyObject, D> {
10131011
///
10141012
/// ```
10151013
/// use ndarray::array;
1016-
/// use pyo3::{pyclass, Py, Python};
1014+
/// use pyo3::{pyclass, Py, Python, types::PyAnyMethods};
10171015
/// use numpy::{PyArray, PyArrayMethods};
10181016
///
10191017
/// #[pyclass]
1018+
/// # #[allow(dead_code)]
10201019
/// struct CustomElement {
10211020
/// foo: i32,
10221021
/// bar: f64,
@@ -1036,7 +1035,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
10361035
///
10371036
/// let pyarray = PyArray::from_owned_object_array_bound(py, array);
10381037
///
1039-
/// assert!(pyarray.readonly().as_array().get(0).unwrap().as_ref(py).is_instance_of::<CustomElement>());
1038+
/// assert!(pyarray.readonly().as_array().get(0).unwrap().bind(py).is_instance_of::<CustomElement>());
10401039
/// });
10411040
/// ```
10421041
pub fn from_owned_object_array_bound<T>(
@@ -1703,14 +1702,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
17031702
/// # Example
17041703
///
17051704
/// ```
1706-
/// use numpy::PyArray2;
1707-
/// use pyo3::Python;
1705+
/// use numpy::{PyArray2, PyArrayMethods};
1706+
/// use pyo3::{Python, types::PyAnyMethods};
17081707
///
17091708
/// Python::with_gil(|py| {
17101709
/// let pyarray= py
1711-
/// .eval("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
1710+
/// .eval_bound("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None)
17121711
/// .unwrap()
1713-
/// .downcast::<PyArray2<i64>>()
1712+
/// .downcast_into::<PyArray2<i64>>()
17141713
/// .unwrap();
17151714
///
17161715
/// assert_eq!(pyarray.to_vec().unwrap(), vec![0, 1, 2, 3]);

src/array_like.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ where
166166
.get_or_try_init(py, || {
167167
get_array_module(py)?.getattr("asarray").map(Into::into)
168168
})?
169-
.as_ref(py);
169+
.bind(py);
170170

171171
let kwargs = if C::VAL {
172-
let kwargs = PyDict::new(py);
172+
let kwargs = PyDict::new_bound(py);
173173
kwargs.set_item(intern!(py, "dtype"), T::get_dtype_bound(py))?;
174174
Some(kwargs)
175175
} else {
176176
None
177177
};
178178

179-
let array = as_array.call((ob,), kwargs)?.extract()?;
179+
let array = as_array.call((ob,), kwargs.as_ref())?.extract()?;
180180
Ok(Self(array, PhantomData))
181181
}
182182
}

src/borrow/shared.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,11 @@ mod tests {
574574
array.as_ptr().cast::<c_void>()
575575
);
576576

577-
let locals = [("view1", &view1)].into_py_dict(py);
577+
let locals = [("view1", &view1)].into_py_dict_bound(py);
578578
let view2 = py
579-
.eval("view1[:,0]", None, Some(locals))
579+
.eval_bound("view1[:,0]", None, Some(&locals))
580580
.unwrap()
581-
.downcast::<PyArray1<f64>>()
581+
.downcast_into::<PyArray1<f64>>()
582582
.unwrap();
583583
assert_ne!(
584584
view2.as_ptr().cast::<c_void>(),
@@ -819,12 +819,12 @@ mod tests {
819819
let array = PyArray::<f64, _>::zeros_bound(py, 10, false);
820820
let base = base_address(py, array.as_array_ptr());
821821

822-
let locals = [("array", array)].into_py_dict(py);
822+
let locals = [("array", array)].into_py_dict_bound(py);
823823

824824
let view1 = py
825-
.eval("array[:5]", None, Some(locals))
825+
.eval_bound("array[:5]", None, Some(&locals))
826826
.unwrap()
827-
.downcast::<PyArray1<f64>>()
827+
.downcast_into::<PyArray1<f64>>()
828828
.unwrap();
829829

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

844844
let view2 = py
845-
.eval("array[5:]", None, Some(locals))
845+
.eval_bound("array[5:]", None, Some(&locals))
846846
.unwrap()
847-
.downcast::<PyArray1<f64>>()
847+
.downcast_into::<PyArray1<f64>>()
848848
.unwrap();
849849

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

867867
let view3 = py
868-
.eval("array[5:]", None, Some(locals))
868+
.eval_bound("array[5:]", None, Some(&locals))
869869
.unwrap()
870-
.downcast::<PyArray1<f64>>()
870+
.downcast_into::<PyArray1<f64>>()
871871
.unwrap();
872872

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

893893
let view4 = py
894-
.eval("array[7:]", None, Some(locals))
894+
.eval_bound("array[7:]", None, Some(&locals))
895895
.unwrap()
896-
.downcast::<PyArray1<f64>>()
896+
.downcast_into::<PyArray1<f64>>()
897897
.unwrap();
898898

899899
let key4 = borrow_key(view4.as_array_ptr());

src/datetime.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
//! # Example
1111
//!
1212
//! ```
13-
//! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1};
14-
//! use pyo3::Python;
13+
//! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1, PyArrayMethods};
14+
//! use pyo3::{Python, types::PyAnyMethods};
1515
//! # use pyo3::types::PyDict;
1616
//!
1717
//! Python::with_gil(|py| {
1818
//! # let locals = py
19-
//! # .eval("{ 'np': __import__('numpy') }", None, None)
19+
//! # .eval_bound("{ 'np': __import__('numpy') }", None, None)
2020
//! # .unwrap()
21-
//! # .downcast::<PyDict>()
21+
//! # .downcast_into::<PyDict>()
2222
//! # .unwrap();
2323
//! #
2424
//! let array = py
25-
//! .eval(
25+
//! .eval_bound(
2626
//! "np.array([np.datetime64('2017-04-21')])",
2727
//! None,
28-
//! Some(locals),
28+
//! Some(&locals),
2929
//! )
3030
//! .unwrap()
31-
//! .downcast::<PyArray1<Datetime<units::Days>>>()
31+
//! .downcast_into::<PyArray1<Datetime<units::Days>>>()
3232
//! .unwrap();
3333
//!
3434
//! assert_eq!(
@@ -37,13 +37,13 @@
3737
//! );
3838
//!
3939
//! let array = py
40-
//! .eval(
40+
//! .eval_bound(
4141
//! "np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])",
4242
//! None,
43-
//! Some(locals),
43+
//! Some(&locals),
4444
//! )
4545
//! .unwrap()
46-
//! .downcast::<PyArray1<Timedelta<units::Days>>>()
46+
//! .downcast_into::<PyArray1<Timedelta<units::Days>>>()
4747
//! .unwrap();
4848
//!
4949
//! assert_eq!(
@@ -251,7 +251,7 @@ mod tests {
251251

252252
use pyo3::{
253253
py_run,
254-
types::{PyDict, PyModule},
254+
types::{PyAnyMethods, PyDict, PyModule},
255255
};
256256

257257
use crate::array::{PyArray1, PyArrayMethods};
@@ -260,19 +260,19 @@ mod tests {
260260
fn from_python_to_rust() {
261261
Python::with_gil(|py| {
262262
let locals = py
263-
.eval("{ 'np': __import__('numpy') }", None, None)
263+
.eval_bound("{ 'np': __import__('numpy') }", None, None)
264264
.unwrap()
265-
.downcast::<PyDict>()
265+
.downcast_into::<PyDict>()
266266
.unwrap();
267267

268268
let array = py
269-
.eval(
269+
.eval_bound(
270270
"np.array([np.datetime64('1970-01-01')])",
271271
None,
272-
Some(locals),
272+
Some(&locals),
273273
)
274274
.unwrap()
275-
.downcast::<PyArray1<Datetime<units::Days>>>()
275+
.downcast_into::<PyArray1<Datetime<units::Days>>>()
276276
.unwrap();
277277

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

290290
let np = py
291-
.eval("__import__('numpy')", None, None)
291+
.eval_bound("__import__('numpy')", None, None)
292292
.unwrap()
293-
.downcast::<PyModule>()
293+
.downcast_into::<PyModule>()
294294
.unwrap();
295295

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

0 commit comments

Comments
 (0)