mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 17:26:13 +00:00
Strip padding fields in dtypes, update the tests
This commit is contained in:
@@ -44,6 +44,19 @@ std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
|
||||
return os << "n:a=" << v.a << ";b=" << v.b;
|
||||
}
|
||||
|
||||
struct PartialStruct {
|
||||
bool x;
|
||||
uint32_t y;
|
||||
float z;
|
||||
long dummy2;
|
||||
};
|
||||
|
||||
struct PartialNestedStruct {
|
||||
long dummy1;
|
||||
PartialStruct a;
|
||||
long dummy2;
|
||||
};
|
||||
|
||||
struct UnboundStruct { };
|
||||
|
||||
template <typename T>
|
||||
@@ -54,7 +67,7 @@ py::array mkarray_via_buffer(size_t n) {
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
py::array_t<S> create_recarray(size_t n) {
|
||||
py::array_t<S, 0> create_recarray(size_t n) {
|
||||
auto arr = mkarray_via_buffer<S>(n);
|
||||
auto ptr = static_cast<S*>(arr.request().ptr);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
@@ -67,7 +80,7 @@ std::string get_format_unbound() {
|
||||
return py::format_descriptor<UnboundStruct>::format();
|
||||
}
|
||||
|
||||
py::array_t<NestedStruct> create_nested(size_t n) {
|
||||
py::array_t<NestedStruct, 0> create_nested(size_t n) {
|
||||
auto arr = mkarray_via_buffer<NestedStruct>(n);
|
||||
auto ptr = static_cast<NestedStruct*>(arr.request().ptr);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
@@ -77,8 +90,17 @@ py::array_t<NestedStruct> create_nested(size_t n) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
|
||||
auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
|
||||
auto ptr = static_cast<PartialNestedStruct*>(arr.request().ptr);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
void print_recarray(py::array_t<S> arr) {
|
||||
void print_recarray(py::array_t<S, 0> arr) {
|
||||
auto buf = arr.request();
|
||||
auto ptr = static_cast<S*>(buf.ptr);
|
||||
for (size_t i = 0; i < buf.size; i++)
|
||||
@@ -89,6 +111,8 @@ void print_format_descriptors() {
|
||||
std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<PackedStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<NestedStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<PartialStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<PartialNestedStruct>::format() << std::endl;
|
||||
}
|
||||
|
||||
void print_dtypes() {
|
||||
@@ -98,16 +122,22 @@ void print_dtypes() {
|
||||
std::cout << to_str(py::dtype_of<SimpleStruct>()) << std::endl;
|
||||
std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl;
|
||||
std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl;
|
||||
std::cout << to_str(py::dtype_of<PartialStruct>()) << std::endl;
|
||||
std::cout << to_str(py::dtype_of<PartialNestedStruct>()) << std::endl;
|
||||
}
|
||||
|
||||
void init_ex20(py::module &m) {
|
||||
PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
|
||||
PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
|
||||
PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
|
||||
PYBIND11_NUMPY_DTYPE(PartialStruct, x, y, z);
|
||||
PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
|
||||
|
||||
m.def("create_rec_simple", &create_recarray<SimpleStruct>);
|
||||
m.def("create_rec_packed", &create_recarray<PackedStruct>);
|
||||
m.def("create_rec_nested", &create_nested);
|
||||
m.def("create_rec_partial", &create_recarray<PartialStruct>);
|
||||
m.def("create_rec_partial_nested", &create_partial_nested);
|
||||
m.def("print_format_descriptors", &print_format_descriptors);
|
||||
m.def("print_rec_simple", &print_recarray<SimpleStruct>);
|
||||
m.def("print_rec_packed", &print_recarray<PackedStruct>);
|
||||
|
||||
Reference in New Issue
Block a user