/* tests/test_numpy_dtypes.cpp -- Structured and compound NumPy dtypes Copyright (c) 2016 Ivan Smirnov All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #include "pybind11_tests.h" #include #ifdef __GNUC__ #define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) #else #define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) #endif namespace py = pybind11; struct SimpleStruct { bool bool_; uint32_t uint_; float float_; long double ldbl_; }; std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) { return os << "s:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; } PYBIND11_PACKED(struct PackedStruct { bool bool_; uint32_t uint_; float float_; long double ldbl_; }); std::ostream& operator<<(std::ostream& os, const PackedStruct& v) { return os << "p:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; } PYBIND11_PACKED(struct NestedStruct { SimpleStruct a; PackedStruct b; }); std::ostream& operator<<(std::ostream& os, const NestedStruct& v) { return os << "n:a=" << v.a << ";b=" << v.b; } struct PartialStruct { bool bool_; uint32_t uint_; float float_; uint64_t dummy2; long double ldbl_; }; struct PartialNestedStruct { uint64_t dummy1; PartialStruct a; uint64_t dummy2; }; struct UnboundStruct { }; struct StringStruct { char a[3]; std::array b; }; PYBIND11_PACKED(struct StructWithUglyNames { int8_t __x__; uint64_t __y__; }); enum class E1 : int64_t { A = -1, B = 1 }; enum E2 : uint8_t { X = 1, Y = 2 }; PYBIND11_PACKED(struct EnumStruct { E1 e1; E2 e2; }); std::ostream& operator<<(std::ostream& os, const StringStruct& v) { os << "a='"; for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i]; os << "',b='"; for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i]; return os << "'"; } std::ostream& operator<<(std::ostream& os, const EnumStruct& v) { return os << "e1=" << (v.e1 == E1::A ? "A" : "B") << ",e2=" << (v.e2 == E2::X ? "X" : "Y"); } template py::array mkarray_via_buffer(size_t n) { return py::array(py::buffer_info(nullptr, sizeof(T), py::format_descriptor::format(), 1, { n }, { sizeof(T) })); } #define SET_TEST_VALS(s, i) do { \ s.bool_ = (i) % 2 != 0; \ s.uint_ = (uint32_t) (i); \ s.float_ = (float) (i) * 1.5f; \ s.ldbl_ = (long double) (i) * -2.5L; } while (0) template py::array_t create_recarray(size_t n) { auto arr = mkarray_via_buffer(n); auto req = arr.request(); auto ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i], i); } return arr; } std::string get_format_unbound() { return py::format_descriptor::format(); } py::array_t create_nested(size_t n) { auto arr = mkarray_via_buffer(n); auto req = arr.request(); auto ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i].a, i); SET_TEST_VALS(ptr[i].b, i + 1); } return arr; } py::array_t create_partial_nested(size_t n) { auto arr = mkarray_via_buffer(n); auto req = arr.request(); auto ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i].a, i); } return arr; } py::array_t create_string_array(bool non_empty) { auto arr = mkarray_via_buffer(non_empty ? 4 : 0); if (non_empty) { auto req = arr.request(); auto ptr = static_cast(req.ptr); for (size_t i = 0; i < req.size * req.itemsize; i++) static_cast(req.ptr)[i] = 0; ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a'; ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a'; ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a'; ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b'; ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b'; ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c'; } return arr; } py::array_t create_enum_array(size_t n) { auto arr = mkarray_via_buffer(n); auto ptr = (EnumStruct *) arr.mutable_data(); for (size_t i = 0; i < n; i++) { ptr[i].e1 = static_cast(-1 + ((int) i % 2) * 2); ptr[i].e2 = static_cast(1 + (i % 2)); } return arr; } template py::list print_recarray(py::array_t arr) { const auto req = arr.request(); const auto ptr = static_cast(req.ptr); auto l = py::list(); for (size_t i = 0; i < req.size; i++) { std::stringstream ss; ss << ptr[i]; l.append(py::str(ss.str())); } return l; } py::list print_format_descriptors() { const auto fmts = { py::format_descriptor::format(), py::format_descriptor::format(), py::format_descriptor::format(), py::format_descriptor::format(), py::format_descriptor::format(), py::format_descriptor::format(), py::format_descriptor::format() }; auto l = py::list(); for (const auto &fmt : fmts) { l.append(py::cast(fmt)); } return l; } py::list print_dtypes() { const auto dtypes = { py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()), py::str(py::dtype::of()) }; auto l = py::list(); for (const auto &s : dtypes) { l.append(s); } return l; } py::array_t test_array_ctors(int i) { using arr_t = py::array_t; std::vector data { 1, 2, 3, 4, 5, 6 }; std::vector shape { 3, 2 }; std::vector strides { 8, 4 }; auto ptr = data.data(); auto vptr = (void *) ptr; auto dtype = py::dtype("int32"); py::buffer_info buf_ndim1(vptr, 4, "i", 6); py::buffer_info buf_ndim1_null(nullptr, 4, "i", 6); py::buffer_info buf_ndim2(vptr, 4, "i", 2, shape, strides); py::buffer_info buf_ndim2_null(nullptr, 4, "i", 2, shape, strides); auto fill = [](py::array arr) { auto req = arr.request(); for (int i = 0; i < 6; i++) ((int32_t *) req.ptr)[i] = i + 1; return arr; }; switch (i) { // shape: (3, 2) case 10: return arr_t(shape, strides, ptr); case 11: return py::array(shape, strides, ptr); case 12: return py::array(dtype, shape, strides, vptr); case 13: return arr_t(shape, ptr); case 14: return py::array(shape, ptr); case 15: return py::array(dtype, shape, vptr); case 16: return arr_t(buf_ndim2); case 17: return py::array(buf_ndim2); // shape: (3, 2) - post-fill case 20: return fill(arr_t(shape, strides)); case 21: return py::array(shape, strides, ptr); // can't have nullptr due to templated ctor case 22: return fill(py::array(dtype, shape, strides)); case 23: return fill(arr_t(shape)); case 24: return py::array(shape, ptr); // can't have nullptr due to templated ctor case 25: return fill(py::array(dtype, shape)); case 26: return fill(arr_t(buf_ndim2_null)); case 27: return fill(py::array(buf_ndim2_null)); // shape: (6, ) case 30: return arr_t(6, ptr); case 31: return py::array(6, ptr); case 32: return py::array(dtype, 6, vptr); case 33: return arr_t(buf_ndim1); case 34: return py::array(buf_ndim1); // shape: (6, ) case 40: return fill(arr_t(6)); case 41: return py::array(6, ptr); // can't have nullptr due to templated ctor case 42: return fill(py::array(dtype, 6)); case 43: return fill(arr_t(buf_ndim1_null)); case 44: return fill(py::array(buf_ndim1_null)); } return arr_t(); } py::list test_dtype_ctors() { py::list list; list.append(py::dtype("int32")); list.append(py::dtype(std::string("float64"))); list.append(py::dtype::from_args(py::str("bool"))); py::list names, offsets, formats; py::dict dict; names.append(py::str("a")); names.append(py::str("b")); dict["names"] = names; offsets.append(py::int_(1)); offsets.append(py::int_(10)); dict["offsets"] = offsets; formats.append(py::dtype("int32")); formats.append(py::dtype("float64")); dict["formats"] = formats; dict["itemsize"] = py::int_(20); list.append(py::dtype::from_args(dict)); list.append(py::dtype(names, formats, offsets, 20)); list.append(py::dtype(py::buffer_info((void *) 0, sizeof(unsigned int), "I", 1))); list.append(py::dtype(py::buffer_info((void *) 0, 0, "T{i:a:f:b:}", 1))); return list; } struct TrailingPaddingStruct { int32_t a; char b; }; py::dtype trailing_padding_dtype() { return py::dtype::of(); } py::dtype buffer_to_dtype(py::buffer& buf) { return py::dtype(buf.request()); } py::list test_dtype_methods() { py::list list; auto dt1 = py::dtype::of(); auto dt2 = py::dtype::of(); list.append(dt1); list.append(dt2); list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields())); list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize())); return list; } struct CompareStruct { bool x; uint32_t y; float z; }; py::list test_compare_buffer_info() { py::list list; list.append(py::bool_(py::detail::compare_buffer_info::compare(py::buffer_info(nullptr, sizeof(float), "f", 1)))); list.append(py::bool_(py::detail::compare_buffer_info::compare(py::buffer_info(nullptr, sizeof(int), "I", 1)))); list.append(py::bool_(py::detail::compare_buffer_info::compare(py::buffer_info(nullptr, sizeof(long), "l", 1)))); list.append(py::bool_(py::detail::compare_buffer_info::compare(py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1)))); list.append(py::bool_(py::detail::compare_buffer_info::compare(py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1)))); return list; } test_initializer numpy_dtypes([](py::module &m) { try { py::module::import("numpy"); } catch (...) { return; } // typeinfo may be registered before the dtype descriptor for scalar casts to work... py::class_(m, "SimpleStruct"); PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); PYBIND11_NUMPY_DTYPE(PackedStruct, bool_, uint_, float_, ldbl_); PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); PYBIND11_NUMPY_DTYPE(PartialStruct, bool_, uint_, float_, ldbl_); PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a); PYBIND11_NUMPY_DTYPE(StringStruct, a, b); PYBIND11_NUMPY_DTYPE(EnumStruct, e1, e2); PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b); PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z); // ... or after py::class_(m, "PackedStruct"); PYBIND11_NUMPY_DTYPE_EX(StructWithUglyNames, __x__, "x", __y__, "y"); // If uncommented, this should produce a static_assert failure telling the user that the struct // is not a POD type // struct NotPOD { std::string v; NotPOD() : v("hi") {}; }; // PYBIND11_NUMPY_DTYPE(NotPOD, v); m.def("create_rec_simple", &create_recarray); m.def("create_rec_packed", &create_recarray); m.def("create_rec_nested", &create_nested); m.def("create_rec_partial", &create_recarray); m.def("create_rec_partial_nested", &create_partial_nested); m.def("print_format_descriptors", &print_format_descriptors); m.def("print_rec_simple", &print_recarray); m.def("print_rec_packed", &print_recarray); m.def("print_rec_nested", &print_recarray); m.def("print_dtypes", &print_dtypes); m.def("get_format_unbound", &get_format_unbound); m.def("create_string_array", &create_string_array); m.def("print_string_array", &print_recarray); m.def("create_enum_array", &create_enum_array); m.def("print_enum_array", &print_recarray); m.def("test_array_ctors", &test_array_ctors); m.def("test_dtype_ctors", &test_dtype_ctors); m.def("test_dtype_methods", &test_dtype_methods); m.def("compare_buffer_info", &test_compare_buffer_info); m.def("trailing_padding_dtype", &trailing_padding_dtype); m.def("buffer_to_dtype", &buffer_to_dtype); m.def("f_simple", [](SimpleStruct s) { return s.uint_ * 10; }); m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; }); m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; }); m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); }); }); #undef PYBIND11_PACKED