Skip to content

Commit

Permalink
Merge pull request #533 from stscieisenhamer/ijwst228-file-handles
Browse files Browse the repository at this point in the history
Ensure when closing, to explicitly close data blocks
  • Loading branch information
drdavella authored Aug 17, 2018
2 parents 2994900 + 894161d commit 485fdb7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Update asdf-standard to reflect more stringent (and, consequently, more
correct) requirements on the formatting of complex numbers. [#526]

- Fix bug with dangling file handle when using ASDF-in-FITS. [#533]

2.0.2 (2018-07-27)
------------------

Expand Down
2 changes: 1 addition & 1 deletion asdf/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ def close(self):
self._data.flush()
if self._data._mmap is not None:
self._data._mmap.close()
self._data = None
self._data = None


class UnloadedBlock(object):
Expand Down
34 changes: 34 additions & 0 deletions asdf/tests/test_fits_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,37 @@ def test_verify_with_astropy(tmpdir):

with fits.open(tmpfile) as hdu:
hdu.verify('exception')

def test_dangling_file_handle(tmpdir):
"""
This tests the bug fix introduced in #533. Without the bug fix, this test
will fail when running the test suite with pytest-openfiles.
"""
import gc

fits_filename = str(tmpdir.join('dangling.fits'))

# Create FITS file to use for test
hdulist = fits.HDUList()
hdulist.append(fits.ImageHDU(np.arange(512, dtype=np.float)))
hdulist.append(fits.ImageHDU(np.arange(512, dtype=np.float)))
hdulist.append(fits.ImageHDU(np.arange(512, dtype=np.float)))
hdulist.writeto(fits_filename)
hdulist.close()

hdul = fits.open(fits_filename)
gc.collect()

ctx = asdf.AsdfFile()
gc.collect()

ctx.blocks.find_or_create_block_for_array(hdul[0].data, ctx)
gc.collect()

hdul.close()
gc.collect()

ctx.close()
gc.collect()

del ctx

0 comments on commit 485fdb7

Please sign in to comment.