Skip to content

Commit

Permalink
Merge pull request #152 from g-bauer/pyo3_0.12_compatible
Browse files Browse the repository at this point in the history
Update to pyo3 0.12.0
  • Loading branch information
kngwyu authored Sep 18, 2020
2 parents 9d6a6cd + bda5de9 commit bacb424
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libc = "0.2"
num-complex = "0.2"
num-traits = "0.2"
ndarray = ">=0.13"
pyo3 = "0.11.1"
pyo3 = "0.12.0"

[features]
# In default setting, python version is automatically detected
Expand Down
2 changes: 1 addition & 1 deletion examples/linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion examples/simple-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ numpy = { path = "../.." }
ndarray = ">= 0.12"

[dependencies.pyo3]
version = "0.11.1"
version = "0.12.0"
features = ["extension-module"]
5 changes: 3 additions & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pyobject_native_type_convert!(
);

pyobject_native_type_named!(PyArray<T, D>, T, D);
pyobject_native_type_fmt!(PyArray<T, D>, T, D);

impl<'a, T, D> std::convert::From<&'a PyArray<T, D>> for &'a PyAny {
fn from(ob: &'a PyArray<T, D>) -> Self {
Expand All @@ -131,7 +132,7 @@ impl<'a, T: Element, D: Dimension> FromPyObject<'a> for &'a PyArray<T, D> {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let array = unsafe {
if npyffi::PyArray_Check(ob.as_ptr()) == 0 {
return Err(PyDowncastError.into());
return Err(PyDowncastError::new(ob, "PyArray<T, D>").into());
}
&*(ob as *const PyAny as *const PyArray<T, D>)
};
Expand Down Expand Up @@ -206,7 +207,7 @@ impl<T, D> PyArray<T, D> {
///
/// # Example
/// ```
/// use pyo3::{GILGuard, Python, Py, AsPyRef};
/// use pyo3::{GILGuard, Python, Py};
/// use numpy::PyArray1;
/// fn return_py_array() -> Py<PyArray1<i32>> {
/// let gil = Python::acquire_gil();
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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::<exc::TypeError>(PyErrValue::from_err_args(err))
exc::PyTypeError::new_err(err)
}
}
};
Expand Down
7 changes: 4 additions & 3 deletions src/npyffi/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down
8 changes: 5 additions & 3 deletions src/npyffi/ufunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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) }
}
Expand Down

0 comments on commit bacb424

Please sign in to comment.