Skip to content

Commit a475cd8

Browse files
bschoenmaeckersdavidhewitt
authored andcommitted
update dtype
1 parent e531922 commit a475cd8

File tree

2 files changed

+44
-56
lines changed

2 files changed

+44
-56
lines changed

src/dtype.rs

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use pyo3::{
1010
ffi::{self, PyTuple_Size},
1111
pyobject_native_type_extract, pyobject_native_type_named,
1212
types::{PyAnyMethods, PyDict, PyDictMethods, PyTuple, PyType},
13-
AsPyPointer, Borrowed, Bound, PyAny, PyNativeType, PyObject, PyResult, PyTypeInfo, Python,
14-
ToPyObject,
13+
Borrowed, Bound, PyAny, PyObject, PyResult, PyTypeInfo, Python, ToPyObject,
1514
};
1615
#[cfg(feature = "half")]
1716
use pyo3::{sync::GILOnceCell, Py};
17+
#[cfg(feature = "gil-refs")]
18+
use pyo3::{AsPyPointer, PyNativeType};
1819

1920
use crate::npyffi::{
2021
NpyTypes, PyArray_Descr, PyDataType_ALIGNMENT, PyDataType_ELSIZE, PyDataType_FIELDS,
@@ -60,6 +61,7 @@ unsafe impl PyTypeInfo for PyArrayDescr {
6061
unsafe { PY_ARRAY_API.get_type_object(py, NpyTypes::PyArrayDescr_Type) }
6162
}
6263

64+
#[cfg(feature = "gil-refs")]
6365
fn is_type_of(ob: &PyAny) -> bool {
6466
unsafe { ffi::PyObject_TypeCheck(ob.as_ptr(), Self::type_object_raw(ob.py())) > 0 }
6567
}
@@ -68,10 +70,7 @@ unsafe impl PyTypeInfo for PyArrayDescr {
6870
pyobject_native_type_extract!(PyArrayDescr);
6971

7072
/// Returns the type descriptor ("dtype") for a registered type.
71-
#[deprecated(
72-
since = "0.21.0",
73-
note = "This will be replaced by `dtype_bound` in the future."
74-
)]
73+
#[cfg(feature = "gil-refs")]
7574
pub fn dtype<'py, T: Element>(py: Python<'py>) -> &'py PyArrayDescr {
7675
T::get_dtype_bound(py).into_gil_ref()
7776
}
@@ -82,18 +81,6 @@ pub fn dtype_bound<'py, T: Element>(py: Python<'py>) -> Bound<'py, PyArrayDescr>
8281
}
8382

8483
impl PyArrayDescr {
85-
/// Creates a new type descriptor ("dtype") object from an arbitrary object.
86-
///
87-
/// Equivalent to invoking the constructor of [`numpy.dtype`][dtype].
88-
///
89-
/// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
90-
#[deprecated(
91-
since = "0.21.0",
92-
note = "This will be replace by `new_bound` in the future."
93-
)]
94-
pub fn new<'py, T: ToPyObject + ?Sized>(py: Python<'py>, ob: &T) -> PyResult<&'py Self> {
95-
Self::new_bound(py, ob).map(Bound::into_gil_ref)
96-
}
9784
/// Creates a new type descriptor ("dtype") object from an arbitrary object.
9885
///
9986
/// Equivalent to invoking the constructor of [`numpy.dtype`][dtype].
@@ -117,6 +104,42 @@ impl PyArrayDescr {
117104
inner(py, ob.to_object(py))
118105
}
119106

107+
/// Shortcut for creating a type descriptor of `object` type.
108+
pub fn object_bound(py: Python<'_>) -> Bound<'_, Self> {
109+
Self::from_npy_type(py, NPY_TYPES::NPY_OBJECT)
110+
}
111+
112+
/// Returns the type descriptor for a registered type.
113+
pub fn of_bound<'py, T: Element>(py: Python<'py>) -> Bound<'py, Self> {
114+
T::get_dtype_bound(py)
115+
}
116+
117+
fn from_npy_type<'py>(py: Python<'py>, npy_type: NPY_TYPES) -> Bound<'py, Self> {
118+
unsafe {
119+
let descr = PY_ARRAY_API.PyArray_DescrFromType(py, npy_type as _);
120+
Bound::from_owned_ptr(py, descr.cast()).downcast_into_unchecked()
121+
}
122+
}
123+
124+
pub(crate) fn new_from_npy_type<'py>(py: Python<'py>, npy_type: NPY_TYPES) -> Bound<'py, Self> {
125+
unsafe {
126+
let descr = PY_ARRAY_API.PyArray_DescrNewFromType(py, npy_type as _);
127+
Bound::from_owned_ptr(py, descr.cast()).downcast_into_unchecked()
128+
}
129+
}
130+
}
131+
132+
#[cfg(feature = "gil-refs")]
133+
impl PyArrayDescr {
134+
/// Creates a new type descriptor ("dtype") object from an arbitrary object.
135+
///
136+
/// Equivalent to invoking the constructor of [`numpy.dtype`][dtype].
137+
///
138+
/// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
139+
pub fn new<'py, T: ToPyObject + ?Sized>(py: Python<'py>, ob: &T) -> PyResult<&'py Self> {
140+
Self::new_bound(py, ob).map(Bound::into_gil_ref)
141+
}
142+
120143
/// Returns `self` as `*mut PyArray_Descr`.
121144
pub fn as_dtype_ptr(&self) -> *mut PyArray_Descr {
122145
self.as_borrowed().as_dtype_ptr()
@@ -130,52 +153,20 @@ impl PyArrayDescr {
130153
}
131154

132155
/// Shortcut for creating a type descriptor of `object` type.
133-
#[deprecated(
134-
since = "0.21.0",
135-
note = "This will be replaced by `object_bound` in the future."
136-
)]
137156
pub fn object<'py>(py: Python<'py>) -> &'py Self {
138157
Self::object_bound(py).into_gil_ref()
139158
}
140159

141-
/// Shortcut for creating a type descriptor of `object` type.
142-
pub fn object_bound(py: Python<'_>) -> Bound<'_, Self> {
143-
Self::from_npy_type(py, NPY_TYPES::NPY_OBJECT)
144-
}
145-
146160
/// Returns the type descriptor for a registered type.
147-
#[deprecated(
148-
since = "0.21.0",
149-
note = "This will be replaced by `of_bound` in the future."
150-
)]
151161
pub fn of<'py, T: Element>(py: Python<'py>) -> &'py Self {
152162
Self::of_bound::<T>(py).into_gil_ref()
153163
}
154164

155-
/// Returns the type descriptor for a registered type.
156-
pub fn of_bound<'py, T: Element>(py: Python<'py>) -> Bound<'py, Self> {
157-
T::get_dtype_bound(py)
158-
}
159-
160165
/// Returns true if two type descriptors are equivalent.
161166
pub fn is_equiv_to(&self, other: &Self) -> bool {
162167
self.as_borrowed().is_equiv_to(&other.as_borrowed())
163168
}
164169

165-
fn from_npy_type<'py>(py: Python<'py>, npy_type: NPY_TYPES) -> Bound<'py, Self> {
166-
unsafe {
167-
let descr = PY_ARRAY_API.PyArray_DescrFromType(py, npy_type as _);
168-
Bound::from_owned_ptr(py, descr.cast()).downcast_into_unchecked()
169-
}
170-
}
171-
172-
pub(crate) fn new_from_npy_type<'py>(py: Python<'py>, npy_type: NPY_TYPES) -> Bound<'py, Self> {
173-
unsafe {
174-
let descr = PY_ARRAY_API.PyArray_DescrNewFromType(py, npy_type as _);
175-
Bound::from_owned_ptr(py, descr.cast()).downcast_into_unchecked()
176-
}
177-
}
178-
179170
/// Returns the [array scalar][arrays-scalars] corresponding to this type descriptor.
180171
///
181172
/// Equivalent to [`numpy.dtype.type`][dtype-type].
@@ -701,10 +692,7 @@ pub unsafe trait Element: Clone + Send {
701692
const IS_COPY: bool;
702693

703694
/// Returns the associated type descriptor ("dtype") for the given element type.
704-
#[deprecated(
705-
since = "0.21.0",
706-
note = "This will be replaced by `get_dtype_bound` in the future."
707-
)]
695+
#[cfg(feature = "gil-refs")]
708696
fn get_dtype<'py>(py: Python<'py>) -> &'py PyArrayDescr {
709697
Self::get_dtype_bound(py).into_gil_ref()
710698
}
@@ -824,8 +812,8 @@ unsafe impl Element for PyObject {
824812
mod tests {
825813
use super::*;
826814

827-
use pyo3::{py_run, types::PyTypeMethods};
828815
use pyo3::types::PyString;
816+
use pyo3::{py_run, types::PyTypeMethods};
829817

830818
use crate::npyffi::{is_numpy_2, NPY_NEEDS_PYAPI};
831819

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub use crate::borrow::{
109109
PyReadwriteArray5, PyReadwriteArray6, PyReadwriteArrayDyn,
110110
};
111111
pub use crate::convert::{IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
112-
#[allow(deprecated)]
112+
#[cfg(feature = "gil-refs")]
113113
pub use crate::dtype::dtype;
114114
pub use crate::dtype::{
115115
dtype_bound, Complex32, Complex64, Element, PyArrayDescr, PyArrayDescrMethods,

0 commit comments

Comments
 (0)