Skip to content

Commit 3d62344

Browse files
committed
Update for upcoming PyO3 0.9.0
1 parent d164ef3 commit 3d62344

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ libc = "0.2"
1515
num-complex = "0.2"
1616
num-traits = "0.2"
1717
ndarray = ">=0.13"
18-
pyo3 = "0.9.0-alpha.1"
18+
pyo3 = "0.9.0"
1919

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

README.md

Lines changed: 1 addition & 1 deletion
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.9.0-alpha.1"
59+
pyo3 = "0.9.0"
6060
numpy = "0.7.0"
6161
```
6262

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.9.0-alpha.1"
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.9.0-alpha.1"
16+
version = "0.9.0"
1717
features = ["extension-module"]

src/array.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ 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 {}
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 {}
104104

105105
pyobject_native_type_convert!(
106106
PyArray<T, D>,
@@ -170,7 +170,7 @@ impl<T, D> PyArray<T, D> {
170170
/// let not_contiguous: &numpy::PyArray1<f32> = py
171171
/// .eval("np.zeros((3, 5))[::2, 4]", Some(locals), None)
172172
/// .unwrap()
173-
/// .downcast_ref()
173+
/// .downcast()
174174
/// .unwrap();
175175
/// assert!(!not_contiguous.is_contiguous());
176176
/// # }
@@ -390,19 +390,23 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
390390
ID: IntoDimension<Dim = D>,
391391
{
392392
let dims = dims.into_dimension();
393-
let container = SliceBox::new(py, slice).expect("SliceBox creation failed");
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.");
394398
let ptr = PY_ARRAY_API.PyArray_New(
395399
PY_ARRAY_API.get_type_object(npyffi::ArrayType::PyArray_Type),
396400
dims.ndim_cint(),
397401
dims.as_dims_ptr(),
398402
T::typenum_default(),
399403
strides as *mut _, // strides
400-
(*container).data as _, // data
404+
data_ptr as _, // data
401405
mem::size_of::<T>() as i32, // itemsize
402406
0, // flag
403407
::std::ptr::null_mut(), //obj
404408
);
405-
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, container as _);
409+
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, cell as _);
406410
Self::from_owned_ptr(py, ptr)
407411
}
408412

@@ -455,7 +459,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
455459
/// let not_contiguous: &PyArray1<f32> = py
456460
/// .eval("np.zeros((3, 5))[[0, 2], [3, 4]]", Some(locals), None)
457461
/// .unwrap()
458-
/// .downcast_ref()
462+
/// .downcast()
459463
/// .unwrap();
460464
/// assert!(not_contiguous.as_slice().is_err());
461465
/// # }

src/slice_box.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
use pyo3::class::methods::{PyMethodDefType, PyMethodsProtocol};
2-
use pyo3::pyclass::{PyClass, PyClassAlloc, PyClassShell};
2+
use pyo3::pyclass::{PyClass, PyClassAlloc};
33
use pyo3::pyclass_slots::PyClassDummySlot;
4-
use pyo3::{ffi, type_object, types::PyAny, PyClassInitializer, PyResult, Python};
4+
use pyo3::{ffi, type_object, types::PyAny, PyCell, PyClassInitializer};
55

66
pub(crate) struct SliceBox<T> {
77
pub(crate) data: *mut [T],
88
}
99

1010
impl<T> SliceBox<T> {
11-
pub(crate) unsafe fn new(
12-
py: Python<'_>,
13-
value: Box<[T]>,
14-
) -> PyResult<*mut PyClassShell<SliceBox<T>>> {
15-
let value = SliceBox {
11+
pub(crate) fn new(value: Box<[T]>) -> Self {
12+
SliceBox {
1613
data: Box::into_raw(value),
17-
};
18-
PyClassInitializer::from(value).create_shell(py)
14+
}
1915
}
2016
}
2117

@@ -30,22 +26,26 @@ impl<T> PyClassAlloc for SliceBox<T> {}
3026
impl<T> PyClass for SliceBox<T> {
3127
type Dict = PyClassDummySlot;
3228
type WeakRef = PyClassDummySlot;
29+
type BaseNativeType = PyAny;
3330
}
3431

35-
impl<T> type_object::PyTypeInfo for SliceBox<T> {
32+
unsafe impl<T> type_object::PyTypeInfo for SliceBox<T> {
3633
type Type = ();
3734
type BaseType = PyAny;
38-
type ConcreteLayout = PyClassShell<Self>;
35+
type BaseLayout = pyo3::pycell::PyCellBase<PyAny>;
36+
type Layout = PyCell<Self>;
3937
type Initializer = PyClassInitializer<Self>;
38+
type AsRefTarget = PyCell<Self>;
4039
const NAME: &'static str = "SliceBox";
4140
const MODULE: Option<&'static str> = Some("_rust_numpy");
4241
const DESCRIPTION: &'static str = "Memory store for PyArray using rust's Box<[T]>.";
4342
const FLAGS: usize = 0;
4443

4544
#[inline]
46-
unsafe fn type_object() -> &'static mut ffi::PyTypeObject {
47-
static mut TYPE_OBJECT: ::pyo3::ffi::PyTypeObject = ::pyo3::ffi::PyTypeObject_INIT;
48-
&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>()
4949
}
5050
}
5151

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)