@@ -199,6 +199,7 @@ struct npy_api {
199
199
int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
200
200
PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int , int );
201
201
PyObject* (*PyArray_Newshape_)(PyObject*, PyArray_Dims*, int );
202
+ PyObject* (*PyArray_View_)(PyObject*, PyObject*, PyObject*);
202
203
203
204
private:
204
205
enum functions {
@@ -216,6 +217,7 @@ struct npy_api {
216
217
API_PyArray_DescrNewFromType = 96 ,
217
218
API_PyArray_Newshape = 135 ,
218
219
API_PyArray_Squeeze = 136 ,
220
+ API_PyArray_View = 137 ,
219
221
API_PyArray_DescrConverter = 174 ,
220
222
API_PyArray_EquivTypes = 182 ,
221
223
API_PyArray_GetArrayParamsFromObject = 278 ,
@@ -248,6 +250,7 @@ struct npy_api {
248
250
DECL_NPY_API (PyArray_DescrNewFromType);
249
251
DECL_NPY_API (PyArray_Newshape);
250
252
DECL_NPY_API (PyArray_Squeeze);
253
+ DECL_NPY_API (PyArray_View);
251
254
DECL_NPY_API (PyArray_DescrConverter);
252
255
DECL_NPY_API (PyArray_EquivTypes);
253
256
DECL_NPY_API (PyArray_GetArrayParamsFromObject);
@@ -802,6 +805,21 @@ class array : public buffer {
802
805
return new_array;
803
806
}
804
807
808
+ // / Create a view of an array in a different data type.
809
+ // / This function may fundamentally reinterpret the data in the array.
810
+ // / It is the responsibility of the caller to ensure that this is safe.
811
+ // / Only supports the `dtype` argument, the `type` argument is omitted,
812
+ // / to be added as needed.
813
+ array view (const std::string &dtype) {
814
+ auto &api = detail::npy_api::get ();
815
+ auto new_view = reinterpret_steal<array>(api.PyArray_View_ (
816
+ m_ptr, dtype::from_args (pybind11::str (dtype)).release ().ptr (), nullptr ));
817
+ if (!new_view) {
818
+ throw error_already_set ();
819
+ }
820
+ return new_view;
821
+ }
822
+
805
823
// / Ensure that the argument is a NumPy array
806
824
// / In case of an error, nullptr is returned and the Python error is cleared.
807
825
static array ensure (handle h, int ExtraFlags = 0 ) {
0 commit comments