Skip to content

Commit 8af258b

Browse files
committed
Update PyO3 to 0.9.0
1 parent 15e15aa commit 8af258b

File tree

6 files changed

+41
-50
lines changed

6 files changed

+41
-50
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ cfg-if = "0.1"
1414
libc = "0.2"
1515
num-complex = "0.2"
1616
num-traits = "0.2"
17-
ndarray = ">=0.12"
18-
pyo3 = "0.8"
17+
ndarray = ">=0.13"
18+
pyo3 = { git = "https://github.com/PyO3/pyo3.git" }
1919

2020
[features]
2121
# In default setting, python version is automatically detected

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ using [setuptools-rust](https://github.com/PyO3/setuptools-rust).
5656
name = "numpy-test"
5757

5858
[dependencies]
59-
pyo3 = "0.8"
59+
pyo3 = "0.9.0-alpha.1"
6060
numpy = "0.7.0"
6161
```
6262

@@ -102,7 +102,7 @@ numpy = "0.7.0"
102102
ndarray = "0.13"
103103

104104
[dependencies.pyo3]
105-
version = "0.8"
105+
version = "0.9.0-alpha.1"
106106
features = ["extension-module"]
107107
```
108108

examples/linalg/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ ndarray = ">= 0.12"
1414
ndarray-linalg = { version = "0.10", features = ["openblas"] }
1515

1616
[dependencies.pyo3]
17-
version = "0.8"
17+
version = "0.9.0-alpha.1"
1818
features = ["extension-module"]

examples/simple-extension/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ numpy = { path = "../.." }
1313
ndarray = ">= 0.12"
1414

1515
[dependencies.pyo3]
16-
version = "0.8"
16+
version = "0.9.0-alpha.1"
1717
features = ["extension-module"]

src/array.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API};
33
use ndarray::*;
44
use num_traits::AsPrimitive;
5-
use pyo3::{ffi, prelude::*, types::PyAny};
5+
use pyo3::{ffi, prelude::*, type_object, types::PyAny};
66
use pyo3::{AsPyPointer, PyDowncastError, PyNativeType};
77
use std::iter::ExactSizeIterator;
88
use std::marker::PhantomData;
@@ -99,8 +99,12 @@ pub fn get_array_module(py: Python<'_>) -> PyResult<&PyModule> {
9999
PyModule::import(py, npyffi::array::MOD_NAME)
100100
}
101101

102+
impl<T, D> type_object::PyObjectLayout<PyArray<T, D>> for npyffi::PyArrayObject {}
103+
impl<T, D> type_object::PyObjectSizedLayout<PyArray<T, D>> for npyffi::PyArrayObject {}
104+
102105
pyobject_native_type_convert!(
103106
PyArray<T, D>,
107+
npyffi::PyArrayObject,
104108
*npyffi::PY_ARRAY_API.get_type_object(npyffi::ArrayType::PyArray_Type),
105109
Some("numpy"),
106110
npyffi::PyArray_Check,
@@ -386,19 +390,19 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
386390
ID: IntoDimension<Dim = D>,
387391
{
388392
let dims = dims.into_dimension();
389-
let slice = SliceBox::new(slice);
393+
let container = SliceBox::new(py, slice).expect("SliceBox creation failed");
390394
let ptr = PY_ARRAY_API.PyArray_New(
391395
PY_ARRAY_API.get_type_object(npyffi::ArrayType::PyArray_Type),
392396
dims.ndim_cint(),
393397
dims.as_dims_ptr(),
394398
T::typenum_default(),
395399
strides as *mut _, // strides
396-
slice.data(), // data
400+
(*container).data as _, // data
397401
mem::size_of::<T>() as i32, // itemsize
398402
0, // flag
399403
::std::ptr::null_mut(), //obj
400404
);
401-
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, slice.as_ptr());
405+
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, container as _);
402406
Self::from_owned_ptr(py, ptr)
403407
}
404408

src/slice_box.rs

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
use pyo3::class::methods::{PyMethodDefType, PyMethodsProtocol};
2-
use pyo3::{ffi, type_object, types::PyAny, AsPyPointer, PyObjectAlloc, Python};
3-
use std::os::raw::c_void;
2+
use pyo3::pyclass::{PyClass, PyClassAlloc, PyClassShell};
3+
use pyo3::pyclass_slots::PyClassDummySlot;
4+
use pyo3::{ffi, type_object, types::PyAny, PyClassInitializer, PyResult, Python};
45

5-
/// It's a memory store for IntoPyArray.
6-
/// See IntoPyArray's doc for what concretely this type is for.
7-
#[repr(C)]
86
pub(crate) struct SliceBox<T> {
9-
ob_base: ffi::PyObject,
10-
inner: *mut [T],
7+
pub(crate) data: *mut [T],
118
}
129

1310
impl<T> SliceBox<T> {
14-
pub(crate) unsafe fn new<'a>(box_: Box<[T]>) -> &'a Self {
15-
let type_ob = <Self as type_object::PyTypeObject>::init_type().as_ptr();
16-
let base = ffi::_PyObject_New(type_ob);
17-
*base = ffi::PyObject_HEAD_INIT;
18-
(*base).ob_type = type_ob;
19-
let self_ = base as *mut SliceBox<T>;
20-
(*self_).inner = Box::into_raw(box_);
21-
&*self_
11+
pub(crate) unsafe fn new(
12+
py: Python<'_>,
13+
value: Box<[T]>,
14+
) -> PyResult<*mut PyClassShell<SliceBox<T>>> {
15+
let value = SliceBox {
16+
data: Box::into_raw(value),
17+
};
18+
PyClassInitializer::from(value).create_shell(py)
2219
}
23-
pub(crate) fn data(&self) -> *mut c_void {
24-
self.inner as *mut c_void
20+
}
21+
22+
impl<T> Drop for SliceBox<T> {
23+
fn drop(&mut self) {
24+
let _boxed_slice = unsafe { Box::from_raw(self.data) };
2525
}
2626
}
2727

28+
impl<T> PyClassAlloc for SliceBox<T> {}
29+
30+
impl<T> PyClass for SliceBox<T> {
31+
type Dict = PyClassDummySlot;
32+
type WeakRef = PyClassDummySlot;
33+
}
34+
2835
impl<T> type_object::PyTypeInfo for SliceBox<T> {
2936
type Type = ();
3037
type BaseType = PyAny;
38+
type ConcreteLayout = PyClassShell<Self>;
39+
type Initializer = PyClassInitializer<Self>;
3140
const NAME: &'static str = "SliceBox";
3241
const MODULE: Option<&'static str> = Some("_rust_numpy");
3342
const DESCRIPTION: &'static str = "Memory store for PyArray using rust's Box<[T]>.";
3443
const FLAGS: usize = 0;
35-
const SIZE: usize = std::mem::size_of::<Self>();
36-
const OFFSET: isize = 0;
44+
3745
#[inline]
3846
unsafe fn type_object() -> &'static mut ffi::PyTypeObject {
3947
static mut TYPE_OBJECT: ::pyo3::ffi::PyTypeObject = ::pyo3::ffi::PyTypeObject_INIT;
@@ -46,24 +54,3 @@ impl<T> PyMethodsProtocol for SliceBox<T> {
4654
Vec::new()
4755
}
4856
}
49-
50-
impl<T> AsPyPointer for SliceBox<T> {
51-
#[inline]
52-
fn as_ptr(&self) -> *mut ffi::PyObject {
53-
&self.ob_base as *const _ as *mut _
54-
}
55-
}
56-
57-
impl<T> PyObjectAlloc for SliceBox<T> {
58-
/// Calls the rust destructor for the object.
59-
unsafe fn drop(py: Python<'_>, obj: *mut ffi::PyObject) {
60-
let data = (*(obj as *mut SliceBox<T>)).inner;
61-
let boxed_slice = Box::from_raw(data);
62-
drop(boxed_slice);
63-
<Self as type_object::PyTypeInfo>::BaseType::drop(py, obj);
64-
}
65-
unsafe fn dealloc(py: Python<'_>, obj: *mut ffi::PyObject) {
66-
Self::drop(py, obj);
67-
ffi::PyObject_Free(obj as *mut c_void);
68-
}
69-
}

0 commit comments

Comments
 (0)