Skip to content

Commit 6ba68c8

Browse files
Some fixes for char signs (#1378)
* Add signed char to HDF5 attribute types * Numpy: return CHAR depending on system char type * Support schar in HDF5 datasets
1 parent 3cd7df7 commit 6ba68c8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

include/openPMD/binding/python/Numpy.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <exception>
3030
#include <string>
31+
#include <type_traits>
3132

3233
namespace openPMD
3334
{
@@ -36,9 +37,23 @@ inline Datatype dtype_from_numpy(pybind11::dtype const dt)
3637
// ref: https://docs.scipy.org/doc/numpy/user/basics.types.html
3738
// ref: https://github.com/numpy/numpy/issues/10678#issuecomment-369363551
3839
if (dt.char_() == pybind11::dtype("b").char_())
39-
return Datatype::CHAR;
40+
if constexpr (std::is_signed_v<char>)
41+
{
42+
return Datatype::CHAR;
43+
}
44+
else
45+
{
46+
return Datatype::SCHAR;
47+
}
4048
else if (dt.char_() == pybind11::dtype("B").char_())
41-
return Datatype::UCHAR;
49+
if constexpr (std::is_unsigned_v<char>)
50+
{
51+
return Datatype::CHAR;
52+
}
53+
else
54+
{
55+
return Datatype::UCHAR;
56+
}
4257
else if (dt.char_() == pybind11::dtype("short").char_())
4358
return Datatype::SHORT;
4459
else if (dt.char_() == pybind11::dtype("intc").char_())

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ void HDF5IOHandlerImpl::openDataset(
991991
d = DT::CHAR;
992992
else if (H5Tequal(dataset_type, H5T_NATIVE_UCHAR))
993993
d = DT::UCHAR;
994+
else if (H5Tequal(dataset_type, H5T_NATIVE_SCHAR))
995+
d = DT::SCHAR;
994996
else if (H5Tequal(dataset_type, H5T_NATIVE_SHORT))
995997
d = DT::SHORT;
996998
else if (H5Tequal(dataset_type, H5T_NATIVE_INT))
@@ -1742,6 +1744,7 @@ void HDF5IOHandlerImpl::readDataset(
17421744
case DT::ULONGLONG:
17431745
case DT::CHAR:
17441746
case DT::UCHAR:
1747+
case DT::SCHAR:
17451748
case DT::BOOL:
17461749
break;
17471750
case DT::UNDEFINED:
@@ -1876,6 +1879,12 @@ void HDF5IOHandlerImpl::readAttribute(
18761879
status = H5Aread(attr_id, attr_type, &u);
18771880
a = Attribute(u);
18781881
}
1882+
else if (H5Tequal(attr_type, H5T_NATIVE_SCHAR))
1883+
{
1884+
signed char u;
1885+
status = H5Aread(attr_id, attr_type, &u);
1886+
a = Attribute(u);
1887+
}
18791888
else if (H5Tequal(attr_type, H5T_NATIVE_SHORT))
18801889
{
18811890
short i;
@@ -2102,6 +2111,12 @@ void HDF5IOHandlerImpl::readAttribute(
21022111
status = H5Aread(attr_id, attr_type, vu.data());
21032112
a = Attribute(vu);
21042113
}
2114+
else if (H5Tequal(attr_type, H5T_NATIVE_SCHAR))
2115+
{
2116+
std::vector<signed char> vu(dims[0], 0);
2117+
status = H5Aread(attr_id, attr_type, vu.data());
2118+
a = Attribute(vu);
2119+
}
21052120
else if (H5Tequal(attr_type, H5T_NATIVE_SHORT))
21062121
{
21072122
std::vector<short> vint16(dims[0], 0);

0 commit comments

Comments
 (0)