Get POSIX errno from HDF5 errors?

In the h5py Python bindings, it would be nice to raise idiomatic Python exceptions such as FileNotFoundError when a file opened for reading does not exist.

To do this reliably, we would need access to the POSIX errno; Python maps from e.g. ENOENT -> FileNotFoundError. HDF5 captures the errno, but as far as I can see, it only provides it formatted into a string. We don’t really want error handling code to be dependent on fragile regexes to extract a number from the string.

Would it be possible for the HDF5 error handling code to store the POSIX errno as well as its own error codes? E.g. the H5E_error2_t struct could grow a posix_errno member. This would be 0 except when the error comes from a system error for which there is an errno.

Another possible avenue would be if HDF5 had an API to pass in a file descriptor instead of a path. That would let h5py open the file using standard Python functions (raising standard Python errors if needed), and then hand the file descriptor to libhdf5. I can’t see any such API at the moment, but maybe I’m missing something?

Hi Thomas,

I entered an enhancement report (HDFFV-10631) for this issue.

Thanks!
-Barbara

1 Like

I’ve had a go at preparing a patch for this, against the current develop branch of HDF5. I don’t usually work with C, so this might be completely unusable, but it builds and doesn’t seem to break the tests.

My understanding is that it’s safe to add fields at the end of a struct, so that’s what I’ve done, but I’m not very familiar with this kind of thing.

posix_errno.patch (7.3 KB)

Any updates on this? It would be a clear enhancement to the Python API if we could raise more precise exceptions when these errors occur.

To record the outcome here: HDF group agreed that we could rely on finding something like errno = 2 in the error message (a comment was added to the source code to highlight that this should be preserved). We’re working on using that, hopefully for h5py 3.2.