@@ -10,11 +10,12 @@ use pyo3::{
10
10
ffi:: { self , PyTuple_Size } ,
11
11
pyobject_native_type_extract, pyobject_native_type_named,
12
12
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 ,
15
14
} ;
16
15
#[ cfg( feature = "half" ) ]
17
16
use pyo3:: { sync:: GILOnceCell , Py } ;
17
+ #[ cfg( feature = "gil-refs" ) ]
18
+ use pyo3:: { AsPyPointer , PyNativeType } ;
18
19
19
20
use crate :: npyffi:: {
20
21
NpyTypes , PyArray_Descr , PyDataType_ALIGNMENT , PyDataType_ELSIZE , PyDataType_FIELDS ,
@@ -60,6 +61,7 @@ unsafe impl PyTypeInfo for PyArrayDescr {
60
61
unsafe { PY_ARRAY_API . get_type_object ( py, NpyTypes :: PyArrayDescr_Type ) }
61
62
}
62
63
64
+ #[ cfg( feature = "gil-refs" ) ]
63
65
fn is_type_of ( ob : & PyAny ) -> bool {
64
66
unsafe { ffi:: PyObject_TypeCheck ( ob. as_ptr ( ) , Self :: type_object_raw ( ob. py ( ) ) ) > 0 }
65
67
}
@@ -68,10 +70,7 @@ unsafe impl PyTypeInfo for PyArrayDescr {
68
70
pyobject_native_type_extract ! ( PyArrayDescr ) ;
69
71
70
72
/// 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" ) ]
75
74
pub fn dtype < ' py , T : Element > ( py : Python < ' py > ) -> & ' py PyArrayDescr {
76
75
T :: get_dtype_bound ( py) . into_gil_ref ( )
77
76
}
@@ -82,18 +81,6 @@ pub fn dtype_bound<'py, T: Element>(py: Python<'py>) -> Bound<'py, PyArrayDescr>
82
81
}
83
82
84
83
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
- }
97
84
/// Creates a new type descriptor ("dtype") object from an arbitrary object.
98
85
///
99
86
/// Equivalent to invoking the constructor of [`numpy.dtype`][dtype].
@@ -117,6 +104,42 @@ impl PyArrayDescr {
117
104
inner ( py, ob. to_object ( py) )
118
105
}
119
106
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
+
120
143
/// Returns `self` as `*mut PyArray_Descr`.
121
144
pub fn as_dtype_ptr ( & self ) -> * mut PyArray_Descr {
122
145
self . as_borrowed ( ) . as_dtype_ptr ( )
@@ -130,52 +153,20 @@ impl PyArrayDescr {
130
153
}
131
154
132
155
/// 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
- ) ]
137
156
pub fn object < ' py > ( py : Python < ' py > ) -> & ' py Self {
138
157
Self :: object_bound ( py) . into_gil_ref ( )
139
158
}
140
159
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
-
146
160
/// 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
- ) ]
151
161
pub fn of < ' py , T : Element > ( py : Python < ' py > ) -> & ' py Self {
152
162
Self :: of_bound :: < T > ( py) . into_gil_ref ( )
153
163
}
154
164
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
-
160
165
/// Returns true if two type descriptors are equivalent.
161
166
pub fn is_equiv_to ( & self , other : & Self ) -> bool {
162
167
self . as_borrowed ( ) . is_equiv_to ( & other. as_borrowed ( ) )
163
168
}
164
169
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
-
179
170
/// Returns the [array scalar][arrays-scalars] corresponding to this type descriptor.
180
171
///
181
172
/// Equivalent to [`numpy.dtype.type`][dtype-type].
@@ -701,10 +692,7 @@ pub unsafe trait Element: Clone + Send {
701
692
const IS_COPY : bool ;
702
693
703
694
/// 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" ) ]
708
696
fn get_dtype < ' py > ( py : Python < ' py > ) -> & ' py PyArrayDescr {
709
697
Self :: get_dtype_bound ( py) . into_gil_ref ( )
710
698
}
@@ -824,8 +812,8 @@ unsafe impl Element for PyObject {
824
812
mod tests {
825
813
use super :: * ;
826
814
827
- use pyo3:: { py_run, types:: PyTypeMethods } ;
828
815
use pyo3:: types:: PyString ;
816
+ use pyo3:: { py_run, types:: PyTypeMethods } ;
829
817
830
818
use crate :: npyffi:: { is_numpy_2, NPY_NEEDS_PYAPI } ;
831
819
0 commit comments