Skip to content

Commit 40d472c

Browse files
committed
Update with PyO3 0.11.0
1 parent 6cf7ad8 commit 40d472c

File tree

7 files changed

+19
-26
lines changed

7 files changed

+19
-26
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ libc = "0.2"
1515
num-complex = "0.2"
1616
num-traits = "0.2"
1717
ndarray = ">=0.13"
18-
pyo3 = "0.10.1"
18+
# pyo3 = "0.10.1"
19+
pyo3 = { git = "https://github.com/PyO3/pyo3.git"}
1920

2021
[features]
2122
# In default setting, python version is automatically detected

src/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<T, D> PyArray<T, D> {
208208
/// # }
209209
/// ```
210210
pub fn to_owned(&self) -> Py<Self> {
211-
unsafe { Py::from_borrowed_ptr(self.as_ptr()) }
211+
unsafe { Py::from_borrowed_ptr(self.py(), self.as_ptr()) }
212212
}
213213

214214
/// Constructs `PyArray` from raw python object without incrementing reference counts.

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
//! );
2929
//! }
3030
//! ```
31-
#![feature(specialization)]
32-
#![deny(rust_2018_idioms)]
3331
3432
#[macro_use]
3533
extern crate cfg_if;

src/npyffi/array.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use libc::FILE;
33
use pyo3::ffi::{self, PyObject, PyTypeObject};
44
use std::os::raw::*;
5-
use std::{cell::Cell, ptr, sync::Once};
5+
use std::{cell::Cell, ptr};
66

77
use crate::npyffi::*;
88

@@ -33,21 +33,20 @@ pub static PY_ARRAY_API: PyArrayAPI = PyArrayAPI::new();
3333

3434
/// See [PY_ARRAY_API] for more.
3535
pub struct PyArrayAPI {
36-
once: Once,
3736
api: Cell<*const *const c_void>,
3837
}
3938

4039
impl PyArrayAPI {
4140
const fn new() -> Self {
4241
Self {
43-
once: Once::new(),
4442
api: Cell::new(ptr::null_mut()),
4543
}
4644
}
4745
fn get(&self, offset: isize) -> *const *const c_void {
4846
if self.api.get().is_null() {
49-
let api = get_numpy_api(MOD_NAME, CAPSULE_NAME);
50-
self.once.call_once(|| self.api.set(api));
47+
let ensure_gil = pyo3::internal_utils::ensure_gil();
48+
let api = get_numpy_api(unsafe { ensure_gil.python() }, MOD_NAME, CAPSULE_NAME);
49+
self.api.set(api);
5150
}
5251
unsafe { self.api.get().offset(offset) }
5352
}

src/npyffi/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
//! https://numpy.org/doc/stable/reference/c-api
44
#![allow(non_camel_case_types)]
55

6-
use pyo3::ffi;
6+
use pyo3::{ffi, Python};
77
use std::ffi::CString;
88
use std::os::raw::c_void;
99
use std::ptr::null_mut;
1010

11-
fn get_numpy_api(module: &str, capsule: &str) -> *const *const c_void {
11+
fn get_numpy_api(_py: Python, module: &str, capsule: &str) -> *const *const c_void {
1212
let module = CString::new(module).unwrap();
1313
let capsule = CString::new(capsule).unwrap();
1414
unsafe {
15-
assert_ne!(
16-
ffi::Py_IsInitialized(),
17-
0,
18-
r"Numpy API is called before initializing Python!
19-
Please make sure that you get gil, by `let gil = Python::acquire_gil();`"
20-
);
2115
let numpy = ffi::PyImport_ImportModule(module.as_ptr());
2216
assert!(!numpy.is_null(), "Failed to import numpy module");
2317
let capsule = ffi::PyObject_GetAttrString(numpy as _, capsule.as_ptr());

src/npyffi/ufunc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Low-Level binding for [UFunc API](https://numpy.org/doc/stable/reference/c-api/ufunc.html)
22
33
use std::os::raw::*;
4-
use std::{cell::Cell, ptr, sync::Once};
4+
use std::{cell::Cell, ptr};
55

66
use pyo3::ffi::PyObject;
77

@@ -17,21 +17,20 @@ const CAPSULE_NAME: &str = "_UFUNC_API";
1717
pub static PY_UFUNC_API: PyUFuncAPI = PyUFuncAPI::new();
1818

1919
pub struct PyUFuncAPI {
20-
once: Once,
2120
api: Cell<*const *const c_void>,
2221
}
2322

2423
impl PyUFuncAPI {
2524
const fn new() -> Self {
2625
Self {
27-
once: Once::new(),
2826
api: Cell::new(ptr::null_mut()),
2927
}
3028
}
3129
fn get(&self, offset: isize) -> *const *const c_void {
3230
if self.api.get().is_null() {
33-
let api = get_numpy_api(MOD_NAME, CAPSULE_NAME);
34-
self.once.call_once(|| self.api.set(api));
31+
let ensure_gil = pyo3::internal_utils::ensure_gil();
32+
let api = get_numpy_api(unsafe { ensure_gil.python() }, MOD_NAME, CAPSULE_NAME);
33+
self.api.set(api);
3534
}
3635
unsafe { self.api.get().offset(offset) }
3736
}

src/slice_box.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pyo3::class::methods::PyMethodsImpl;
1+
use pyo3::class::{methods::PyMethods, proto_methods::PyProtoMethods};
22
use pyo3::pyclass::{PyClass, PyClassAlloc};
33
use pyo3::pyclass_slots::PyClassDummySlot;
44
use pyo3::{ffi, type_object, types::PyAny, PyCell, PyClassInitializer};
@@ -42,11 +42,13 @@ unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> {
4242
const FLAGS: usize = 0;
4343

4444
#[inline]
45-
fn type_object() -> &'static ffi::PyTypeObject {
45+
fn type_object_raw(py: pyo3::Python) -> *mut ffi::PyTypeObject {
4646
use pyo3::type_object::LazyStaticType;
4747
static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
48-
TYPE_OBJECT.get_or_init::<Self>()
48+
TYPE_OBJECT.get_or_init::<Self>(py)
4949
}
5050
}
5151

52-
impl<T> PyMethodsImpl for SliceBox<T> {}
52+
impl<T> PyMethods for SliceBox<T> {}
53+
impl<T> PyProtoMethods for SliceBox<T> {}
54+
unsafe impl<T> Send for SliceBox<T> {}

0 commit comments

Comments
 (0)