Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ struct field_descriptor {
const char *name;
size_t offset;
size_t size;
size_t alignment;
std::string format;
dtype descr;
};
Expand Down Expand Up @@ -726,8 +727,10 @@ inline PYBIND11_NOINLINE void register_structured_dtype(
for (auto& field : ordered_fields) {
if (field.offset > offset)
oss << (field.offset - offset) << 'x';
// note that '=' is required to cover the case of unaligned fields
oss << '=' << field.format << ':' << field.name << ':';
// mark unaligned fields with '='
if (field.offset % field.alignment)
oss << '=';
oss << field.format << ':' << field.name << ':';
offset = field.offset + field.size;
}
if (itemsize > offset)
Expand Down Expand Up @@ -787,6 +790,7 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> {
#define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \
::pybind11::detail::field_descriptor { \
Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \
alignof(decltype(std::declval<T>().Field)), \
::pybind11::format_descriptor<decltype(std::declval<T>().Field)>::format(), \
::pybind11::detail::npy_format_descriptor<decltype(std::declval<T>().Field)>::dtype() \
}
Expand Down
14 changes: 7 additions & 7 deletions tests/test_numpy_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def test_format_descriptors():
assert re.match('^NumPy type info missing for .*UnboundStruct.*$', str(excinfo.value))

assert print_format_descriptors() == [
"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}",
"T{=3s:a:=3s:b:}",
'T{=q:e1:=B:e2:}'
"T{?:x:3xI:y:f:z:}",
"T{?:x:=I:y:=f:z:}",
"T{T{?:x:3xI:y:f:z:}:a:T{?:x:=I:y:=f:z:}:b:}",
"T{?:x:3xI:y:f:z:12x}",
"T{8xT{?:x:3xI:y:f:z:12x}:a:8x}",
"T{3s:a:3s:b:}",
'T{q:e1:B:e2:}'
]


Expand Down