Skip to content

Commit c4f1dc6

Browse files
committed
Close on error if ignore_missing_end is set
1 parent 1483a4f commit c4f1dc6

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎astropy/io/fits/hdu/hdulist.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,10 @@ def _readfrom(cls, fileobj=None, data=None, mode=None,
10231023
# If we're trying to read only and no header units were found,
10241024
# raise an exception
10251025
if not read_one and mode in ('readonly', 'denywrite'):
1026+
# Close the file if necessary (issue #6168)
1027+
if hdulist._file.close_on_error:
1028+
hdulist._file.close()
1029+
10261030
raise IOError('Empty or corrupt FITS file')
10271031

10281032
if not lazy_load_hdus:

‎astropy/io/fits/tests/test_hdulist.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,8 @@ def test_proper_error_raised_on_non_fits_file_with_unicode(self):
918918
# Avoid a spurious pyflakes warning about an unused import
919919
# of six by adding a reason string
920920
@pytest.mark.skipif(six.PY2, reason='six.PY2')
921-
def test_no_resource_warning_raised_on_non_fits_file(self):
921+
@pytest.mark.parametrize(('ignore', ), [(False, ), (True, )])
922+
def test_no_resource_warning_raised_on_non_fits_file(self, ignore):
922923
"""
923924
Regression test for https://github.com/astropy/astropy/issues/6168
924925
@@ -933,7 +934,7 @@ def test_no_resource_warning_raised_on_non_fits_file(self):
933934
# This should raise an IOError because there is no end card.
934935
with catch_warnings(ResourceWarning) as ws:
935936
with pytest.raises(IOError):
936-
fits.open(filename)
937+
fits.open(filename, ignore_missing_end=ignore)
937938

938939
assert len(ws) == 0
939940

0 commit comments

Comments
 (0)