1 /*
2 pybind11/numpy.h: Basic NumPy support, vectorize() wrapper
3
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8 */
9
10 #pragma once
11
12 #include "pybind11.h"
13 #include "complex.h"
14 #include <numeric>
15 #include <algorithm>
16 #include <array>
17 #include <cstdlib>
18 #include <cstring>
19 #include <sstream>
20 #include <string>
21 #include <initializer_list>
22 #include <functional>
23 #include <utility>
24 #include <typeindex>
25
26 #if defined(_MSC_VER)
27 # pragma warning(push)
28 # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
29 #endif
30
31 /* This will be true on all flat address space platforms and allows us to reduce the
32 whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size
33 and dimension types (e.g. shape, strides, indexing), instead of inflicting this
34 upon the library user. */
35 static_assert(sizeof(ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t");
36
37 NAMESPACE_BEGIN(pybind11)
38
39 class array; // Forward declaration
40
41 NAMESPACE_BEGIN(detail)
42 template <typename type, typename SFINAE = void> struct npy_format_descriptor;
43
44 struct PyArrayDescr_Proxy {
45 PyObject_HEAD
46 PyObject *typeobj;
47 char kind;
48 char type;
49 char byteorder;
50 char flags;
51 int type_num;
52 int elsize;
53 int alignment;
54 char *subarray;
55 PyObject *fields;
56 PyObject *names;
57 };
58
59 struct PyArray_Proxy {
60 PyObject_HEAD
61 char *data;
62 int nd;
63 ssize_t *dimensions;
64 ssize_t *strides;
65 PyObject *base;
66 PyObject *descr;
67 int flags;
68 };
69
70 struct PyVoidScalarObject_Proxy {
71 PyObject_VAR_HEAD
72 char *obval;
73 PyArrayDescr_Proxy *descr;
74 int flags;
75 PyObject *base;
76 };
77
78 struct numpy_type_info {
79 PyObject* dtype_ptr;
80 std::string format_str;
81 };
82
83 struct numpy_internals {
84 std::unordered_map<std::type_index, numpy_type_info> registered_dtypes;
85
86 numpy_type_info *get_type_info(const std::type_info& tinfo, bool throw_if_missing = true) {
87 auto it = registered_dtypes.find(std::type_index(tinfo));
88 if (it != registered_dtypes.end())
89 return &(it->second);
90 if (throw_if_missing)
91 pybind11_fail(std::string("NumPy type info missing for ") + tinfo.name());
92 return nullptr;
93 }
94
95 template<typename T> numpy_type_info *get_type_info(bool throw_if_missing = true) {
96 return get_type_info(typeid(typename std::remove_cv<T>::type), throw_if_missing);
97 }
98 };
99
load_numpy_internals(numpy_internals * & ptr)100 inline PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) {
101 ptr = &get_or_create_shared_data<numpy_internals>("_numpy_internals");
102 }
103
get_numpy_internals()104 inline numpy_internals& get_numpy_internals() {
105 static numpy_internals* ptr = nullptr;
106 if (!ptr)
107 load_numpy_internals(ptr);
108 return *ptr;
109 }
110
111 struct npy_api {
112 enum constants {
113 NPY_ARRAY_C_CONTIGUOUS_ = 0x0001,
114 NPY_ARRAY_F_CONTIGUOUS_ = 0x0002,
115 NPY_ARRAY_OWNDATA_ = 0x0004,
116 NPY_ARRAY_FORCECAST_ = 0x0010,
117 NPY_ARRAY_ENSUREARRAY_ = 0x0040,
118 NPY_ARRAY_ALIGNED_ = 0x0100,
119 NPY_ARRAY_WRITEABLE_ = 0x0400,
120 NPY_BOOL_ = 0,
121 NPY_BYTE_, NPY_UBYTE_,
122 NPY_SHORT_, NPY_USHORT_,
123 NPY_INT_, NPY_UINT_,
124 NPY_LONG_, NPY_ULONG_,
125 NPY_LONGLONG_, NPY_ULONGLONG_,
126 NPY_FLOAT_, NPY_DOUBLE_, NPY_LONGDOUBLE_,
127 NPY_CFLOAT_, NPY_CDOUBLE_, NPY_CLONGDOUBLE_,
128 NPY_OBJECT_ = 17,
129 NPY_STRING_, NPY_UNICODE_, NPY_VOID_
130 };
131
132 typedef struct {
133 Py_intptr_t *ptr;
134 int len;
135 } PyArray_Dims;
136
getnpy_api137 static npy_api& get() {
138 static npy_api api = lookup();
139 return api;
140 }
141
PyArray_Check_npy_api142 bool PyArray_Check_(PyObject *obj) const {
143 return (bool) PyObject_TypeCheck(obj, PyArray_Type_);
144 }
PyArrayDescr_Check_npy_api145 bool PyArrayDescr_Check_(PyObject *obj) const {
146 return (bool) PyObject_TypeCheck(obj, PyArrayDescr_Type_);
147 }
148
149 unsigned int (*PyArray_GetNDArrayCFeatureVersion_)();
150 PyObject *(*PyArray_DescrFromType_)(int);
151 PyObject *(*PyArray_NewFromDescr_)
152 (PyTypeObject *, PyObject *, int, Py_intptr_t *,
153 Py_intptr_t *, void *, int, PyObject *);
154 PyObject *(*PyArray_DescrNewFromType_)(int);
155 int (*PyArray_CopyInto_)(PyObject *, PyObject *);
156 PyObject *(*PyArray_NewCopy_)(PyObject *, int);
157 PyTypeObject *PyArray_Type_;
158 PyTypeObject *PyVoidArrType_Type_;
159 PyTypeObject *PyArrayDescr_Type_;
160 PyObject *(*PyArray_DescrFromScalar_)(PyObject *);
161 PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *);
162 int (*PyArray_DescrConverter_) (PyObject *, PyObject **);
163 bool (*PyArray_EquivTypes_) (PyObject *, PyObject *);
164 int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, char, PyObject **, int *,
165 Py_ssize_t *, PyObject **, PyObject *);
166 PyObject *(*PyArray_Squeeze_)(PyObject *);
167 int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
168 PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int);
169 private:
170 enum functions {
171 API_PyArray_GetNDArrayCFeatureVersion = 211,
172 API_PyArray_Type = 2,
173 API_PyArrayDescr_Type = 3,
174 API_PyVoidArrType_Type = 39,
175 API_PyArray_DescrFromType = 45,
176 API_PyArray_DescrFromScalar = 57,
177 API_PyArray_FromAny = 69,
178 API_PyArray_Resize = 80,
179 API_PyArray_CopyInto = 82,
180 API_PyArray_NewCopy = 85,
181 API_PyArray_NewFromDescr = 94,
182 API_PyArray_DescrNewFromType = 9,
183 API_PyArray_DescrConverter = 174,
184 API_PyArray_EquivTypes = 182,
185 API_PyArray_GetArrayParamsFromObject = 278,
186 API_PyArray_Squeeze = 136,
187 API_PyArray_SetBaseObject = 282
188 };
189
lookupnpy_api190 static npy_api lookup() {
191 module m = module::import("numpy.core.multiarray");
192 auto c = m.attr("_ARRAY_API");
193 #if PY_MAJOR_VERSION >= 3
194 void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL);
195 #else
196 void **api_ptr = (void **) PyCObject_AsVoidPtr(c.ptr());
197 #endif
198 npy_api api;
199 #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func];
200 DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion);
201 if (api.PyArray_GetNDArrayCFeatureVersion_() < 0x7)
202 pybind11_fail("pybind11 numpy support requires numpy >= 1.7.0");
203 DECL_NPY_API(PyArray_Type);
204 DECL_NPY_API(PyVoidArrType_Type);
205 DECL_NPY_API(PyArrayDescr_Type);
206 DECL_NPY_API(PyArray_DescrFromType);
207 DECL_NPY_API(PyArray_DescrFromScalar);
208 DECL_NPY_API(PyArray_FromAny);
209 DECL_NPY_API(PyArray_Resize);
210 DECL_NPY_API(PyArray_CopyInto);
211 DECL_NPY_API(PyArray_NewCopy);
212 DECL_NPY_API(PyArray_NewFromDescr);
213 DECL_NPY_API(PyArray_DescrNewFromType);
214 DECL_NPY_API(PyArray_DescrConverter);
215 DECL_NPY_API(PyArray_EquivTypes);
216 DECL_NPY_API(PyArray_GetArrayParamsFromObject);
217 DECL_NPY_API(PyArray_Squeeze);
218 DECL_NPY_API(PyArray_SetBaseObject);
219 #undef DECL_NPY_API
220 return api;
221 }
222 };
223
array_proxy(void * ptr)224 inline PyArray_Proxy* array_proxy(void* ptr) {
225 return reinterpret_cast<PyArray_Proxy*>(ptr);
226 }
227
array_proxy(const void * ptr)228 inline const PyArray_Proxy* array_proxy(const void* ptr) {
229 return reinterpret_cast<const PyArray_Proxy*>(ptr);
230 }
231
array_descriptor_proxy(PyObject * ptr)232 inline PyArrayDescr_Proxy* array_descriptor_proxy(PyObject* ptr) {
233 return reinterpret_cast<PyArrayDescr_Proxy*>(ptr);
234 }
235
array_descriptor_proxy(const PyObject * ptr)236 inline const PyArrayDescr_Proxy* array_descriptor_proxy(const PyObject* ptr) {
237 return reinterpret_cast<const PyArrayDescr_Proxy*>(ptr);
238 }
239
check_flags(const void * ptr,int flag)240 inline bool check_flags(const void* ptr, int flag) {
241 return (flag == (array_proxy(ptr)->flags & flag));
242 }
243
244 template <typename T> struct is_std_array : std::false_type { };
245 template <typename T, size_t N> struct is_std_array<std::array<T, N>> : std::true_type { };
246 template <typename T> struct is_complex : std::false_type { };
247 template <typename T> struct is_complex<std::complex<T>> : std::true_type { };
248
249 template <typename T> struct array_info_scalar {
250 typedef T type;
251 static constexpr bool is_array = false;
252 static constexpr bool is_empty = false;
253 static PYBIND11_DESCR extents() { return _(""); }
254 static void append_extents(list& /* shape */) { }
255 };
256 // Computes underlying type and a comma-separated list of extents for array
257 // types (any mix of std::array and built-in arrays). An array of char is
258 // treated as scalar because it gets special handling.
259 template <typename T> struct array_info : array_info_scalar<T> { };
260 template <typename T, size_t N> struct array_info<std::array<T, N>> {
261 using type = typename array_info<T>::type;
262 static constexpr bool is_array = true;
263 static constexpr bool is_empty = (N == 0) || array_info<T>::is_empty;
264 static constexpr size_t extent = N;
265
266 // appends the extents to shape
267 static void append_extents(list& shape) {
268 shape.append(N);
269 array_info<T>::append_extents(shape);
270 }
271
272 template<typename T2 = T, enable_if_t<!array_info<T2>::is_array, int> = 0>
273 static PYBIND11_DESCR extents() {
274 return _<N>();
275 }
276
277 template<typename T2 = T, enable_if_t<array_info<T2>::is_array, int> = 0>
278 static PYBIND11_DESCR extents() {
279 return concat(_<N>(), array_info<T>::extents());
280 }
281 };
282 // For numpy we have special handling for arrays of characters, so we don't include
283 // the size in the array extents.
284 template <size_t N> struct array_info<char[N]> : array_info_scalar<char[N]> { };
285 template <size_t N> struct array_info<std::array<char, N>> : array_info_scalar<std::array<char, N>> { };
286 template <typename T, size_t N> struct array_info<T[N]> : array_info<std::array<T, N>> { };
287 template <typename T> using remove_all_extents_t = typename array_info<T>::type;
288
289 template <typename T> using is_pod_struct = all_of<
290 std::is_standard_layout<T>, // since we're accessing directly in memory we need a standard layout type
291 #if !defined(__GNUG__) || defined(__clang__) || __GNUC__ >= 5
292 std::is_trivially_copyable<T>,
293 #else
294 // GCC 4 doesn't implement is_trivially_copyable, so approximate it
295 std::is_trivially_destructible<T>,
296 satisfies_any_of<T, std::has_trivial_copy_constructor, std::has_trivial_copy_assign>,
297 #endif
298 satisfies_none_of<T, std::is_reference, std::is_array, is_std_array, std::is_arithmetic, is_complex, std::is_enum>
299 >;
300
301 template <ssize_t Dim = 0, typename Strides> ssize_t byte_offset_unsafe(const Strides &) { return 0; }
302 template <ssize_t Dim = 0, typename Strides, typename... Ix>
303 ssize_t byte_offset_unsafe(const Strides &strides, ssize_t i, Ix... index) {
304 return i * strides[Dim] + byte_offset_unsafe<Dim + 1>(strides, index...);
305 }
306
307 /**
308 * Proxy class providing unsafe, unchecked const access to array data. This is constructed through
309 * the `unchecked<T, N>()` method of `array` or the `unchecked<N>()` method of `array_t<T>`. `Dims`
310 * will be -1 for dimensions determined at runtime.
311 */
312 template <typename T, ssize_t Dims>
313 class unchecked_reference {
314 protected:
315 static constexpr bool Dynamic = Dims < 0;
316 const unsigned char *data_;
317 // Storing the shape & strides in local variables (i.e. these arrays) allows the compiler to
318 // make large performance gains on big, nested loops, but requires compile-time dimensions
319 conditional_t<Dynamic, const ssize_t *, std::array<ssize_t, (size_t) Dims>>
320 shape_, strides_;
321 const ssize_t dims_;
322
323 friend class pybind11::array;
324 // Constructor for compile-time dimensions:
325 template <bool Dyn = Dynamic>
326 unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<!Dyn, ssize_t>)
327 : data_{reinterpret_cast<const unsigned char *>(data)}, dims_{Dims} {
328 for (size_t i = 0; i < (size_t) dims_; i++) {
329 shape_[i] = shape[i];
330 strides_[i] = strides[i];
331 }
332 }
333 // Constructor for runtime dimensions:
334 template <bool Dyn = Dynamic>
335 unchecked_reference(const void *data, const ssize_t *shape, const ssize_t *strides, enable_if_t<Dyn, ssize_t> dims)
336 : data_{reinterpret_cast<const unsigned char *>(data)}, shape_{shape}, strides_{strides}, dims_{dims} {}
337
338 public:
339 /**
340 * Unchecked const reference access to data at the given indices. For a compile-time known
341 * number of dimensions, this requires the correct number of arguments; for run-time
342 * dimensionality, this is not checked (and so is up to the caller to use safely).
343 */
344 template <typename... Ix> const T &operator()(Ix... index) const {
345 static_assert(sizeof...(Ix) == Dims || Dynamic,
346 "Invalid number of indices for unchecked array reference");
347 return *reinterpret_cast<const T *>(data_ + byte_offset_unsafe(strides_, ssize_t(index)...));
348 }
349 /**
350 * Unchecked const reference access to data; this operator only participates if the reference
351 * is to a 1-dimensional array. When present, this is exactly equivalent to `obj(index)`.
352 */
353 template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
354 const T &operator[](ssize_t index) const { return operator()(index); }
355
356 /// Pointer access to the data at the given indices.
357 template <typename... Ix> const T *data(Ix... ix) const { return &operator()(ssize_t(ix)...); }
358
359 /// Returns the item size, i.e. sizeof(T)
360 constexpr static ssize_t itemsize() { return sizeof(T); }
361
362 /// Returns the shape (i.e. size) of dimension `dim`
363 ssize_t shape(ssize_t dim) const { return shape_[(size_t) dim]; }
364
365 /// Returns the number of dimensions of the array
366 ssize_t ndim() const { return dims_; }
367
368 /// Returns the total number of elements in the referenced array, i.e. the product of the shapes
369 template <bool Dyn = Dynamic>
370 enable_if_t<!Dyn, ssize_t> size() const {
371 return std::accumulate(shape_.begin(), shape_.end(), (ssize_t) 1, std::multiplies<ssize_t>());
372 }
373 template <bool Dyn = Dynamic>
374 enable_if_t<Dyn, ssize_t> size() const {
375 return std::accumulate(shape_, shape_ + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
376 }
377
378 /// Returns the total number of bytes used by the referenced data. Note that the actual span in
379 /// memory may be larger if the referenced array has non-contiguous strides (e.g. for a slice).
380 ssize_t nbytes() const {
381 return size() * itemsize();
382 }
383 };
384
385 template <typename T, ssize_t Dims>
386 class unchecked_mutable_reference : public unchecked_reference<T, Dims> {
387 friend class pybind11::array;
388 using ConstBase = unchecked_reference<T, Dims>;
389 using ConstBase::ConstBase;
390 using ConstBase::Dynamic;
391 public:
392 /// Mutable, unchecked access to data at the given indices.
393 template <typename... Ix> T& operator()(Ix... index) {
394 static_assert(sizeof...(Ix) == Dims || Dynamic,
395 "Invalid number of indices for unchecked array reference");
396 return const_cast<T &>(ConstBase::operator()(index...));
397 }
398 /**
399 * Mutable, unchecked access data at the given index; this operator only participates if the
400 * reference is to a 1-dimensional array (or has runtime dimensions). When present, this is
401 * exactly equivalent to `obj(index)`.
402 */
403 template <ssize_t D = Dims, typename = enable_if_t<D == 1 || Dynamic>>
404 T &operator[](ssize_t index) { return operator()(index); }
405
406 /// Mutable pointer access to the data at the given indices.
407 template <typename... Ix> T *mutable_data(Ix... ix) { return &operator()(ssize_t(ix)...); }
408 };
409
410 template <typename T, ssize_t Dim>
411 struct type_caster<unchecked_reference<T, Dim>> {
412 static_assert(Dim == 0 && Dim > 0 /* always fail */, "unchecked array proxy object is not castable");
413 };
414 template <typename T, ssize_t Dim>
415 struct type_caster<unchecked_mutable_reference<T, Dim>> : type_caster<unchecked_reference<T, Dim>> {};
416
417 NAMESPACE_END(detail)
418
419 class dtype : public object {
420 public:
421 PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_);
422
423 explicit dtype(const buffer_info &info) {
424 dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format)));
425 // If info.itemsize == 0, use the value calculated from the format string
426 m_ptr = descr.strip_padding(info.itemsize ? info.itemsize : descr.itemsize()).release().ptr();
427 }
428
429 explicit dtype(const std::string &format) {
430 m_ptr = from_args(pybind11::str(format)).release().ptr();
431 }
432
433 dtype(const char *format) : dtype(std::string(format)) { }
434
435 dtype(list names, list formats, list offsets, ssize_t itemsize) {
436 dict args;
437 args["names"] = names;
438 args["formats"] = formats;
439 args["offsets"] = offsets;
440 args["itemsize"] = pybind11::int_(itemsize);
441 m_ptr = from_args(args).release().ptr();
442 }
443
444 /// This is essentially the same as calling numpy.dtype(args) in Python.
445 static dtype from_args(object args) {
446 PyObject *ptr = nullptr;
447 if (!detail::npy_api::get().PyArray_DescrConverter_(args.release().ptr(), &ptr) || !ptr)
448 throw error_already_set();
449 return reinterpret_steal<dtype>(ptr);
450 }
451
452 /// Return dtype associated with a C++ type.
453 template <typename T> static dtype of() {
454 return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::dtype();
455 }
456
457 /// Size of the data type in bytes.
458 ssize_t itemsize() const {
459 return detail::array_descriptor_proxy(m_ptr)->elsize;
460 }
461
462 /// Returns true for structured data types.
463 bool has_fields() const {
464 return detail::array_descriptor_proxy(m_ptr)->names != nullptr;
465 }
466
467 /// Single-character type code.
468 char kind() const {
469 return detail::array_descriptor_proxy(m_ptr)->kind;
470 }
471
472 private:
473 static object _dtype_from_pep3118() {
474 static PyObject *obj = module::import("numpy.core._internal")
475 .attr("_dtype_from_pep3118").cast<object>().release().ptr();
476 return reinterpret_borrow<object>(obj);
477 }
478
479 dtype strip_padding(ssize_t itemsize) {
480 // Recursively strip all void fields with empty names that are generated for
481 // padding fields (as of NumPy v1.11).
482 if (!has_fields())
483 return *this;
484
485 struct field_descr { PYBIND11_STR_TYPE name; object format; pybind11::int_ offset; };
486 std::vector<field_descr> field_descriptors;
487
488 for (auto field : attr("fields").attr("items")()) {
489 auto spec = field.cast<tuple>();
490 auto name = spec[0].cast<pybind11::str>();
491 auto format = spec[1].cast<tuple>()[0].cast<dtype>();
492 auto offset = spec[1].cast<tuple>()[1].cast<pybind11::int_>();
493 if (!len(name) && format.kind() == 'V')
494 continue;
495 field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset});
496 }
497
498 std::sort(field_descriptors.begin(), field_descriptors.end(),
499 [](const field_descr& a, const field_descr& b) {
500 return a.offset.cast<int>() < b.offset.cast<int>();
501 });
502
503 list names, formats, offsets;
504 for (auto& descr : field_descriptors) {
505 names.append(descr.name);
506 formats.append(descr.format);
507 offsets.append(descr.offset);
508 }
509 return dtype(names, formats, offsets, itemsize);
510 }
511 };
512
513 class array : public buffer {
514 public:
515 PYBIND11_OBJECT_CVT(array, buffer, detail::npy_api::get().PyArray_Check_, raw_array)
516
517 enum {
518 c_style = detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_,
519 f_style = detail::npy_api::NPY_ARRAY_F_CONTIGUOUS_,
520 forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_
521 };
522
523 array() : array({{0}}, static_cast<const double *>(nullptr)) {}
524
525 using ShapeContainer = detail::any_container<ssize_t>;
526 using StridesContainer = detail::any_container<ssize_t>;
527
528 // Constructs an array taking shape/strides from arbitrary container types
529 array(const pybind11::dtype &dt, ShapeContainer shape, StridesContainer strides,
530 const void *ptr = nullptr, handle base = handle()) {
531
532 if (strides->empty())
533 *strides = c_strides(*shape, dt.itemsize());
534
535 auto ndim = shape->size();
536 if (ndim != strides->size())
537 pybind11_fail("NumPy: shape ndim doesn't match strides ndim");
538 auto descr = dt;
539
540 int flags = 0;
541 if (base && ptr) {
542 if (isinstance<array>(base))
543 /* Copy flags from base (except ownership bit) */
544 flags = reinterpret_borrow<array>(base).flags() & ~detail::npy_api::NPY_ARRAY_OWNDATA_;
545 else
546 /* Writable by default, easy to downgrade later on if needed */
547 flags = detail::npy_api::NPY_ARRAY_WRITEABLE_;
548 }
549
550 auto &api = detail::npy_api::get();
551 auto tmp = reinterpret_steal<object>(api.PyArray_NewFromDescr_(
552 api.PyArray_Type_, descr.release().ptr(), (int) ndim, shape->data(), strides->data(),
553 const_cast<void *>(ptr), flags, nullptr));
554 if (!tmp)
555 throw error_already_set();
556 if (ptr) {
557 if (base) {
558 api.PyArray_SetBaseObject_(tmp.ptr(), base.inc_ref().ptr());
559 } else {
560 tmp = reinterpret_steal<object>(api.PyArray_NewCopy_(tmp.ptr(), -1 /* any order */));
561 }
562 }
563 m_ptr = tmp.release().ptr();
564 }
565
566 array(const pybind11::dtype &dt, ShapeContainer shape, const void *ptr = nullptr, handle base = handle())
567 : array(dt, std::move(shape), {}, ptr, base) { }
568
569 template <typename T, typename = detail::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>>
570 array(const pybind11::dtype &dt, T count, const void *ptr = nullptr, handle base = handle())
571 : array(dt, {{count}}, ptr, base) { }
572
573 template <typename T>
574 array(ShapeContainer shape, StridesContainer strides, const T *ptr, handle base = handle())
575 : array(pybind11::dtype::of<T>(), std::move(shape), std::move(strides), ptr, base) { }
576
577 template <typename T>
578 array(ShapeContainer shape, const T *ptr, handle base = handle())
579 : array(std::move(shape), {}, ptr, base) { }
580
581 template <typename T>
582 explicit array(ssize_t count, const T *ptr, handle base = handle()) : array({count}, {}, ptr, base) { }
583
584 explicit array(const buffer_info &info)
585 : array(pybind11::dtype(info), info.shape, info.strides, info.ptr) { }
586
587 /// Array descriptor (dtype)
588 pybind11::dtype dtype() const {
589 return reinterpret_borrow<pybind11::dtype>(detail::array_proxy(m_ptr)->descr);
590 }
591
592 /// Total number of elements
593 ssize_t size() const {
594 return std::accumulate(shape(), shape() + ndim(), (ssize_t) 1, std::multiplies<ssize_t>());
595 }
596
597 /// Byte size of a single element
598 ssize_t itemsize() const {
599 return detail::array_descriptor_proxy(detail::array_proxy(m_ptr)->descr)->elsize;
600 }
601
602 /// Total number of bytes
603 ssize_t nbytes() const {
604 return size() * itemsize();
605 }
606
607 /// Number of dimensions
608 ssize_t ndim() const {
609 return detail::array_proxy(m_ptr)->nd;
610 }
611
612 /// Base object
613 object base() const {
614 return reinterpret_borrow<object>(detail::array_proxy(m_ptr)->base);
615 }
616
617 /// Dimensions of the array
618 const ssize_t* shape() const {
619 return detail::array_proxy(m_ptr)->dimensions;
620 }
621
622 /// Dimension along a given axis
623 ssize_t shape(ssize_t dim) const {
624 if (dim >= ndim())
625 fail_dim_check(dim, "invalid axis");
626 return shape()[dim];
627 }
628
629 /// Strides of the array
630 const ssize_t* strides() const {
631 return detail::array_proxy(m_ptr)->strides;
632 }
633
634 /// Stride along a given axis
635 ssize_t strides(ssize_t dim) const {
636 if (dim >= ndim())
637 fail_dim_check(dim, "invalid axis");
638 return strides()[dim];
639 }
640
641 /// Return the NumPy array flags
642 int flags() const {
643 return detail::array_proxy(m_ptr)->flags;
644 }
645
646 /// If set, the array is writeable (otherwise the buffer is read-only)
647 bool writeable() const {
648 return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_WRITEABLE_);
649 }
650
651 /// If set, the array owns the data (will be freed when the array is deleted)
652 bool owndata() const {
653 return detail::check_flags(m_ptr, detail::npy_api::NPY_ARRAY_OWNDATA_);
654 }
655
656 /// Pointer to the contained data. If index is not provided, points to the
657 /// beginning of the buffer. May throw if the index would lead to out of bounds access.
658 template<typename... Ix> const void* data(Ix... index) const {
659 return static_cast<const void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
660 }
661
662 /// Mutable pointer to the contained data. If index is not provided, points to the
663 /// beginning of the buffer. May throw if the index would lead to out of bounds access.
664 /// May throw if the array is not writeable.
665 template<typename... Ix> void* mutable_data(Ix... index) {
666 check_writeable();
667 return static_cast<void *>(detail::array_proxy(m_ptr)->data + offset_at(index...));
668 }
669
670 /// Byte offset from beginning of the array to a given index (full or partial).
671 /// May throw if the index would lead to out of bounds access.
672 template<typename... Ix> ssize_t offset_at(Ix... index) const {
673 if ((ssize_t) sizeof...(index) > ndim())
674 fail_dim_check(sizeof...(index), "too many indices for an array");
675 return byte_offset(ssize_t(index)...);
676 }
677
678 ssize_t offset_at() const { return 0; }
679
680 /// Item count from beginning of the array to a given index (full or partial).
681 /// May throw if the index would lead to out of bounds access.
682 template<typename... Ix> ssize_t index_at(Ix... index) const {
683 return offset_at(index...) / itemsize();
684 }
685
686 /**
687 * Returns a proxy object that provides access to the array's data without bounds or
688 * dimensionality checking. Will throw if the array is missing the `writeable` flag. Use with
689 * care: the array must not be destroyed or reshaped for the duration of the returned object,
690 * and the caller must take care not to access invalid dimensions or dimension indices.
691 */
692 template <typename T, ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() {
693 if (Dims >= 0 && ndim() != Dims)
694 throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
695 "; expected " + std::to_string(Dims));
696 return detail::unchecked_mutable_reference<T, Dims>(mutable_data(), shape(), strides(), ndim());
697 }
698
699 /**
700 * Returns a proxy object that provides const access to the array's data without bounds or
701 * dimensionality checking. Unlike `mutable_unchecked()`, this does not require that the
702 * underlying array have the `writable` flag. Use with care: the array must not be destroyed or
703 * reshaped for the duration of the returned object, and the caller must take care not to access
704 * invalid dimensions or dimension indices.
705 */
706 template <typename T, ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const {
707 if (Dims >= 0 && ndim() != Dims)
708 throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
709 "; expected " + std::to_string(Dims));
710 return detail::unchecked_reference<T, Dims>(data(), shape(), strides(), ndim());
711 }
712
713 /// Return a new view with all of the dimensions of length 1 removed
714 array squeeze() {
715 auto& api = detail::npy_api::get();
716 return reinterpret_steal<array>(api.PyArray_Squeeze_(m_ptr));
717 }
718
719 /// Resize array to given shape
720 /// If refcheck is true and more that one reference exist to this array
721 /// then resize will succeed only if it makes a reshape, i.e. original size doesn't change
722 void resize(ShapeContainer new_shape, bool refcheck = true) {
723 detail::npy_api::PyArray_Dims d = {
724 new_shape->data(), int(new_shape->size())
725 };
726 // try to resize, set ordering param to -1 cause it's not used anyway
727 object new_array = reinterpret_steal<object>(
728 detail::npy_api::get().PyArray_Resize_(m_ptr, &d, int(refcheck), -1)
729 );
730 if (!new_array) throw error_already_set();
731 if (isinstance<array>(new_array)) { *this = std::move(new_array); }
732 }
733
734 /// Ensure that the argument is a NumPy array
735 /// In case of an error, nullptr is returned and the Python error is cleared.
736 static array ensure(handle h, int ExtraFlags = 0) {
737 auto result = reinterpret_steal<array>(raw_array(h.ptr(), ExtraFlags));
738 if (!result)
739 PyErr_Clear();
740 return result;
741 }
742
743 protected:
744 template<typename, typename> friend struct detail::npy_format_descriptor;
745
746 void fail_dim_check(ssize_t dim, const std::string& msg) const {
747 throw index_error(msg + ": " + std::to_string(dim) +
748 " (ndim = " + std::to_string(ndim()) + ")");
749 }
750
751 template<typename... Ix> ssize_t byte_offset(Ix... index) const {
752 check_dimensions(index...);
753 return detail::byte_offset_unsafe(strides(), ssize_t(index)...);
754 }
755
756 void check_writeable() const {
757 if (!writeable())
758 throw std::domain_error("array is not writeable");
759 }
760
761 // Default, C-style strides
762 static std::vector<ssize_t> c_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) {
763 auto ndim = shape.size();
764 std::vector<ssize_t> strides(ndim, itemsize);
765 for (size_t i = ndim - 1; i > 0; --i)
766 strides[i - 1] = strides[i] * shape[i];
767 return strides;
768 }
769
770 // F-style strides; default when constructing an array_t with `ExtraFlags & f_style`
771 static std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) {
772 auto ndim = shape.size();
773 std::vector<ssize_t> strides(ndim, itemsize);
774 for (size_t i = 1; i < ndim; ++i)
775 strides[i] = strides[i - 1] * shape[i - 1];
776 return strides;
777 }
778
779 template<typename... Ix> void check_dimensions(Ix... index) const {
780 check_dimensions_impl(ssize_t(0), shape(), ssize_t(index)...);
781 }
782
783 void check_dimensions_impl(ssize_t, const ssize_t*) const { }
784
785 template<typename... Ix> void check_dimensions_impl(ssize_t axis, const ssize_t* shape, ssize_t i, Ix... index) const {
786 if (i >= *shape) {
787 throw index_error(std::string("index ") + std::to_string(i) +
788 " is out of bounds for axis " + std::to_string(axis) +
789 " with size " + std::to_string(*shape));
790 }
791 check_dimensions_impl(axis + 1, shape + 1, index...);
792 }
793
794 /// Create array from any object -- always returns a new reference
795 static PyObject *raw_array(PyObject *ptr, int ExtraFlags = 0) {
796 if (ptr == nullptr) {
797 PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array from a nullptr");
798 return nullptr;
799 }
800 return detail::npy_api::get().PyArray_FromAny_(
801 ptr, nullptr, 0, 0, detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr);
802 }
803 };
804
805 template <typename T, int ExtraFlags = array::forcecast> class array_t : public array {
806 private:
807 struct private_ctor {};
808 // Delegating constructor needed when both moving and accessing in the same constructor
809 array_t(private_ctor, ShapeContainer &&shape, StridesContainer &&strides, const T *ptr, handle base)
810 : array(std::move(shape), std::move(strides), ptr, base) {}
811 public:
812 static_assert(!detail::array_info<T>::is_array, "Array types cannot be used with array_t");
813
814 using value_type = T;
815
816 array_t() : array(0, static_cast<const T *>(nullptr)) {}
817 array_t(handle h, borrowed_t) : array(h, borrowed_t{}) { }
818 array_t(handle h, stolen_t) : array(h, stolen_t{}) { }
819
820 PYBIND11_DEPRECATED("Use array_t<T>::ensure() instead")
821 array_t(handle h, bool is_borrowed) : array(raw_array_t(h.ptr()), stolen_t{}) {
822 if (!m_ptr) PyErr_Clear();
823 if (!is_borrowed) Py_XDECREF(h.ptr());
824 }
825
826 array_t(const object &o) : array(raw_array_t(o.ptr()), stolen_t{}) {
827 if (!m_ptr) throw error_already_set();
828 }
829
830 explicit array_t(const buffer_info& info) : array(info) { }
831
832 array_t(ShapeContainer shape, StridesContainer strides, const T *ptr = nullptr, handle base = handle())
833 : array(std::move(shape), std::move(strides), ptr, base) { }
834
835 explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
836 : array_t(private_ctor{}, std::move(shape),
837 ExtraFlags & f_style ? f_strides(*shape, itemsize()) : c_strides(*shape, itemsize()),
838 ptr, base) { }
839
840 explicit array_t(size_t count, const T *ptr = nullptr, handle base = handle())
841 : array({count}, {}, ptr, base) { }
842
843 constexpr ssize_t itemsize() const {
844 return sizeof(T);
845 }
846
847 template<typename... Ix> ssize_t index_at(Ix... index) const {
848 return offset_at(index...) / itemsize();
849 }
850
851 template<typename... Ix> const T* data(Ix... index) const {
852 return static_cast<const T*>(array::data(index...));
853 }
854
855 template<typename... Ix> T* mutable_data(Ix... index) {
856 return static_cast<T*>(array::mutable_data(index...));
857 }
858
859 // Reference to element at a given index
860 template<typename... Ix> const T& at(Ix... index) const {
861 if (sizeof...(index) != ndim())
862 fail_dim_check(sizeof...(index), "index dimension mismatch");
863 return *(static_cast<const T*>(array::data()) + byte_offset(ssize_t(index)...) / itemsize());
864 }
865
866 // Mutable reference to element at a given index
867 template<typename... Ix> T& mutable_at(Ix... index) {
868 if (sizeof...(index) != ndim())
869 fail_dim_check(sizeof...(index), "index dimension mismatch");
870 return *(static_cast<T*>(array::mutable_data()) + byte_offset(ssize_t(index)...) / itemsize());
871 }
872
873 /**
874 * Returns a proxy object that provides access to the array's data without bounds or
875 * dimensionality checking. Will throw if the array is missing the `writeable` flag. Use with
876 * care: the array must not be destroyed or reshaped for the duration of the returned object,
877 * and the caller must take care not to access invalid dimensions or dimension indices.
878 */
879 template <ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() {
880 return array::mutable_unchecked<T, Dims>();
881 }
882
883 /**
884 * Returns a proxy object that provides const access to the array's data without bounds or
885 * dimensionality checking. Unlike `unchecked()`, this does not require that the underlying
886 * array have the `writable` flag. Use with care: the array must not be destroyed or reshaped
887 * for the duration of the returned object, and the caller must take care not to access invalid
888 * dimensions or dimension indices.
889 */
890 template <ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const {
891 return array::unchecked<T, Dims>();
892 }
893
894 /// Ensure that the argument is a NumPy array of the correct dtype (and if not, try to convert
895 /// it). In case of an error, nullptr is returned and the Python error is cleared.
896 static array_t ensure(handle h) {
897 auto result = reinterpret_steal<array_t>(raw_array_t(h.ptr()));
898 if (!result)
899 PyErr_Clear();
900 return result;
901 }
902
903 static bool check_(handle h) {
904 const auto &api = detail::npy_api::get();
905 return api.PyArray_Check_(h.ptr())
906 && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of<T>().ptr());
907 }
908
909 protected:
910 /// Create array from any object -- always returns a new reference
911 static PyObject *raw_array_t(PyObject *ptr) {
912 if (ptr == nullptr) {
913 PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array_t from a nullptr");
914 return nullptr;
915 }
916 return detail::npy_api::get().PyArray_FromAny_(
917 ptr, dtype::of<T>().release().ptr(), 0, 0,
918 detail::npy_api::NPY_ARRAY_ENSUREARRAY_ | ExtraFlags, nullptr);
919 }
920 };
921
922 template <typename T>
923 struct format_descriptor<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
924 static std::string format() {
925 return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::format();
926 }
927 };
928
929 template <size_t N> struct format_descriptor<char[N]> {
930 static std::string format() { return std::to_string(N) + "s"; }
931 };
932 template <size_t N> struct format_descriptor<std::array<char, N>> {
933 static std::string format() { return std::to_string(N) + "s"; }
934 };
935
936 template <typename T>
937 struct format_descriptor<T, detail::enable_if_t<std::is_enum<T>::value>> {
938 static std::string format() {
939 return format_descriptor<
940 typename std::remove_cv<typename std::underlying_type<T>::type>::type>::format();
941 }
942 };
943
944 template <typename T>
945 struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
946 static std::string format() {
947 using detail::_;
948 PYBIND11_DESCR extents = _("(") + detail::array_info<T>::extents() + _(")");
949 return extents.text() + format_descriptor<detail::remove_all_extents_t<T>>::format();
950 }
951 };
952
953 NAMESPACE_BEGIN(detail)
954 template <typename T, int ExtraFlags>
955 struct pyobject_caster<array_t<T, ExtraFlags>> {
956 using type = array_t<T, ExtraFlags>;
957
958 bool load(handle src, bool convert) {
959 if (!convert && !type::check_(src))
960 return false;
961 value = type::ensure(src);
962 return static_cast<bool>(value);
963 }
964
965 static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
966 return src.inc_ref();
967 }
968 PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name());
969 };
970
971 template <typename T>
972 struct compare_buffer_info<T, detail::enable_if_t<detail::is_pod_struct<T>::value>> {
973 static bool compare(const buffer_info& b) {
974 return npy_api::get().PyArray_EquivTypes_(dtype::of<T>().ptr(), dtype(b).ptr());
975 }
976 };
977
978 template <typename T> struct npy_format_descriptor<T, enable_if_t<satisfies_any_of<T, std::is_arithmetic, is_complex>::value>> {
979 private:
980 // NB: the order here must match the one in common.h
981 constexpr static const int values[15] = {
982 npy_api::NPY_BOOL_,
983 npy_api::NPY_BYTE_, npy_api::NPY_UBYTE_, npy_api::NPY_SHORT_, npy_api::NPY_USHORT_,
984 npy_api::NPY_INT_, npy_api::NPY_UINT_, npy_api::NPY_LONGLONG_, npy_api::NPY_ULONGLONG_,
985 npy_api::NPY_FLOAT_, npy_api::NPY_DOUBLE_, npy_api::NPY_LONGDOUBLE_,
986 npy_api::NPY_CFLOAT_, npy_api::NPY_CDOUBLE_, npy_api::NPY_CLONGDOUBLE_
987 };
988
989 public:
990 static constexpr int value = values[detail::is_fmt_numeric<T>::index];
991
992 static pybind11::dtype dtype() {
993 if (auto ptr = npy_api::get().PyArray_DescrFromType_(value))
994 return reinterpret_borrow<pybind11::dtype>(ptr);
995 pybind11_fail("Unsupported buffer format!");
996 }
997 template <typename T2 = T, enable_if_t<std::is_integral<T2>::value, int> = 0>
998 static PYBIND11_DESCR name() {
999 return _<std::is_same<T, bool>::value>(_("bool"),
1000 _<std::is_signed<T>::value>("int", "uint") + _<sizeof(T)*8>());
1001 }
1002 template <typename T2 = T, enable_if_t<std::is_floating_point<T2>::value, int> = 0>
1003 static PYBIND11_DESCR name() {
1004 return _<std::is_same<T, float>::value || std::is_same<T, double>::value>(
1005 _("float") + _<sizeof(T)*8>(), _("longdouble"));
1006 }
1007 template <typename T2 = T, enable_if_t<is_complex<T2>::value, int> = 0>
1008 static PYBIND11_DESCR name() {
1009 return _<std::is_same<typename T2::value_type, float>::value || std::is_same<typename T2::value_type, double>::value>(
1010 _("complex") + _<sizeof(typename T2::value_type)*16>(), _("longcomplex"));
1011 }
1012 };
1013
1014 #define PYBIND11_DECL_CHAR_FMT \
1015 static PYBIND11_DESCR name() { return _("S") + _<N>(); } \
1016 static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); }
1017 template <size_t N> struct npy_format_descriptor<char[N]> { PYBIND11_DECL_CHAR_FMT };
1018 template <size_t N> struct npy_format_descriptor<std::array<char, N>> { PYBIND11_DECL_CHAR_FMT };
1019 #undef PYBIND11_DECL_CHAR_FMT
1020
1021 template<typename T> struct npy_format_descriptor<T, enable_if_t<array_info<T>::is_array>> {
1022 private:
1023 using base_descr = npy_format_descriptor<typename array_info<T>::type>;
1024 public:
1025 static_assert(!array_info<T>::is_empty, "Zero-sized arrays are not supported");
1026
1027 static PYBIND11_DESCR name() { return _("(") + array_info<T>::extents() + _(")") + base_descr::name(); }
1028 static pybind11::dtype dtype() {
1029 list shape;
1030 array_info<T>::append_extents(shape);
1031 return pybind11::dtype::from_args(pybind11::make_tuple(base_descr::dtype(), shape));
1032 }
1033 };
1034
1035 template<typename T> struct npy_format_descriptor<T, enable_if_t<std::is_enum<T>::value>> {
1036 private:
1037 using base_descr = npy_format_descriptor<typename std::underlying_type<T>::type>;
1038 public:
1039 static PYBIND11_DESCR name() { return base_descr::name(); }
1040 static pybind11::dtype dtype() { return base_descr::dtype(); }
1041 };
1042
1043 struct field_descriptor {
1044 const char *name;
1045 ssize_t offset;
1046 ssize_t size;
1047 std::string format;
1048 dtype descr;
1049 };
1050
1051 inline PYBIND11_NOINLINE void register_structured_dtype(
1052 const std::initializer_list<field_descriptor>& fields,
1053 const std::type_info& tinfo, ssize_t itemsize,
1054 bool (*direct_converter)(PyObject *, void *&)) {
1055
1056 auto& numpy_internals = get_numpy_internals();
1057 if (numpy_internals.get_type_info(tinfo, false))
1058 pybind11_fail("NumPy: dtype is already registered");
1059
1060 list names, formats, offsets;
1061 for (auto field : fields) {
1062 if (!field.descr)
1063 pybind11_fail(std::string("NumPy: unsupported field dtype: `") +
1064 field.name + "` @ " + tinfo.name());
1065 names.append(PYBIND11_STR_TYPE(field.name));
1066 formats.append(field.descr);
1067 offsets.append(pybind11::int_(field.offset));
1068 }
1069 auto dtype_ptr = pybind11::dtype(names, formats, offsets, itemsize).release().ptr();
1070
1071 // There is an existing bug in NumPy (as of v1.11): trailing bytes are
1072 // not encoded explicitly into the format string. This will supposedly
1073 // get fixed in v1.12; for further details, see these:
1074 // - https://github.com/numpy/numpy/issues/7797
1075 // - https://github.com/numpy/numpy/pull/7798
1076 // Because of this, we won't use numpy's logic to generate buffer format
1077 // strings and will just do it ourselves.
1078 std::vector<field_descriptor> ordered_fields(fields);
1079 std::sort(ordered_fields.begin(), ordered_fields.end(),
1080 [](const field_descriptor &a, const field_descriptor &b) { return a.offset < b.offset; });
1081 ssize_t offset = 0;
1082 std::ostringstream oss;
1083 // mark the structure as unaligned with '^', because numpy and C++ don't
1084 // always agree about alignment (particularly for complex), and we're
1085 // explicitly listing all our padding. This depends on none of the fields
1086 // overriding the endianness. Putting the ^ in front of individual fields
1087 // isn't guaranteed to work due to https://github.com/numpy/numpy/issues/9049
1088 oss << "^T{";
1089 for (auto& field : ordered_fields) {
1090 if (field.offset > offset)
1091 oss << (field.offset - offset) << 'x';
1092 oss << field.format << ':' << field.name << ':';
1093 offset = field.offset + field.size;
1094 }
1095 if (itemsize > offset)
1096 oss << (itemsize - offset) << 'x';
1097 oss << '}';
1098 auto format_str = oss.str();
1099
1100 // Sanity check: verify that NumPy properly parses our buffer format string
1101 auto& api = npy_api::get();
1102 auto arr = array(buffer_info(nullptr, itemsize, format_str, 1));
1103 if (!api.PyArray_EquivTypes_(dtype_ptr, arr.dtype().ptr()))
1104 pybind11_fail("NumPy: invalid buffer descriptor!");
1105
1106 auto tindex = std::type_index(tinfo);
1107 numpy_internals.registered_dtypes[tindex] = { dtype_ptr, format_str };
1108 get_internals().direct_conversions[tindex].push_back(direct_converter);
1109 }
1110
1111 template <typename T, typename SFINAE> struct npy_format_descriptor {
1112 static_assert(is_pod_struct<T>::value, "Attempt to use a non-POD or unimplemented POD type as a numpy dtype");
1113
1114 static PYBIND11_DESCR name() { return make_caster<T>::name(); }
1115
1116 static pybind11::dtype dtype() {
1117 return reinterpret_borrow<pybind11::dtype>(dtype_ptr());
1118 }
1119
1120 static std::string format() {
1121 static auto format_str = get_numpy_internals().get_type_info<T>(true)->format_str;
1122 return format_str;
1123 }
1124
1125 static void register_dtype(const std::initializer_list<field_descriptor>& fields) {
1126 register_structured_dtype(fields, typeid(typename std::remove_cv<T>::type),
1127 sizeof(T), &direct_converter);
1128 }
1129
1130 private:
1131 static PyObject* dtype_ptr() {
1132 static PyObject* ptr = get_numpy_internals().get_type_info<T>(true)->dtype_ptr;
1133 return ptr;
1134 }
1135
1136 static bool direct_converter(PyObject *obj, void*& value) {
1137 auto& api = npy_api::get();
1138 if (!PyObject_TypeCheck(obj, api.PyVoidArrType_Type_))
1139 return false;
1140 if (auto descr = reinterpret_steal<object>(api.PyArray_DescrFromScalar_(obj))) {
1141 if (api.PyArray_EquivTypes_(dtype_ptr(), descr.ptr())) {
1142 value = ((PyVoidScalarObject_Proxy *) obj)->obval;
1143 return true;
1144 }
1145 }
1146 return false;
1147 }
1148 };
1149
1150 #ifdef __CLION_IDE__ // replace heavy macro with dummy code for the IDE (doesn't affect code)
1151 # define PYBIND11_NUMPY_DTYPE(Type, ...) ((void)0)
1152 # define PYBIND11_NUMPY_DTYPE_EX(Type, ...) ((void)0)
1153 #else
1154
1155 #define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \
1156 ::pybind11::detail::field_descriptor { \
1157 Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \
1158 ::pybind11::format_descriptor<decltype(std::declval<T>().Field)>::format(), \
1159 ::pybind11::detail::npy_format_descriptor<decltype(std::declval<T>().Field)>::dtype() \
1160 }
1161
1162 // Extract name, offset and format descriptor for a struct field
1163 #define PYBIND11_FIELD_DESCRIPTOR(T, Field) PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, #Field)
1164
1165 // The main idea of this macro is borrowed from https://github.com/swansontec/map-macro
1166 // (C) William Swanson, Paul Fultz
1167 #define PYBIND11_EVAL0(...) __VA_ARGS__
1168 #define PYBIND11_EVAL1(...) PYBIND11_EVAL0 (PYBIND11_EVAL0 (PYBIND11_EVAL0 (__VA_ARGS__)))
1169 #define PYBIND11_EVAL2(...) PYBIND11_EVAL1 (PYBIND11_EVAL1 (PYBIND11_EVAL1 (__VA_ARGS__)))
1170 #define PYBIND11_EVAL3(...) PYBIND11_EVAL2 (PYBIND11_EVAL2 (PYBIND11_EVAL2 (__VA_ARGS__)))
1171 #define PYBIND11_EVAL4(...) PYBIND11_EVAL3 (PYBIND11_EVAL3 (PYBIND11_EVAL3 (__VA_ARGS__)))
1172 #define PYBIND11_EVAL(...) PYBIND11_EVAL4 (PYBIND11_EVAL4 (PYBIND11_EVAL4 (__VA_ARGS__)))
1173 #define PYBIND11_MAP_END(...)
1174 #define PYBIND11_MAP_OUT
1175 #define PYBIND11_MAP_COMMA ,
1176 #define PYBIND11_MAP_GET_END() 0, PYBIND11_MAP_END
1177 #define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT
1178 #define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0 (test, next, 0)
1179 #define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1 (PYBIND11_MAP_GET_END test, next)
1180 #ifdef _MSC_VER // MSVC is not as eager to expand macros, hence this workaround
1181 #define PYBIND11_MAP_LIST_NEXT1(test, next) \
1182 PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0))
1183 #else
1184 #define PYBIND11_MAP_LIST_NEXT1(test, next) \
1185 PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)
1186 #endif
1187 #define PYBIND11_MAP_LIST_NEXT(test, next) \
1188 PYBIND11_MAP_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next)
1189 #define PYBIND11_MAP_LIST0(f, t, x, peek, ...) \
1190 f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST1) (f, t, peek, __VA_ARGS__)
1191 #define PYBIND11_MAP_LIST1(f, t, x, peek, ...) \
1192 f(t, x) PYBIND11_MAP_LIST_NEXT (peek, PYBIND11_MAP_LIST0) (f, t, peek, __VA_ARGS__)
1193 // PYBIND11_MAP_LIST(f, t, a1, a2, ...) expands to f(t, a1), f(t, a2), ...
1194 #define PYBIND11_MAP_LIST(f, t, ...) \
1195 PYBIND11_EVAL (PYBIND11_MAP_LIST1 (f, t, __VA_ARGS__, (), 0))
1196
1197 #define PYBIND11_NUMPY_DTYPE(Type, ...) \
1198 ::pybind11::detail::npy_format_descriptor<Type>::register_dtype \
1199 ({PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)})
1200
1201 #ifdef _MSC_VER
1202 #define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1203 PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0))
1204 #else
1205 #define PYBIND11_MAP2_LIST_NEXT1(test, next) \
1206 PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)
1207 #endif
1208 #define PYBIND11_MAP2_LIST_NEXT(test, next) \
1209 PYBIND11_MAP2_LIST_NEXT1 (PYBIND11_MAP_GET_END test, next)
1210 #define PYBIND11_MAP2_LIST0(f, t, x1, x2, peek, ...) \
1211 f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST1) (f, t, peek, __VA_ARGS__)
1212 #define PYBIND11_MAP2_LIST1(f, t, x1, x2, peek, ...) \
1213 f(t, x1, x2) PYBIND11_MAP2_LIST_NEXT (peek, PYBIND11_MAP2_LIST0) (f, t, peek, __VA_ARGS__)
1214 // PYBIND11_MAP2_LIST(f, t, a1, a2, ...) expands to f(t, a1, a2), f(t, a3, a4), ...
1215 #define PYBIND11_MAP2_LIST(f, t, ...) \
1216 PYBIND11_EVAL (PYBIND11_MAP2_LIST1 (f, t, __VA_ARGS__, (), 0))
1217
1218 #define PYBIND11_NUMPY_DTYPE_EX(Type, ...) \
1219 ::pybind11::detail::npy_format_descriptor<Type>::register_dtype \
1220 ({PYBIND11_MAP2_LIST (PYBIND11_FIELD_DESCRIPTOR_EX, Type, __VA_ARGS__)})
1221
1222 #endif // __CLION_IDE__
1223
1224 template <class T>
1225 using array_iterator = typename std::add_pointer<T>::type;
1226
1227 template <class T>
1228 array_iterator<T> array_begin(const buffer_info& buffer) {
1229 return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr));
1230 }
1231
1232 template <class T>
1233 array_iterator<T> array_end(const buffer_info& buffer) {
1234 return array_iterator<T>(reinterpret_cast<T*>(buffer.ptr) + buffer.size);
1235 }
1236
1237 class common_iterator {
1238 public:
1239 using container_type = std::vector<ssize_t>;
1240 using value_type = container_type::value_type;
1241 using size_type = container_type::size_type;
1242
1243 common_iterator() : p_ptr(0), m_strides() {}
1244
1245 common_iterator(void* ptr, const container_type& strides, const container_type& shape)
1246 : p_ptr(reinterpret_cast<char*>(ptr)), m_strides(strides.size()) {
1247 m_strides.back() = static_cast<value_type>(strides.back());
1248 for (size_type i = m_strides.size() - 1; i != 0; --i) {
1249 size_type j = i - 1;
1250 value_type s = static_cast<value_type>(shape[i]);
1251 m_strides[j] = strides[j] + m_strides[i] - strides[i] * s;
1252 }
1253 }
1254
1255 void increment(size_type dim) {
1256 p_ptr += m_strides[dim];
1257 }
1258
1259 void* data() const {
1260 return p_ptr;
1261 }
1262
1263 private:
1264 char* p_ptr;
1265 container_type m_strides;
1266 };
1267
1268 template <size_t N> class multi_array_iterator {
1269 public:
1270 using container_type = std::vector<ssize_t>;
1271
1272 multi_array_iterator(const std::array<buffer_info, N> &buffers,
1273 const container_type &shape)
1274 : m_shape(shape.size()), m_index(shape.size(), 0),
1275 m_common_iterator() {
1276
1277 // Manual copy to avoid conversion warning if using std::copy
1278 for (size_t i = 0; i < shape.size(); ++i)
1279 m_shape[i] = shape[i];
1280
1281 container_type strides(shape.size());
1282 for (size_t i = 0; i < N; ++i)
1283 init_common_iterator(buffers[i], shape, m_common_iterator[i], strides);
1284 }
1285
1286 multi_array_iterator& operator++() {
1287 for (size_t j = m_index.size(); j != 0; --j) {
1288 size_t i = j - 1;
1289 if (++m_index[i] != m_shape[i]) {
1290 increment_common_iterator(i);
1291 break;
1292 } else {
1293 m_index[i] = 0;
1294 }
1295 }
1296 return *this;
1297 }
1298
1299 template <size_t K, class T = void> T* data() const {
1300 return reinterpret_cast<T*>(m_common_iterator[K].data());
1301 }
1302
1303 private:
1304
1305 using common_iter = common_iterator;
1306
1307 void init_common_iterator(const buffer_info &buffer,
1308 const container_type &shape,
1309 common_iter &iterator,
1310 container_type &strides) {
1311 auto buffer_shape_iter = buffer.shape.rbegin();
1312 auto buffer_strides_iter = buffer.strides.rbegin();
1313 auto shape_iter = shape.rbegin();
1314 auto strides_iter = strides.rbegin();
1315
1316 while (buffer_shape_iter != buffer.shape.rend()) {
1317 if (*shape_iter == *buffer_shape_iter)
1318 *strides_iter = *buffer_strides_iter;
1319 else
1320 *strides_iter = 0;
1321
1322 ++buffer_shape_iter;
1323 ++buffer_strides_iter;
1324 ++shape_iter;
1325 ++strides_iter;
1326 }
1327
1328 std::fill(strides_iter, strides.rend(), 0);
1329 iterator = common_iter(buffer.ptr, strides, shape);
1330 }
1331
1332 void increment_common_iterator(size_t dim) {
1333 for (auto &iter : m_common_iterator)
1334 iter.increment(dim);
1335 }
1336
1337 container_type m_shape;
1338 container_type m_index;
1339 std::array<common_iter, N> m_common_iterator;
1340 };
1341
1342 enum class broadcast_trivial { non_trivial, c_trivial, f_trivial };
1343
1344 // Populates the shape and number of dimensions for the set of buffers. Returns a broadcast_trivial
1345 // enum value indicating whether the broadcast is "trivial"--that is, has each buffer being either a
1346 // singleton or a full-size, C-contiguous (`c_trivial`) or Fortran-contiguous (`f_trivial`) storage
1347 // buffer; returns `non_trivial` otherwise.
1348 template <size_t N>
1349 broadcast_trivial broadcast(const std::array<buffer_info, N> &buffers, ssize_t &ndim, std::vector<ssize_t> &shape) {
1350 ndim = std::accumulate(buffers.begin(), buffers.end(), ssize_t(0), [](ssize_t res, const buffer_info &buf) {
1351 return std::max(res, buf.ndim);
1352 });
1353
1354 shape.clear();
1355 shape.resize((size_t) ndim, 1);
1356
1357 // Figure out the output size, and make sure all input arrays conform (i.e. are either size 1 or
1358 // the full size).
1359 for (size_t i = 0; i < N; ++i) {
1360 auto res_iter = shape.rbegin();
1361 auto end = buffers[i].shape.rend();
1362 for (auto shape_iter = buffers[i].shape.rbegin(); shape_iter != end; ++shape_iter, ++res_iter) {
1363 const auto &dim_size_in = *shape_iter;
1364 auto &dim_size_out = *res_iter;
1365
1366 // Each input dimension can either be 1 or `n`, but `n` values must match across buffers
1367 if (dim_size_out == 1)
1368 dim_size_out = dim_size_in;
1369 else if (dim_size_in != 1 && dim_size_in != dim_size_out)
1370 pybind11_fail("pybind11::vectorize: incompatible size/dimension of inputs!");
1371 }
1372 }
1373
1374 bool trivial_broadcast_c = true;
1375 bool trivial_broadcast_f = true;
1376 for (size_t i = 0; i < N && (trivial_broadcast_c || trivial_broadcast_f); ++i) {
1377 if (buffers[i].size == 1)
1378 continue;
1379
1380 // Require the same number of dimensions:
1381 if (buffers[i].ndim != ndim)
1382 return broadcast_trivial::non_trivial;
1383
1384 // Require all dimensions be full-size:
1385 if (!std::equal(buffers[i].shape.cbegin(), buffers[i].shape.cend(), shape.cbegin()))
1386 return broadcast_trivial::non_trivial;
1387
1388 // Check for C contiguity (but only if previous inputs were also C contiguous)
1389 if (trivial_broadcast_c) {
1390 ssize_t expect_stride = buffers[i].itemsize;
1391 auto end = buffers[i].shape.crend();
1392 for (auto shape_iter = buffers[i].shape.crbegin(), stride_iter = buffers[i].strides.crbegin();
1393 trivial_broadcast_c && shape_iter != end; ++shape_iter, ++stride_iter) {
1394 if (expect_stride == *stride_iter)
1395 expect_stride *= *shape_iter;
1396 else
1397 trivial_broadcast_c = false;
1398 }
1399 }
1400
1401 // Check for Fortran contiguity (if previous inputs were also F contiguous)
1402 if (trivial_broadcast_f) {
1403 ssize_t expect_stride = buffers[i].itemsize;
1404 auto end = buffers[i].shape.cend();
1405 for (auto shape_iter = buffers[i].shape.cbegin(), stride_iter = buffers[i].strides.cbegin();
1406 trivial_broadcast_f && shape_iter != end; ++shape_iter, ++stride_iter) {
1407 if (expect_stride == *stride_iter)
1408 expect_stride *= *shape_iter;
1409 else
1410 trivial_broadcast_f = false;
1411 }
1412 }
1413 }
1414
1415 return
1416 trivial_broadcast_c ? broadcast_trivial::c_trivial :
1417 trivial_broadcast_f ? broadcast_trivial::f_trivial :
1418 broadcast_trivial::non_trivial;
1419 }
1420
1421 template <typename T>
1422 struct vectorize_arg {
1423 static_assert(!std::is_rvalue_reference<T>::value, "Functions with rvalue reference arguments cannot be vectorized");
1424 // The wrapped function gets called with this type:
1425 using call_type = remove_reference_t<T>;
1426 // Is this a vectorized argument?
1427 static constexpr bool vectorize =
1428 satisfies_any_of<call_type, std::is_arithmetic, is_complex, std::is_pod>::value &&
1429 satisfies_none_of<call_type, std::is_pointer, std::is_array, is_std_array, std::is_enum>::value &&
1430 (!std::is_reference<T>::value ||
1431 (std::is_lvalue_reference<T>::value && std::is_const<call_type>::value));
1432 // Accept this type: an array for vectorized types, otherwise the type as-is:
1433 using type = conditional_t<vectorize, array_t<remove_cv_t<call_type>, array::forcecast>, T>;
1434 };
1435
1436 template <typename Func, typename Return, typename... Args>
1437 struct vectorize_helper {
1438 private:
1439 static constexpr size_t N = sizeof...(Args);
1440 static constexpr size_t NVectorized = constexpr_sum(vectorize_arg<Args>::vectorize...);
1441 static_assert(NVectorized >= 1,
1442 "pybind11::vectorize(...) requires a function with at least one vectorizable argument");
1443
1444 public:
1445 template <typename T>
1446 explicit vectorize_helper(T &&f) : f(std::forward<T>(f)) { }
1447
1448 object operator()(typename vectorize_arg<Args>::type... args) {
1449 return run(args...,
1450 make_index_sequence<N>(),
1451 select_indices<vectorize_arg<Args>::vectorize...>(),
1452 make_index_sequence<NVectorized>());
1453 }
1454
1455 private:
1456 remove_reference_t<Func> f;
1457
1458 template <size_t Index> using param_n_t = typename pack_element<Index, typename vectorize_arg<Args>::call_type...>::type;
1459
1460 // Runs a vectorized function given arguments tuple and three index sequences:
1461 // - Index is the full set of 0 ... (N-1) argument indices;
1462 // - VIndex is the subset of argument indices with vectorized parameters, letting us access
1463 // vectorized arguments (anything not in this sequence is passed through)
1464 // - BIndex is a incremental sequence (beginning at 0) of the same size as VIndex, so that
1465 // we can store vectorized buffer_infos in an array (argument VIndex has its buffer at
1466 // index BIndex in the array).
1467 template <size_t... Index, size_t... VIndex, size_t... BIndex> object run(
1468 typename vectorize_arg<Args>::type &...args,
1469 index_sequence<Index...> i_seq, index_sequence<VIndex...> vi_seq, index_sequence<BIndex...> bi_seq) {
1470
1471 // Pointers to values the function was called with; the vectorized ones set here will start
1472 // out as array_t<T> pointers, but they will be changed them to T pointers before we make
1473 // call the wrapped function. Non-vectorized pointers are left as-is.
1474 std::array<void *, N> params{{ &args... }};
1475
1476 // The array of `buffer_info`s of vectorized arguments:
1477 std::array<buffer_info, NVectorized> buffers{{ reinterpret_cast<array *>(params[VIndex])->request()... }};
1478
1479 /* Determine dimensions parameters of output array */
1480 ssize_t nd = 0;
1481 std::vector<ssize_t> shape(0);
1482 auto trivial = broadcast(buffers, nd, shape);
1483 size_t ndim = (size_t) nd;
1484
1485 size_t size = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies<size_t>());
1486
1487 // If all arguments are 0-dimension arrays (i.e. single values) return a plain value (i.e.
1488 // not wrapped in an array).
1489 if (size == 1 && ndim == 0) {
1490 PYBIND11_EXPAND_SIDE_EFFECTS(params[VIndex] = buffers[BIndex].ptr);
1491 return cast(f(*reinterpret_cast<param_n_t<Index> *>(params[Index])...));
1492 }
1493
1494 array_t<Return> result;
1495 if (trivial == broadcast_trivial::f_trivial) result = array_t<Return, array::f_style>(shape);
1496 else result = array_t<Return>(shape);
1497
1498 if (size == 0) return result;
1499
1500 /* Call the function */
1501 if (trivial == broadcast_trivial::non_trivial)
1502 apply_broadcast(buffers, params, result, i_seq, vi_seq, bi_seq);
1503 else
1504 apply_trivial(buffers, params, result.mutable_data(), size, i_seq, vi_seq, bi_seq);
1505
1506 return result;
1507 }
1508
1509 template <size_t... Index, size_t... VIndex, size_t... BIndex>
1510 void apply_trivial(std::array<buffer_info, NVectorized> &buffers,
1511 std::array<void *, N> ¶ms,
1512 Return *out,
1513 size_t size,
1514 index_sequence<Index...>, index_sequence<VIndex...>, index_sequence<BIndex...>) {
1515
1516 // Initialize an array of mutable byte references and sizes with references set to the
1517 // appropriate pointer in `params`; as we iterate, we'll increment each pointer by its size
1518 // (except for singletons, which get an increment of 0).
1519 std::array<std::pair<unsigned char *&, const size_t>, NVectorized> vecparams{{
1520 std::pair<unsigned char *&, const size_t>(
1521 reinterpret_cast<unsigned char *&>(params[VIndex] = buffers[BIndex].ptr),
1522 buffers[BIndex].size == 1 ? 0 : sizeof(param_n_t<VIndex>)
1523 )...
1524 }};
1525
1526 for (size_t i = 0; i < size; ++i) {
1527 out[i] = f(*reinterpret_cast<param_n_t<Index> *>(params[Index])...);
1528 for (auto &x : vecparams) x.first += x.second;
1529 }
1530 }
1531
1532 template <size_t... Index, size_t... VIndex, size_t... BIndex>
1533 void apply_broadcast(std::array<buffer_info, NVectorized> &buffers,
1534 std::array<void *, N> ¶ms,
1535 array_t<Return> &output_array,
1536 index_sequence<Index...>, index_sequence<VIndex...>, index_sequence<BIndex...>) {
1537
1538 buffer_info output = output_array.request();
1539 multi_array_iterator<NVectorized> input_iter(buffers, output.shape);
1540
1541 for (array_iterator<Return> iter = array_begin<Return>(output), end = array_end<Return>(output);
1542 iter != end;
1543 ++iter, ++input_iter) {
1544 PYBIND11_EXPAND_SIDE_EFFECTS((
1545 params[VIndex] = input_iter.template data<BIndex>()
1546 ));
1547 *iter = f(*reinterpret_cast<param_n_t<Index> *>(std::get<Index>(params))...);
1548 }
1549 }
1550 };
1551
1552 template <typename Func, typename Return, typename... Args>
1553 vectorize_helper<Func, Return, Args...>
1554 vectorize_extractor(const Func &f, Return (*) (Args ...)) {
1555 return detail::vectorize_helper<Func, Return, Args...>(f);
1556 }
1557
1558 template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> {
1559 static PYBIND11_DESCR name() {
1560 return _("numpy.ndarray[") + npy_format_descriptor<T>::name() + _("]");
1561 }
1562 };
1563
1564 NAMESPACE_END(detail)
1565
1566 // Vanilla pointer vectorizer:
1567 template <typename Return, typename... Args>
1568 detail::vectorize_helper<Return (*)(Args...), Return, Args...>
1569 vectorize(Return (*f) (Args ...)) {
1570 return detail::vectorize_helper<Return (*)(Args...), Return, Args...>(f);
1571 }
1572
1573 // lambda vectorizer:
1574 template <typename Func, typename FuncType = typename detail::remove_class<decltype(&detail::remove_reference_t<Func>::operator())>::type>
1575 auto vectorize(Func &&f) -> decltype(
1576 detail::vectorize_extractor(std::forward<Func>(f), (FuncType *) nullptr)) {
1577 return detail::vectorize_extractor(std::forward<Func>(f), (FuncType *) nullptr);
1578 }
1579
1580 // Vectorize a class method (non-const):
1581 template <typename Return, typename Class, typename... Args,
1582 typename Helper = detail::vectorize_helper<decltype(std::mem_fn(std::declval<Return (Class::*)(Args...)>())), Return, Class *, Args...>>
1583 Helper vectorize(Return (Class::*f)(Args...)) {
1584 return Helper(std::mem_fn(f));
1585 }
1586
1587 // Vectorize a class method (non-const):
1588 template <typename Return, typename Class, typename... Args,
1589 typename Helper = detail::vectorize_helper<decltype(std::mem_fn(std::declval<Return (Class::*)(Args...) const>())), Return, const Class *, Args...>>
1590 Helper vectorize(Return (Class::*f)(Args...) const) {
1591 return Helper(std::mem_fn(f));
1592 }
1593
1594 NAMESPACE_END(pybind11)
1595
1596 #if defined(_MSC_VER)
1597 #pragma warning(pop)
1598 #endif
1599