Skip to content

Commit 8599efc

Browse files
committed
Mark both PyArray and PyArrayDescr as repr(transparent) to make them strictly layout-compatible with PyAny.
1 parent 8b2679a commit 8599efc

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

src/array.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use ndarray::{
1616
};
1717
use num_traits::AsPrimitive;
1818
use pyo3::{
19-
ffi, pyobject_native_type_named, type_object, types::PyModule, AsPyPointer, FromPyObject,
20-
IntoPy, Py, PyAny, PyClassInitializer, PyDowncastError, PyErr, PyNativeType, PyObject,
21-
PyResult, PyTypeInfo, Python, ToPyObject,
19+
ffi, pyobject_native_type_named, types::PyModule, AsPyPointer, FromPyObject, IntoPy, Py, PyAny,
20+
PyClassInitializer, PyDowncastError, PyErr, PyNativeType, PyObject, PyResult, PyTypeInfo,
21+
Python, ToPyObject,
2222
};
2323

2424
use crate::borrow::{PyReadonlyArray, PyReadwriteArray};
@@ -95,6 +95,7 @@ use crate::slice_container::PySliceContainer;
9595
///
9696
/// [ndarray]: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html
9797
/// [pyo3-memory]: https://pyo3.rs/main/memory.html
98+
#[repr(transparent)]
9899
pub struct PyArray<T, D>(PyAny, PhantomData<T>, PhantomData<D>);
99100

100101
/// Zero-dimensional array.
@@ -119,10 +120,6 @@ pub fn get_array_module(py: Python<'_>) -> PyResult<&PyModule> {
119120
PyModule::import(py, npyffi::array::MOD_NAME)
120121
}
121122

122-
unsafe impl<T, D> type_object::PyLayout<PyArray<T, D>> for npyffi::PyArrayObject {}
123-
124-
impl<T, D> type_object::PySizedLayout<PyArray<T, D>> for npyffi::PyArrayObject {}
125-
126123
unsafe impl<T: Element, D: Dimension> PyTypeInfo for PyArray<T, D> {
127124
type AsRefTarget = Self;
128125

src/dtype.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub use num_complex::{Complex32, Complex64};
4545
/// ```
4646
///
4747
/// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
48+
#[repr(transparent)]
4849
pub struct PyArrayDescr(PyAny);
4950

5051
pyobject_native_type_named!(PyArrayDescr);
@@ -61,12 +62,7 @@ unsafe impl PyTypeInfo for PyArrayDescr {
6162
}
6263

6364
fn is_type_of(ob: &PyAny) -> bool {
64-
unsafe {
65-
ffi::PyObject_TypeCheck(
66-
ob.as_ptr(),
67-
PY_ARRAY_API.get_type_object(ob.py(), NpyTypes::PyArrayDescr_Type),
68-
) > 0
69-
}
65+
unsafe { ffi::PyObject_TypeCheck(ob.as_ptr(), Self::type_object_raw(ob.py())) > 0 }
7066
}
7167
}
7268

0 commit comments

Comments
 (0)