From 507087c315abf9f9a1a526987ba1584150e14e14 Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Fri, 4 Sep 2020 13:40:13 +0200 Subject: [PATCH 1/8] started fixes to allow compilation with current pyo3 master --- Cargo.toml | 2 +- src/error.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e22b53ff..2eab3cf30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ libc = "0.2" num-complex = "0.2" num-traits = "0.2" ndarray = ">=0.13" -pyo3 = "0.11.1" +pyo3 = { git = "https://github.com/PyO3/pyo3" } [features] # In default setting, python version is automatically detected diff --git a/src/error.rs b/src/error.rs index a276a5aa8..872286880 100644 --- a/src/error.rs +++ b/src/error.rs @@ -70,7 +70,7 @@ macro_rules! impl_pyerr { impl std::convert::From<$err_type> for PyErr { fn from(err: $err_type) -> PyErr { - PyErr::from_value::(PyErrValue::from_err_args(err)) + PyErr::from_value::(PyErrValue::from_err_args(err)) } } }; From 28bbe9a84a9e377892ea6d423dd34e4bad7235cc Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Fri, 4 Sep 2020 14:08:43 +0200 Subject: [PATCH 2/8] use PyDowncastError constructor and add Debug for test/array.rs --- src/array.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/array.rs b/src/array.rs index c94ae1331..4f61cbb03 100644 --- a/src/array.rs +++ b/src/array.rs @@ -75,6 +75,7 @@ use crate::types::Element; /// array![[8., 15.], [12., 23.]] /// ); /// ``` +#[derive(Debug)] pub struct PyArray(PyAny, PhantomData, PhantomData); /// One-dimensional array. @@ -131,7 +132,7 @@ impl<'a, T: Element, D: Dimension> FromPyObject<'a> for &'a PyArray { fn extract(ob: &'a PyAny) -> PyResult { let array = unsafe { if npyffi::PyArray_Check(ob.as_ptr()) == 0 { - return Err(PyDowncastError.into()); + return Err(PyDowncastError::new(ob, "Array").into()); } &*(ob as *const PyAny as *const PyArray) }; From 242590a61fc78f7f76a84807fcbb365bc028918b Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Mon, 7 Sep 2020 14:06:21 +0200 Subject: [PATCH 3/8] Removed AsPyRef from test import --- src/array.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/array.rs b/src/array.rs index 4f61cbb03..c8bf7d7a8 100644 --- a/src/array.rs +++ b/src/array.rs @@ -132,7 +132,7 @@ impl<'a, T: Element, D: Dimension> FromPyObject<'a> for &'a PyArray { fn extract(ob: &'a PyAny) -> PyResult { let array = unsafe { if npyffi::PyArray_Check(ob.as_ptr()) == 0 { - return Err(PyDowncastError::new(ob, "Array").into()); + return Err(PyDowncastError::new(ob, "PyArray").into()); } &*(ob as *const PyAny as *const PyArray) }; @@ -207,7 +207,7 @@ impl PyArray { /// /// # Example /// ``` - /// use pyo3::{GILGuard, Python, Py, AsPyRef}; + /// use pyo3::{GILGuard, Python, Py}; /// use numpy::PyArray1; /// fn return_py_array() -> Py> { /// let gil = Python::acquire_gil(); From 11d29cce67328d6ca9c82e3d29cd529ad08edddf Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Sun, 13 Sep 2020 10:24:52 -0400 Subject: [PATCH 4/8] Switch from pyo3::internal_utils::ensure_gil to with_gil In PyO3/PyO3#1115 the internal_utils module was removed from pyo3. There is a note in the review that rust-numpy will need to migrate to using with_gil() instead. This commit makes this change to enable running with pyo3 0.12.0. --- Cargo.toml | 2 +- src/npyffi/array.rs | 7 ++++--- src/npyffi/ufunc.rs | 8 +++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2eab3cf30..6d9829713 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ libc = "0.2" num-complex = "0.2" num-traits = "0.2" ndarray = ">=0.13" -pyo3 = { git = "https://github.com/PyO3/pyo3" } +pyo3 = "0.12.0" [features] # In default setting, python version is automatically detected diff --git a/src/npyffi/array.rs b/src/npyffi/array.rs index 5d1dfc933..db40a4457 100644 --- a/src/npyffi/array.rs +++ b/src/npyffi/array.rs @@ -42,9 +42,10 @@ impl PyArrayAPI { } fn get(&self, offset: isize) -> *const *const c_void { if self.api.get().is_null() { - let ensure_gil = pyo3::internal_utils::ensure_gil(); - let api = get_numpy_api(unsafe { ensure_gil.python() }, MOD_NAME, CAPSULE_NAME); - self.api.set(api); + Python::with_gil(|py| { + let api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); + self.api.set(api); + }) } unsafe { self.api.get().offset(offset) } } diff --git a/src/npyffi/ufunc.rs b/src/npyffi/ufunc.rs index 8e85aab73..df6a21bb8 100644 --- a/src/npyffi/ufunc.rs +++ b/src/npyffi/ufunc.rs @@ -4,6 +4,7 @@ use std::os::raw::*; use std::{cell::Cell, ptr}; use pyo3::ffi::PyObject; +use pyo3::Python; use super::get_numpy_api; use super::objects::*; @@ -28,9 +29,10 @@ impl PyUFuncAPI { } fn get(&self, offset: isize) -> *const *const c_void { if self.api.get().is_null() { - let ensure_gil = pyo3::internal_utils::ensure_gil(); - let api = get_numpy_api(unsafe { ensure_gil.python() }, MOD_NAME, CAPSULE_NAME); - self.api.set(api); + Python::with_gil(|py| { + let api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); + self.api.set(api); + }) } unsafe { self.api.get().offset(offset) } } From 29f1b36fd4dfa5defb6b241b7c92d580d220457e Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Sun, 13 Sep 2020 11:01:52 -0400 Subject: [PATCH 5/8] Finish migration of error.rs This commit finishes the migration of error.rs to use the new exception API in 0.12.0. Most of the work was done on this, just a few little pieces that needed to be updated to conform to the new api. --- src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 872286880..620fbc8b7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ //! Defines error types. use crate::types::DataType; -use pyo3::{exceptions as exc, PyErr, PyErrArguments, PyErrValue, PyObject, Python, ToPyObject}; +use pyo3::{exceptions as exc, PyErr, PyErrArguments, PyObject, Python, ToPyObject}; use std::fmt; /// Represents a dimension and dtype of numpy array. @@ -63,14 +63,14 @@ macro_rules! impl_pyerr { impl std::error::Error for $err_type {} impl PyErrArguments for $err_type { - fn arguments(&self, py: Python) -> PyObject { + fn arguments(self, py: Python) -> PyObject { format!("{}", self).to_object(py) } } impl std::convert::From<$err_type> for PyErr { fn from(err: $err_type) -> PyErr { - PyErr::from_value::(PyErrValue::from_err_args(err)) + exc::PyTypeError::new_err(err) } } }; From 63da099ea00b3f82bd8b410124512788d5532d9c Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Sun, 13 Sep 2020 11:13:00 -0400 Subject: [PATCH 6/8] Update PyO3 version in example Cargo.toml files --- examples/linalg/Cargo.toml | 2 +- examples/simple-extension/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/linalg/Cargo.toml b/examples/linalg/Cargo.toml index 1a0bd1574..f3c5f4f87 100644 --- a/examples/linalg/Cargo.toml +++ b/examples/linalg/Cargo.toml @@ -14,5 +14,5 @@ ndarray = ">= 0.13" ndarray-linalg = { version = "0.12", features = ["openblas"] } [dependencies.pyo3] -version = "0.11.1" +version = "0.12.0" features = ["extension-module"] diff --git a/examples/simple-extension/Cargo.toml b/examples/simple-extension/Cargo.toml index cd5e3e606..a419b828a 100644 --- a/examples/simple-extension/Cargo.toml +++ b/examples/simple-extension/Cargo.toml @@ -13,5 +13,5 @@ numpy = { path = "../.." } ndarray = ">= 0.12" [dependencies.pyo3] -version = "0.11.1" +version = "0.12.0" features = ["extension-module"] From d701b128b288eed4910b1b1bd63fa5d09d5927cd Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Sun, 13 Sep 2020 11:20:28 -0400 Subject: [PATCH 7/8] Add missing semicolon from with_gil() calls --- src/npyffi/array.rs | 2 +- src/npyffi/ufunc.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/npyffi/array.rs b/src/npyffi/array.rs index db40a4457..dfa4bb170 100644 --- a/src/npyffi/array.rs +++ b/src/npyffi/array.rs @@ -45,7 +45,7 @@ impl PyArrayAPI { Python::with_gil(|py| { let api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); self.api.set(api); - }) + }); } unsafe { self.api.get().offset(offset) } } diff --git a/src/npyffi/ufunc.rs b/src/npyffi/ufunc.rs index df6a21bb8..73798a35c 100644 --- a/src/npyffi/ufunc.rs +++ b/src/npyffi/ufunc.rs @@ -32,7 +32,7 @@ impl PyUFuncAPI { Python::with_gil(|py| { let api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); self.api.set(api); - }) + }); } unsafe { self.api.get().offset(offset) } } From bda5de94d55b799283f05b11489ca07d1842f7e1 Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Wed, 16 Sep 2020 16:21:56 +0200 Subject: [PATCH 8/8] Removed Debug, added pyobject_native_type_fmt --- src/array.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array.rs b/src/array.rs index c8bf7d7a8..95d8dad95 100644 --- a/src/array.rs +++ b/src/array.rs @@ -75,7 +75,6 @@ use crate::types::Element; /// array![[8., 15.], [12., 23.]] /// ); /// ``` -#[derive(Debug)] pub struct PyArray(PyAny, PhantomData, PhantomData); /// One-dimensional array. @@ -111,6 +110,7 @@ pyobject_native_type_convert!( ); pyobject_native_type_named!(PyArray, T, D); +pyobject_native_type_fmt!(PyArray, T, D); impl<'a, T, D> std::convert::From<&'a PyArray> for &'a PyAny { fn from(ob: &'a PyArray) -> Self {