From 99f1fb98dfdaae7990de39f8a71b82d588d6df54 Mon Sep 17 00:00:00 2001 From: Nils Wentzell Date: Tue, 13 Feb 2024 10:24:55 -0500 Subject: [PATCH] Allow reading of all integer types into Python long without warning --- python/h5/h5py_io.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/python/h5/h5py_io.cpp b/python/h5/h5py_io.cpp index e547fd3b..1bac7710 100644 --- a/python/h5/h5py_io.cpp +++ b/python/h5/h5py_io.cpp @@ -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(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_INT)) { + return PyLong_FromLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_LONG)) { + return PyLong_FromLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_LLONG)) { + return PyLong_FromLongLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_USHORT)) { + return PyLong_FromUnsignedLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_UINT)) { + return PyLong_FromUnsignedLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_ULONG)) { + return PyLong_FromUnsignedLong(h5_read(g, name)); + } else if (H5Tequal(h5type, H5T_NATIVE_ULLONG)) { + return PyLong_FromUnsignedLongLong(h5_read(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(); @@ -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 x; h5_read(g, name, x);