Skip to content

Commit 88315b3

Browse files
committed
Raise the modified IndexError only if the file was not fully read
1 parent be6ff72 commit 88315b3

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ def __getitem__(self, key):
271271
Get an HDU from the `HDUList`, indexed by number or name.
272272
"""
273273

274+
def _better_index_error(exc):
275+
# Raise a more helpful IndexError if the file was not fully read.
276+
if self._read_all:
277+
raise exc
278+
else:
279+
raise IndexError('HDU not found, possibly because the index '
280+
'is out of range, or because the file was '
281+
'closed before all HDUs were read')
282+
274283
# If the key is a slice we need to make sure the necessary HDUs
275284
# have been loaded before passing the slice on to super.
276285
if isinstance(key, slice):
@@ -301,10 +310,8 @@ def __getitem__(self, key):
301310

302311
try:
303312
hdus = super(HDUList, self).__getitem__(key)
304-
except IndexError:
305-
raise IndexError('HDU not found, possibly because the index '
306-
'is out of range, or because the file was '
307-
'closed before all HDUs were read')
313+
except IndexError as e:
314+
_better_index_error(e)
308315
else:
309316
return HDUList(hdus)
310317

@@ -314,10 +321,8 @@ def __getitem__(self, key):
314321
try:
315322
return self._try_while_unread_hdus(super(HDUList, self).__getitem__,
316323
self._positive_index_of(key))
317-
except IndexError:
318-
raise IndexError('HDU not found, possibly because the index '
319-
'is out of range, or because the file was '
320-
'closed before all HDUs were read')
324+
except IndexError as e:
325+
_better_index_error(e)
321326

322327
def __contains__(self, item):
323328
"""
@@ -1065,6 +1070,9 @@ def _read_next_hdu(self):
10651070
saved_compression_enabled = compressed.COMPRESSION_ENABLED
10661071
fileobj, data, kwargs = self._file, self._data, self._open_kwargs
10671072

1073+
if fileobj is not None and fileobj.closed:
1074+
return False
1075+
10681076
try:
10691077
self._in_read_next_hdu = True
10701078

@@ -1088,14 +1096,6 @@ def _read_next_hdu(self):
10881096
except EOFError:
10891097
self._read_all = True
10901098
return False
1091-
except ValueError:
1092-
# A ValueError can occur when trying to perform I/O
1093-
# on a closed file
1094-
if fileobj.closed:
1095-
self._read_all = True
1096-
return False
1097-
else:
1098-
raise
10991099
except IOError:
11001100
if fileobj.writeonly:
11011101
self._read_all = True

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ def test_open_3(self):
128128

129129
with pytest.raises(IndexError) as exc_info:
130130
r[6]
131-
assert str(exc_info.value) == ('HDU not found, possibly because the index '
132-
'is out of range, or because the file was '
133-
'closed before all HDUs were read')
131+
assert str(exc_info.value) == 'list index out of range'
134132

135133
# And the same with the global config item
136134
assert fits.conf.lazy_load_hdus # True by default

0 commit comments

Comments
 (0)