Skip to content

Commit

Permalink
Merge pull request #157 from SWIFTSIM/more_robust_metadat_reading
Browse files Browse the repository at this point in the history
Make the reading of strings in snapshots header more robust to h5py's…
  • Loading branch information
JBorrow authored Mar 13, 2023
2 parents 451e182 + ff97024 commit e65e705
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion swiftsimio/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,15 @@ def postprocess_header(self):

for field, name in metadata.metadata_fields.header_unpack_string.items():
try:
setattr(self, name, self.header[field].decode("utf-8"))
# Deal with h5py's quirkiness that fixed-sized and variable-sized
# strings are read as strings or bytes
# See: https://github.com/h5py/h5py/issues/2172
raw = self.header[field]
try:
string = raw.decode("utf-8")
except AttributeError:
string = raw
setattr(self, name, string)
except KeyError:
# Must not be present, just skip it
setattr(self, name, "")
Expand Down

0 comments on commit e65e705

Please sign in to comment.