master: Print offending key on HDF5 dataset type error

This helps debugging the cause of TypeErrors arising from types
not handled by the HDF5 serializer, as the backtrace doesn't
otherwise include any useful information.
This commit is contained in:
David Nadlinger 2018-06-25 10:50:30 +01:00
parent 56b2e0c262
commit 01c3000ef3
1 changed files with 12 additions and 2 deletions

View File

@ -173,8 +173,18 @@ class DatasetManager:
def write_hdf5(self, f):
datasets_group = f.create_group("datasets")
for k, v in self.local.items():
datasets_group[k] = v
_write(datasets_group, k, v)
archive_group = f.create_group("archive")
for k, v in self.archive.items():
archive_group[k] = v
_write(archive_group, k, v)
def _write(group, k, v):
# Add context to exception message when the user writes a dataset that is
# not representable in HDF5.
try:
group[k] = v
except TypeError as e:
raise TypeError("Error writing dataset '{}' of type '{}': {}".format(
k, type(v), e))