blob: 5f987a84717202e5fee37fc385b51b88dce4be4f [file] [log] [blame]
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_numpy_dtypes.cpp -- Structured and compound NumPy dtypes
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01003
4 Copyright (c) 2016 Ivan Smirnov
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010011#include <pybind11/numpy.h>
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010012
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010013#ifdef __GNUC__
14#define PYBIND11_PACKED(cls) cls __attribute__((__packed__))
15#else
16#define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop))
17#endif
18
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010019namespace py = pybind11;
20
Ivan Smirnov4f164212016-06-22 01:07:20 +010021struct SimpleStruct {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050022 bool bool_;
23 uint32_t uint_;
24 float float_;
25 long double ldbl_;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010026};
27
Ivan Smirnov4f164212016-06-22 01:07:20 +010028std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050029 return os << "s:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_;
Ivan Smirnov669e1422016-06-21 21:05:29 +010030}
31
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010032PYBIND11_PACKED(struct PackedStruct {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050033 bool bool_;
34 uint32_t uint_;
35 float float_;
36 long double ldbl_;
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010037});
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010038
Ivan Smirnov669e1422016-06-21 21:05:29 +010039std::ostream& operator<<(std::ostream& os, const PackedStruct& v) {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050040 return os << "p:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_;
Ivan Smirnov669e1422016-06-21 21:05:29 +010041}
42
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010043PYBIND11_PACKED(struct NestedStruct {
Ivan Smirnov4f164212016-06-22 01:07:20 +010044 SimpleStruct a;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010045 PackedStruct b;
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010046});
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010047
Ivan Smirnov669e1422016-06-21 21:05:29 +010048std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
Ivan Smirnov4f164212016-06-22 01:07:20 +010049 return os << "n:a=" << v.a << ";b=" << v.b;
Ivan Smirnov669e1422016-06-21 21:05:29 +010050}
51
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010052struct PartialStruct {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050053 bool bool_;
54 uint32_t uint_;
55 float float_;
Ivan Smirnov245f77b2016-08-13 13:20:36 +010056 uint64_t dummy2;
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -050057 long double ldbl_;
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010058};
59
60struct PartialNestedStruct {
Ivan Smirnov245f77b2016-08-13 13:20:36 +010061 uint64_t dummy1;
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010062 PartialStruct a;
Ivan Smirnov245f77b2016-08-13 13:20:36 +010063 uint64_t dummy2;
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010064};
65
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010066struct UnboundStruct { };
67
Ivan Smirnovf9c0def2016-07-20 00:19:24 +010068struct StringStruct {
69 char a[3];
70 std::array<char, 3> b;
71};
72
Bruce Merryb82c0f02017-05-10 11:36:24 +020073struct ComplexStruct {
74 std::complex<float> cflt;
75 std::complex<double> cdbl;
76};
77
78std::ostream& operator<<(std::ostream& os, const ComplexStruct& v) {
79 return os << "c:" << v.cflt << "," << v.cdbl;
80}
81
Bruce Merry8e0d8322017-05-10 10:21:01 +020082struct ArrayStruct {
83 char a[3][4];
84 int32_t b[2];
85 std::array<uint8_t, 3> c;
86 std::array<float, 2> d[4];
87};
88
Ivan Smirnovabd34292016-11-01 13:29:32 +000089PYBIND11_PACKED(struct StructWithUglyNames {
90 int8_t __x__;
91 uint64_t __y__;
92});
93
Ivan Smirnov2f3f3682016-10-20 12:28:47 +010094enum class E1 : int64_t { A = -1, B = 1 };
95enum E2 : uint8_t { X = 1, Y = 2 };
96
97PYBIND11_PACKED(struct EnumStruct {
98 E1 e1;
99 E2 e2;
100});
101
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100102std::ostream& operator<<(std::ostream& os, const StringStruct& v) {
103 os << "a='";
104 for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i];
105 os << "',b='";
106 for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i];
107 return os << "'";
108}
109
Bruce Merry8e0d8322017-05-10 10:21:01 +0200110std::ostream& operator<<(std::ostream& os, const ArrayStruct& v) {
111 os << "a={";
112 for (int i = 0; i < 3; i++) {
113 if (i > 0)
114 os << ',';
115 os << '{';
116 for (int j = 0; j < 3; j++)
117 os << v.a[i][j] << ',';
118 os << v.a[i][3] << '}';
119 }
120 os << "},b={" << v.b[0] << ',' << v.b[1];
121 os << "},c={" << int(v.c[0]) << ',' << int(v.c[1]) << ',' << int(v.c[2]);
122 os << "},d={";
123 for (int i = 0; i < 4; i++) {
124 if (i > 0)
125 os << ',';
126 os << '{' << v.d[i][0] << ',' << v.d[i][1] << '}';
127 }
128 return os << '}';
129}
130
Ivan Smirnov2f3f3682016-10-20 12:28:47 +0100131std::ostream& operator<<(std::ostream& os, const EnumStruct& v) {
132 return os << "e1=" << (v.e1 == E1::A ? "A" : "B") << ",e2=" << (v.e2 == E2::X ? "X" : "Y");
133}
134
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100135template <typename T>
136py::array mkarray_via_buffer(size_t n) {
137 return py::array(py::buffer_info(nullptr, sizeof(T),
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100138 py::format_descriptor<T>::format(),
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100139 1, { n }, { sizeof(T) }));
140}
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100141
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500142#define SET_TEST_VALS(s, i) do { \
143 s.bool_ = (i) % 2 != 0; \
144 s.uint_ = (uint32_t) (i); \
145 s.float_ = (float) (i) * 1.5f; \
146 s.ldbl_ = (long double) (i) * -2.5L; } while (0)
147
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100148template <typename S>
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100149py::array_t<S, 0> create_recarray(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100150 auto arr = mkarray_via_buffer<S>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100151 auto req = arr.request();
152 auto ptr = static_cast<S*>(req.ptr);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100153 for (size_t i = 0; i < n; i++) {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500154 SET_TEST_VALS(ptr[i], i);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100155 }
156 return arr;
157}
158
Ivan Smirnovd0bafd92016-06-26 16:35:28 +0100159std::string get_format_unbound() {
160 return py::format_descriptor<UnboundStruct>::format();
161}
162
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100163py::array_t<NestedStruct, 0> create_nested(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100164 auto arr = mkarray_via_buffer<NestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100165 auto req = arr.request();
166 auto ptr = static_cast<NestedStruct*>(req.ptr);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100167 for (size_t i = 0; i < n; i++) {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500168 SET_TEST_VALS(ptr[i].a, i);
169 SET_TEST_VALS(ptr[i].b, i + 1);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100170 }
171 return arr;
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100172}
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100173
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100174py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
175 auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100176 auto req = arr.request();
177 auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100178 for (size_t i = 0; i < n; i++) {
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500179 SET_TEST_VALS(ptr[i].a, i);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100180 }
181 return arr;
182}
183
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100184py::array_t<StringStruct, 0> create_string_array(bool non_empty) {
185 auto arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
186 if (non_empty) {
187 auto req = arr.request();
188 auto ptr = static_cast<StringStruct*>(req.ptr);
Cris Luengo30d43c42017-04-14 14:33:44 -0600189 for (ssize_t i = 0; i < req.size * req.itemsize; i++)
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100190 static_cast<char*>(req.ptr)[i] = 0;
191 ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a';
192 ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a';
193 ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a';
194
195 ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b';
196 ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b';
197
198 ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c';
199 }
200 return arr;
201}
202
Bruce Merry8e0d8322017-05-10 10:21:01 +0200203py::array_t<ArrayStruct, 0> create_array_array(size_t n) {
204 auto arr = mkarray_via_buffer<ArrayStruct>(n);
205 auto ptr = (ArrayStruct *) arr.mutable_data();
206 for (size_t i = 0; i < n; i++) {
207 for (size_t j = 0; j < 3; j++)
208 for (size_t k = 0; k < 4; k++)
209 ptr[i].a[j][k] = char('A' + (i * 100 + j * 10 + k) % 26);
210 for (size_t j = 0; j < 2; j++)
211 ptr[i].b[j] = int32_t(i * 1000 + j);
212 for (size_t j = 0; j < 3; j++)
213 ptr[i].c[j] = uint8_t(i * 10 + j);
214 for (size_t j = 0; j < 4; j++)
215 for (size_t k = 0; k < 2; k++)
216 ptr[i].d[j][k] = float(i) * 100.0f + float(j) * 10.0f + float(k);
217 }
218 return arr;
219}
220
Ivan Smirnov2f3f3682016-10-20 12:28:47 +0100221py::array_t<EnumStruct, 0> create_enum_array(size_t n) {
222 auto arr = mkarray_via_buffer<EnumStruct>(n);
223 auto ptr = (EnumStruct *) arr.mutable_data();
224 for (size_t i = 0; i < n; i++) {
225 ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2);
226 ptr[i].e2 = static_cast<E2>(1 + (i % 2));
227 }
228 return arr;
229}
230
Bruce Merryb82c0f02017-05-10 11:36:24 +0200231py::array_t<ComplexStruct, 0> create_complex_array(size_t n) {
232 auto arr = mkarray_via_buffer<ComplexStruct>(n);
233 auto ptr = (ComplexStruct *) arr.mutable_data();
234 for (size_t i = 0; i < n; i++) {
235 ptr[i].cflt.real(float(i));
236 ptr[i].cflt.imag(float(i) + 0.25f);
237 ptr[i].cdbl.real(double(i) + 0.5);
238 ptr[i].cdbl.imag(double(i) + 0.75);
239 }
240 return arr;
241}
242
Ivan Smirnov669e1422016-06-21 21:05:29 +0100243template <typename S>
Dean Moldovan665e8802016-08-12 22:28:31 +0200244py::list print_recarray(py::array_t<S, 0> arr) {
245 const auto req = arr.request();
246 const auto ptr = static_cast<S*>(req.ptr);
247 auto l = py::list();
Cris Luengo30d43c42017-04-14 14:33:44 -0600248 for (ssize_t i = 0; i < req.size; i++) {
Dean Moldovan665e8802016-08-12 22:28:31 +0200249 std::stringstream ss;
250 ss << ptr[i];
251 l.append(py::str(ss.str()));
252 }
253 return l;
Ivan Smirnov669e1422016-06-21 21:05:29 +0100254}
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100255
Dean Moldovan665e8802016-08-12 22:28:31 +0200256py::list print_format_descriptors() {
257 const auto fmts = {
258 py::format_descriptor<SimpleStruct>::format(),
259 py::format_descriptor<PackedStruct>::format(),
260 py::format_descriptor<NestedStruct>::format(),
261 py::format_descriptor<PartialStruct>::format(),
262 py::format_descriptor<PartialNestedStruct>::format(),
Ivan Smirnov2f3f3682016-10-20 12:28:47 +0100263 py::format_descriptor<StringStruct>::format(),
Bruce Merry8e0d8322017-05-10 10:21:01 +0200264 py::format_descriptor<ArrayStruct>::format(),
Bruce Merryb82c0f02017-05-10 11:36:24 +0200265 py::format_descriptor<EnumStruct>::format(),
266 py::format_descriptor<ComplexStruct>::format()
Dean Moldovan665e8802016-08-12 22:28:31 +0200267 };
268 auto l = py::list();
269 for (const auto &fmt : fmts) {
270 l.append(py::cast(fmt));
271 }
272 return l;
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100273}
274
Dean Moldovan665e8802016-08-12 22:28:31 +0200275py::list print_dtypes() {
276 const auto dtypes = {
Dean Moldovane18bc022016-10-25 22:12:39 +0200277 py::str(py::dtype::of<SimpleStruct>()),
278 py::str(py::dtype::of<PackedStruct>()),
279 py::str(py::dtype::of<NestedStruct>()),
280 py::str(py::dtype::of<PartialStruct>()),
281 py::str(py::dtype::of<PartialNestedStruct>()),
282 py::str(py::dtype::of<StringStruct>()),
Bruce Merry8e0d8322017-05-10 10:21:01 +0200283 py::str(py::dtype::of<ArrayStruct>()),
Dean Moldovane18bc022016-10-25 22:12:39 +0200284 py::str(py::dtype::of<EnumStruct>()),
Bruce Merryb82c0f02017-05-10 11:36:24 +0200285 py::str(py::dtype::of<StructWithUglyNames>()),
286 py::str(py::dtype::of<ComplexStruct>())
Dean Moldovan665e8802016-08-12 22:28:31 +0200287 };
288 auto l = py::list();
289 for (const auto &s : dtypes) {
290 l.append(s);
291 }
292 return l;
Ivan Smirnov4f164212016-06-22 01:07:20 +0100293}
294
Ivan Smirnove19980c2016-07-24 20:30:17 +0100295py::array_t<int32_t, 0> test_array_ctors(int i) {
296 using arr_t = py::array_t<int32_t, 0>;
297
298 std::vector<int32_t> data { 1, 2, 3, 4, 5, 6 };
Cris Luengo30d43c42017-04-14 14:33:44 -0600299 std::vector<ssize_t> shape { 3, 2 };
Cris Luengod400f602017-04-05 16:13:04 -0600300 std::vector<ssize_t> strides { 8, 4 };
Ivan Smirnove19980c2016-07-24 20:30:17 +0100301
302 auto ptr = data.data();
303 auto vptr = (void *) ptr;
304 auto dtype = py::dtype("int32");
305
306 py::buffer_info buf_ndim1(vptr, 4, "i", 6);
Ivan Smirnovc6257f82016-07-25 00:46:39 +0100307 py::buffer_info buf_ndim1_null(nullptr, 4, "i", 6);
Ivan Smirnove19980c2016-07-24 20:30:17 +0100308 py::buffer_info buf_ndim2(vptr, 4, "i", 2, shape, strides);
Ivan Smirnovc6257f82016-07-25 00:46:39 +0100309 py::buffer_info buf_ndim2_null(nullptr, 4, "i", 2, shape, strides);
310
311 auto fill = [](py::array arr) {
312 auto req = arr.request();
313 for (int i = 0; i < 6; i++) ((int32_t *) req.ptr)[i] = i + 1;
314 return arr;
315 };
Ivan Smirnove19980c2016-07-24 20:30:17 +0100316
317 switch (i) {
318 // shape: (3, 2)
Ivan Smirnovc6257f82016-07-25 00:46:39 +0100319 case 10: return arr_t(shape, strides, ptr);
320 case 11: return py::array(shape, strides, ptr);
321 case 12: return py::array(dtype, shape, strides, vptr);
322 case 13: return arr_t(shape, ptr);
323 case 14: return py::array(shape, ptr);
324 case 15: return py::array(dtype, shape, vptr);
325 case 16: return arr_t(buf_ndim2);
326 case 17: return py::array(buf_ndim2);
327 // shape: (3, 2) - post-fill
328 case 20: return fill(arr_t(shape, strides));
329 case 21: return py::array(shape, strides, ptr); // can't have nullptr due to templated ctor
330 case 22: return fill(py::array(dtype, shape, strides));
331 case 23: return fill(arr_t(shape));
332 case 24: return py::array(shape, ptr); // can't have nullptr due to templated ctor
333 case 25: return fill(py::array(dtype, shape));
334 case 26: return fill(arr_t(buf_ndim2_null));
335 case 27: return fill(py::array(buf_ndim2_null));
Ivan Smirnove19980c2016-07-24 20:30:17 +0100336 // shape: (6, )
Ivan Smirnovc6257f82016-07-25 00:46:39 +0100337 case 30: return arr_t(6, ptr);
338 case 31: return py::array(6, ptr);
339 case 32: return py::array(dtype, 6, vptr);
340 case 33: return arr_t(buf_ndim1);
341 case 34: return py::array(buf_ndim1);
342 // shape: (6, )
343 case 40: return fill(arr_t(6));
344 case 41: return py::array(6, ptr); // can't have nullptr due to templated ctor
345 case 42: return fill(py::array(dtype, 6));
346 case 43: return fill(arr_t(buf_ndim1_null));
347 case 44: return fill(py::array(buf_ndim1_null));
Ivan Smirnove19980c2016-07-24 20:30:17 +0100348 }
349 return arr_t();
350}
351
Ivan Smirnov611e6142016-07-24 23:52:42 +0100352py::list test_dtype_ctors() {
353 py::list list;
354 list.append(py::dtype("int32"));
355 list.append(py::dtype(std::string("float64")));
356 list.append(py::dtype::from_args(py::str("bool")));
357 py::list names, offsets, formats;
358 py::dict dict;
359 names.append(py::str("a")); names.append(py::str("b")); dict["names"] = names;
360 offsets.append(py::int_(1)); offsets.append(py::int_(10)); dict["offsets"] = offsets;
361 formats.append(py::dtype("int32")); formats.append(py::dtype("float64")); dict["formats"] = formats;
362 dict["itemsize"] = py::int_(20);
363 list.append(py::dtype::from_args(dict));
364 list.append(py::dtype(names, formats, offsets, 20));
Patrick Stewart52715762016-11-22 14:56:52 +0000365 list.append(py::dtype(py::buffer_info((void *) 0, sizeof(unsigned int), "I", 1)));
366 list.append(py::dtype(py::buffer_info((void *) 0, 0, "T{i:a:f:b:}", 1)));
Ivan Smirnov611e6142016-07-24 23:52:42 +0100367 return list;
368}
369
Patrick Stewart52715762016-11-22 14:56:52 +0000370struct TrailingPaddingStruct {
371 int32_t a;
372 char b;
373};
374
375py::dtype trailing_padding_dtype() {
376 return py::dtype::of<TrailingPaddingStruct>();
377}
378
379py::dtype buffer_to_dtype(py::buffer& buf) {
380 return py::dtype(buf.request());
381}
382
Ivan Smirnov10af58f2016-07-25 00:15:07 +0100383py::list test_dtype_methods() {
384 py::list list;
385 auto dt1 = py::dtype::of<int32_t>();
386 auto dt2 = py::dtype::of<SimpleStruct>();
387 list.append(dt1); list.append(dt2);
388 list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields()));
389 list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize()));
390 return list;
Ivan Smirnov611e6142016-07-24 23:52:42 +0100391}
392
Patrick Stewart0b6d08a2016-11-21 17:40:43 +0000393struct CompareStruct {
394 bool x;
395 uint32_t y;
396 float z;
397};
398
399py::list test_compare_buffer_info() {
400 py::list list;
401 list.append(py::bool_(py::detail::compare_buffer_info<float>::compare(py::buffer_info(nullptr, sizeof(float), "f", 1))));
402 list.append(py::bool_(py::detail::compare_buffer_info<unsigned>::compare(py::buffer_info(nullptr, sizeof(int), "I", 1))));
403 list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), "l", 1))));
404 list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1))));
405 list.append(py::bool_(py::detail::compare_buffer_info<CompareStruct>::compare(py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1))));
406 return list;
407}
408
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400409test_initializer numpy_dtypes([](py::module &m) {
Dean Moldovan23919172016-08-25 17:08:09 +0200410 try {
411 py::module::import("numpy");
412 } catch (...) {
413 return;
414 }
415
Ivan Smirnovcbbb7832016-10-20 16:47:29 +0100416 // typeinfo may be registered before the dtype descriptor for scalar casts to work...
417 py::class_<SimpleStruct>(m, "SimpleStruct");
418
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500419 PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_);
420 PYBIND11_NUMPY_DTYPE(PackedStruct, bool_, uint_, float_, ldbl_);
Ivan Smirnov5412a052016-07-02 16:18:42 +0100421 PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500422 PYBIND11_NUMPY_DTYPE(PartialStruct, bool_, uint_, float_, ldbl_);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100423 PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100424 PYBIND11_NUMPY_DTYPE(StringStruct, a, b);
Bruce Merry8e0d8322017-05-10 10:21:01 +0200425 PYBIND11_NUMPY_DTYPE(ArrayStruct, a, b, c, d);
Ivan Smirnov2f3f3682016-10-20 12:28:47 +0100426 PYBIND11_NUMPY_DTYPE(EnumStruct, e1, e2);
Bruce Merryb82c0f02017-05-10 11:36:24 +0200427 PYBIND11_NUMPY_DTYPE(ComplexStruct, cflt, cdbl);
Patrick Stewart52715762016-11-22 14:56:52 +0000428 PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b);
Patrick Stewart0b6d08a2016-11-21 17:40:43 +0000429 PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100430
Ivan Smirnova6e6a8b2016-10-23 15:27:13 +0100431 // ... or after
Ivan Smirnovcbbb7832016-10-20 16:47:29 +0100432 py::class_<PackedStruct>(m, "PackedStruct");
433
Ivan Smirnovabd34292016-11-01 13:29:32 +0000434 PYBIND11_NUMPY_DTYPE_EX(StructWithUglyNames, __x__, "x", __y__, "y");
435
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500436 // If uncommented, this should produce a static_assert failure telling the user that the struct
437 // is not a POD type
438// struct NotPOD { std::string v; NotPOD() : v("hi") {}; };
439// PYBIND11_NUMPY_DTYPE(NotPOD, v);
440
Ivan Smirnov4f164212016-06-22 01:07:20 +0100441 m.def("create_rec_simple", &create_recarray<SimpleStruct>);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100442 m.def("create_rec_packed", &create_recarray<PackedStruct>);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100443 m.def("create_rec_nested", &create_nested);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100444 m.def("create_rec_partial", &create_recarray<PartialStruct>);
445 m.def("create_rec_partial_nested", &create_partial_nested);
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100446 m.def("print_format_descriptors", &print_format_descriptors);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100447 m.def("print_rec_simple", &print_recarray<SimpleStruct>);
Ivan Smirnov669e1422016-06-21 21:05:29 +0100448 m.def("print_rec_packed", &print_recarray<PackedStruct>);
449 m.def("print_rec_nested", &print_recarray<NestedStruct>);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100450 m.def("print_dtypes", &print_dtypes);
Ivan Smirnovd0bafd92016-06-26 16:35:28 +0100451 m.def("get_format_unbound", &get_format_unbound);
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100452 m.def("create_string_array", &create_string_array);
453 m.def("print_string_array", &print_recarray<StringStruct>);
Bruce Merry8e0d8322017-05-10 10:21:01 +0200454 m.def("create_array_array", &create_array_array);
455 m.def("print_array_array", &print_recarray<ArrayStruct>);
Ivan Smirnov2f3f3682016-10-20 12:28:47 +0100456 m.def("create_enum_array", &create_enum_array);
457 m.def("print_enum_array", &print_recarray<EnumStruct>);
Bruce Merryb82c0f02017-05-10 11:36:24 +0200458 m.def("create_complex_array", &create_complex_array);
459 m.def("print_complex_array", &print_recarray<ComplexStruct>);
Ivan Smirnove19980c2016-07-24 20:30:17 +0100460 m.def("test_array_ctors", &test_array_ctors);
Ivan Smirnov611e6142016-07-24 23:52:42 +0100461 m.def("test_dtype_ctors", &test_dtype_ctors);
462 m.def("test_dtype_methods", &test_dtype_methods);
Patrick Stewart0b6d08a2016-11-21 17:40:43 +0000463 m.def("compare_buffer_info", &test_compare_buffer_info);
Patrick Stewart52715762016-11-22 14:56:52 +0000464 m.def("trailing_padding_dtype", &trailing_padding_dtype);
465 m.def("buffer_to_dtype", &buffer_to_dtype);
Jason Rhinelanderf7f5bc82017-01-31 11:00:15 -0500466 m.def("f_simple", [](SimpleStruct s) { return s.uint_ * 10; });
467 m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; });
468 m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; });
469 m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400470});
Ivan Smirnovf5f75c62016-07-18 22:47:40 +0100471
472#undef PYBIND11_PACKED