Skip to content

Commit 6811345

Browse files
authored
Merge pull request #124 from PyO3/v0.8.0
Update PyO3 to 0.9.0
2 parents 5138ba5 + ce3a106 commit 6811345

File tree

7 files changed

+55
-60
lines changed

7 files changed

+55
-60
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 = "0.9.0"
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"
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
numpy = { path = "../.." }
13-
ndarray = ">= 0.12"
14-
ndarray-linalg = { version = "0.10", features = ["openblas"] }
13+
ndarray = ">= 0.13"
14+
ndarray-linalg = { version = "0.12", features = ["openblas"] }
1515

1616
[dependencies.pyo3]
17-
version = "0.8"
17+
version = "0.9.0"
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"
1717
features = ["extension-module"]

src/array.rs

Lines changed: 14 additions & 6 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+
unsafe impl<T, D> type_object::PyLayout<PyArray<T, D>> for npyffi::PyArrayObject {}
103+
impl<T, D> type_object::PySizedLayout<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,
@@ -166,7 +170,7 @@ impl<T, D> PyArray<T, D> {
166170
/// let not_contiguous: &numpy::PyArray1<f32> = py
167171
/// .eval("np.zeros((3, 5))[::2, 4]", Some(locals), None)
168172
/// .unwrap()
169-
/// .downcast_ref()
173+
/// .downcast()
170174
/// .unwrap();
171175
/// assert!(!not_contiguous.is_contiguous());
172176
/// # }
@@ -386,19 +390,23 @@ 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(slice);
394+
let data_ptr = container.data;
395+
let cell = pyo3::PyClassInitializer::from(container)
396+
.create_cell(py)
397+
.expect("Object creation failed.");
390398
let ptr = PY_ARRAY_API.PyArray_New(
391399
PY_ARRAY_API.get_type_object(npyffi::ArrayType::PyArray_Type),
392400
dims.ndim_cint(),
393401
dims.as_dims_ptr(),
394402
T::typenum_default(),
395403
strides as *mut _, // strides
396-
slice.data(), // data
404+
data_ptr as _, // data
397405
mem::size_of::<T>() as i32, // itemsize
398406
0, // flag
399407
::std::ptr::null_mut(), //obj
400408
);
401-
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, slice.as_ptr());
409+
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, cell as _);
402410
Self::from_owned_ptr(py, ptr)
403411
}
404412

@@ -451,7 +459,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
451459
/// let not_contiguous: &PyArray1<f32> = py
452460
/// .eval("np.zeros((3, 5))[[0, 2], [3, 4]]", Some(locals), None)
453461
/// .unwrap()
454-
/// .downcast_ref()
462+
/// .downcast()
455463
/// .unwrap();
456464
/// assert!(not_contiguous.as_slice().is_err());
457465
/// # }

src/slice_box.rs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
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};
3+
use pyo3::pyclass_slots::PyClassDummySlot;
4+
use pyo3::{ffi, type_object, types::PyAny, PyCell, PyClassInitializer};
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) fn new(value: Box<[T]>) -> Self {
12+
SliceBox {
13+
data: Box::into_raw(value),
14+
}
2215
}
23-
pub(crate) fn data(&self) -> *mut c_void {
24-
self.inner as *mut c_void
16+
}
17+
18+
impl<T> Drop for SliceBox<T> {
19+
fn drop(&mut self) {
20+
let _boxed_slice = unsafe { Box::from_raw(self.data) };
2521
}
2622
}
2723

28-
impl<T> type_object::PyTypeInfo for SliceBox<T> {
24+
impl<T> PyClassAlloc for SliceBox<T> {}
25+
26+
impl<T> PyClass for SliceBox<T> {
27+
type Dict = PyClassDummySlot;
28+
type WeakRef = PyClassDummySlot;
29+
type BaseNativeType = PyAny;
30+
}
31+
32+
unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> {
2933
type Type = ();
3034
type BaseType = PyAny;
35+
type BaseLayout = pyo3::pycell::PyCellBase<PyAny>;
36+
type Layout = PyCell<Self>;
37+
type Initializer = PyClassInitializer<Self>;
38+
type AsRefTarget = PyCell<Self>;
3139
const NAME: &'static str = "SliceBox";
3240
const MODULE: Option<&'static str> = Some("_rust_numpy");
33-
const DESCRIPTION: &'static str = "Memory store for PyArray using rust's Box<[T]>.";
41+
const DESCRIPTION: &'static str = "Memory store for PyArray using rust's Box<[T]> \0";
3442
const FLAGS: usize = 0;
35-
const SIZE: usize = std::mem::size_of::<Self>();
36-
const OFFSET: isize = 0;
43+
3744
#[inline]
38-
unsafe fn type_object() -> &'static mut ffi::PyTypeObject {
39-
static mut TYPE_OBJECT: ::pyo3::ffi::PyTypeObject = ::pyo3::ffi::PyTypeObject_INIT;
40-
&mut TYPE_OBJECT
45+
fn type_object() -> &'static ffi::PyTypeObject {
46+
use pyo3::type_object::LazyStaticType;
47+
static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
48+
TYPE_OBJECT.get_or_init::<Self>()
4149
}
4250
}
4351

@@ -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-
}

tests/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn not_contiguous_array<'py>(py: Python<'py>) -> &'py PyArray1<i32> {
1717
None,
1818
)
1919
.unwrap()
20-
.downcast_ref()
20+
.downcast()
2121
.unwrap()
2222
}
2323

0 commit comments

Comments
 (0)