prjemian / punx

Python Utilities for NeXus HDF5 files
https://prjemian.github.io/punx
5 stars 7 forks source link

tree: KeyError when ExternalLink file cannot be found #233

Closed prjemian closed 1 year ago

prjemian commented 1 year ago

While creating a demo for the h5py.ExternalLink, found the application raises a KeyError when the external file is not found.

import h5py, pathlib

demo = pathlib.Path("demo.h5")
demo.unlink(missing_ok=True)

with h5py.File(demo, "w") as root:
    root["linked"] = h5py.ExternalLink("other.h5", "/number")

!punx tree demo.h5

produces this output:

!!! WARNING: this program is not ready for distribution.

Traceback (most recent call last):
  File "/home/prjemian/.conda/envs/bluesky_2023_3/bin/punx", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/prjemian/Documents/projects/prjemian/punx/punx/main.py", line 441, in main
    args.func(args)
  File "/home/prjemian/Documents/projects/prjemian/punx/punx/main.py", line 178, in func_tree
    mc = h5tree.Hdf5TreeView(os.path.abspath(args.infile))
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/prjemian/Documents/projects/prjemian/punx/punx/h5tree.py", line 56, in __init__
    self.isNeXus = utils.isNeXusFile(filename)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/prjemian/Documents/projects/prjemian/punx/punx/utils.py", line 120, in isNeXusFile
    if isNeXusGroup(f[item], "NXentry"):
                    ~^^^^^^
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/home/prjemian/.conda/envs/bluesky_2023_3/lib/python3.11/site-packages/h5py/_hl/group.py", line 357, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to synchronously open object (unable to open external file, external link file name = 'other.h5')"
prjemian commented 1 year ago

Expect that punx tree should report external link parameters (file, h5addr) and note the file cannot be found. Should not raise KeyError and terminate application.