diff --git a/HISTORY.rst b/HISTORY.rst index aed47df..ffad389 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,7 @@ History X.Y.Z (YYYY-MM-DD) ------------------ +* Add a weakref.finalize method to close HDUList objects on FitsProxy instances (:pr:`12`) * Depend on fsspec (:pr:`11`) * Improve dask array name determinism (:pr:`10`) * Change license from GPL3 to BSD3 (:pr:`9``) diff --git a/xarrayfits/fits_proxy.py b/xarrayfits/fits_proxy.py index 696da74..298db55 100644 --- a/xarrayfits/fits_proxy.py +++ b/xarrayfits/fits_proxy.py @@ -54,8 +54,9 @@ def hdu_list(self): try: return self._hdul except AttributeError: - self._hdul = fits.open(self._filename, **self._kwargs) - return self._hdul + self._hdul = hdul = fits.open(self._filename, **self._kwargs) + weakref.finalize(self, hdul.close) + return hdul def __hash__(self): return hash((self._filename, tuple(set(self._kwargs.items()))))