Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/nexusformat/nexus/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,11 @@ def __enter__(self):

def __exit__(self, *args):
"""Close the NeXus file and, if necessary, release the lock."""
if self._with_count == 1:
self.close()
self._with_count -= 1
try:
if self._with_count == 1:
self.close()
finally:
self._with_count -= 1

def __del__(self):
"""Close the file and delete the NXFile instance."""
Expand Down Expand Up @@ -721,9 +723,11 @@ def close(self):
-----
The file modification time of the root object is updated.
"""
if self.is_open():
self._file.close()
self.release_lock()
try:
if self.is_open():
self._file.close()
finally:
self.release_lock()
try:
self._root._mtime = self.mtime
except Exception:
Expand Down Expand Up @@ -4886,7 +4890,7 @@ def __getitem__(self, key):
try:
path = PurePath(str(key))
except TypeError:
raise NeXusError("Invalid index")
raise NeXusError(f"'{key}' is an invalid index")
if path.is_absolute():
node = self.nxroot
path = path.relative_to('/')
Expand All @@ -4896,7 +4900,7 @@ def __getitem__(self, key):
try:
node = node.entries[name]
except KeyError:
raise NeXusError("Invalid path")
raise NeXusError(f"'{path}' is an invalid path")
return node

def __setitem__(self, key, value):
Expand Down Expand Up @@ -5007,7 +5011,7 @@ def __delitem__(self, key):
if name in group:
group = group[name]
else:
raise NeXusError("Invalid path")
raise NeXusError(f"'{key}' is an invalid path")
if key not in group:
raise NeXusError("'"+key+"' not in "+group.nxpath)
elif group[key].is_linked():
Expand Down
Loading