mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-11 08:50:31 +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>);
|
||||
|
||||
@@ -5,7 +5,8 @@ import unittest
|
||||
import numpy as np
|
||||
from example import (
|
||||
create_rec_simple, create_rec_packed, create_rec_nested, print_format_descriptors,
|
||||
print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes, get_format_unbound
|
||||
print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes, get_format_unbound,
|
||||
create_rec_partial, create_rec_partial_nested
|
||||
)
|
||||
|
||||
|
||||
@@ -23,6 +24,8 @@ simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
|
||||
'offsets': [0, 4, 8]})
|
||||
packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
|
||||
|
||||
elements = [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)]
|
||||
|
||||
for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
|
||||
arr = func(0)
|
||||
assert arr.dtype == dtype
|
||||
@@ -31,14 +34,30 @@ for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packe
|
||||
|
||||
arr = func(3)
|
||||
assert arr.dtype == dtype
|
||||
check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], simple_dtype)
|
||||
check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], packed_dtype)
|
||||
check_eq(arr, elements, simple_dtype)
|
||||
check_eq(arr, elements, packed_dtype)
|
||||
|
||||
if dtype == simple_dtype:
|
||||
print_rec_simple(arr)
|
||||
else:
|
||||
print_rec_packed(arr)
|
||||
|
||||
|
||||
arr = create_rec_partial(3)
|
||||
print(arr.dtype)
|
||||
partial_dtype = arr.dtype
|
||||
assert '' not in arr.dtype.fields
|
||||
assert partial_dtype.itemsize > simple_dtype.itemsize
|
||||
check_eq(arr, elements, simple_dtype)
|
||||
check_eq(arr, elements, packed_dtype)
|
||||
|
||||
arr = create_rec_partial_nested(3)
|
||||
print(arr.dtype)
|
||||
assert '' not in arr.dtype.fields
|
||||
assert '' not in arr.dtype.fields['a'][0].fields
|
||||
assert arr.dtype.itemsize > partial_dtype.itemsize
|
||||
np.testing.assert_equal(arr['a'], create_rec_partial(3))
|
||||
|
||||
nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])
|
||||
|
||||
arr = create_rec_nested(0)
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
T{?:x:xxxI:y:f:z:}
|
||||
T{?:x:=I:y:f:z:}
|
||||
T{T{?:x:xxxI:y:f:z:}:a:T{?:x:=I:y:f:z:}:b:}
|
||||
T{=?:x:3x=I:y:=f:z:}
|
||||
T{=?:x:=I:y:=f:z:}
|
||||
T{=T{=?:x:3x=I:y:=f:z:}:a:=T{=?:x:=I:y:=f:z:}:b:}
|
||||
T{=?:x:3x=I:y:=f:z:12x}
|
||||
T{8x=T{=?:x:3x=I:y:=f:z:12x}:a:8x}
|
||||
{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}
|
||||
[('x', '?'), ('y', '<u4'), ('z', '<f4')]
|
||||
[('a', {'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}), ('b', [('x', '?'), ('y', '<u4'), ('z', '<f4')])]
|
||||
{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}
|
||||
{'names':['a'], 'formats':[{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}], 'offsets':[8], 'itemsize':40}
|
||||
s:0,0,0
|
||||
s:1,1,1.5
|
||||
s:0,2,3
|
||||
p:0,0,0
|
||||
p:1,1,1.5
|
||||
p:0,2,3
|
||||
{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}
|
||||
{'names':['a'], 'formats':[{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}], 'offsets':[8], 'itemsize':40}
|
||||
n:a=s:0,0,0;b=p:1,1,1.5
|
||||
n:a=s:1,1,1.5;b=p:0,2,3
|
||||
n:a=s:0,2,3;b=p:1,3,4.5
|
||||
Reference in New Issue
Block a user