# Get POSIX errno from HDF5 errors?

**URL:** <https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003>\
**Category:** HDF5 Library\
**Created:** [October 24, 2018, 10:12am UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003 "2018-10-24T10:12:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas1](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/thomas1/32/1733_2.png) [@thomas1](https://forum.hdfgroup.org/u/thomas1)\
**Post date:** [October 24, 2018, 10:12am UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003/1 "2018-10-24T10:12:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bljones](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@bljones](https://forum.hdfgroup.org/u/bljones)\
**Post date:** [November 7, 2018, 3:21pm UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003/2 "2018-11-07T15:21:40Z")

</div>

Hi Thomas,

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

Thanks!  
-Barbara

---

<div class="post-metadata">

**Author:** ![thomas1](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/thomas1/32/1733_2.png) [@thomas1](https://forum.hdfgroup.org/u/thomas1)\
**Post date:** [April 24, 2019, 11:36am UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003/3 "2019-04-24T11:36:46Z")

</div>

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](https://hdf-discourse-1.s3.amazonaws.com/original/2X/d/d18b94e9a17e34a1de3342a03960eb6b5c87f09b.patch) (7.3 KB)

---

<div class="post-metadata">

**Author:** ![thomas1](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/thomas1/32/1733_2.png) [@thomas1](https://forum.hdfgroup.org/u/thomas1)\
**Post date:** [January 23, 2020, 1:37pm UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003/4 "2020-01-23T13:37:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thomas1](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/thomas1/32/1733_2.png) [@thomas1](https://forum.hdfgroup.org/u/thomas1)\
**Post date:** [February 16, 2021, 3:24pm UTC](https://forum.hdfgroup.org/t/get-posix-errno-from-hdf5-errors/5003/5 "2021-02-16T15:24:20Z")

</div>

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](https://github.com/HDFGroup/hdf5/pull/37)). We’re [working on using that](https://github.com/h5py/h5py/pull/1815), hopefully for h5py 3.2.
