Skip to content

Commit

Permalink
Allow reading of all integer types into Python long without warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Feb 13, 2024
1 parent 3c9b08d commit 99f1fb9
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions python/h5/h5py_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ namespace h5 {

// -------------------------

// Read any integer type from hdf5 and return a Python long
PyObject *h5_read_any_int(group g, std::string const &name, auto h5type) {
if (H5Tequal(h5type, H5T_NATIVE_SHORT)) {
return PyLong_FromLong(h5_read<short>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_INT)) {
return PyLong_FromLong(h5_read<int>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_LONG)) {
return PyLong_FromLong(h5_read<long>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_LLONG)) {
return PyLong_FromLongLong(h5_read<long long>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_USHORT)) {
return PyLong_FromUnsignedLong(h5_read<unsigned short>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_UINT)) {
return PyLong_FromUnsignedLong(h5_read<unsigned int>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_ULONG)) {
return PyLong_FromUnsignedLong(h5_read<unsigned long>(g, name));
} else if (H5Tequal(h5type, H5T_NATIVE_ULLONG)) {
return PyLong_FromUnsignedLongLong(h5_read<unsigned long long>(g, name));
} else {
PyErr_SetString(PyExc_RuntimeError, "h5_read to Python: unknown integer type");
return NULL;
}
}

PyObject *h5_read_bare(group g, std::string const &name) { // There should be no errors from h5 reading
import_numpy();

Expand All @@ -215,11 +239,7 @@ namespace h5 {
h5_read(g, name, x);
return PyFloat_FromDouble(x);
}
if (H5Tget_class(lt.ty) == H5T_INTEGER) {
long x;
h5_read(g, name, x);
return PyLong_FromLong(x);
}
if (H5Tget_class(lt.ty) == H5T_INTEGER) { return h5_read_any_int(g, name, lt.ty); }
if (H5Tequal(lt.ty, h5::hdf5_type<bool>())) {
bool x;
h5_read(g, name, x);
Expand Down

0 comments on commit 99f1fb9

Please sign in to comment.