Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the 2 opened file in the Nexus class are actually closed when the object is deleted #2453

Merged
merged 5 commits into from
Feb 21, 2025
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
35 changes: 24 additions & 11 deletions src/pyFAI/io/nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Project: Azimuthal integration
# https://github.com/silx-kit/pyFAI
#
# Copyright (C) 2015-2024 European Synchrotron Radiation Facility, Grenoble, France
# Copyright (C) 2015-2025 European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme Kieffer ([email protected])
#
Expand Down Expand Up @@ -31,7 +31,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "24/04/2024"
__date__ = "21/02/2025"
__status__ = "production"
__docformat__ = 'restructuredtext'

Expand Down Expand Up @@ -165,18 +165,31 @@ def __init__(self, filename, mode=None, creator=None, start_time=None):
self.h5.attrs["HDF5_Version"] = h5py.version.hdf5_version
self.h5.attrs["creator"] = creator or self.__class__.__name__

def __del__(self):
self.close()

def close(self, end_time=None):
"""
close the filename and update all entries
Close the file and update all entries.
"""
if self.mode != "r":
end_time = get_isotime(end_time)
for entry in self.to_close:
entry["end_time"] = end_time
self.h5.attrs["file_update_time"] = get_isotime()
self.h5.close()
if self.file_handle:
self.file_handle.close()
try:
if self.mode != "r":
if self.h5:
end_time = get_isotime(end_time)
while self.to_close:
entry = self.to_close.pop()
entry["end_time"] = end_time
self.h5.attrs["file_update_time"] = get_isotime()
except Exception as error:
sys.stderr.write(f"{type(error)}: {error},\nwhile finalizing Nexus file\n")

try:
if self.h5:
self.h5.close()
if self.file_handle:
self.file_handle.close()
except Exception as error:
sys.stderr.write(f"Error closing file: {error}\n")

# Context manager for "with" statement compatibility
def __enter__(self, *arg, **kwarg):
Expand Down