Skip to content

Commit 18b6ba6

Browse files
mwcraigdhomeier
authored andcommitted
Handle slices passed to __getitem__ when some HDUs have not been loaded
Closes astropy#5585
1 parent 006f2c4 commit 18b6ba6

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,24 @@ def __getitem__(self, key):
276276
Get an HDU from the `HDUList`, indexed by number or name.
277277
"""
278278

279+
# If the key is a slice we need to make sure the necessary HDUs
280+
# have been loaded before passing the slice on to super.
279281
if isinstance(key, slice):
282+
max_idx = self._positive_index_of(key.stop)
283+
if max_idx == sys.maxsize:
284+
# We need all of the HDUs, so load them
285+
# and reset the maximum to the actual length.
286+
max_idx = len(self)
287+
288+
number_loaded = super(HDUList, self).__len__()
289+
if max_idx >= number_loaded:
290+
# We need more than we have, try loading up to and including
291+
# max_idx. Note we do not try to be clever about skipping HDUs
292+
# even though key.step might conceivably allow it.
293+
for i in range(number_loaded, max_idx):
294+
print(i)
295+
self._read_next_hdu()
296+
280297
hdus = super(HDUList, self).__getitem__(key)
281298
return HDUList(hdus)
282299

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,3 +808,39 @@ def test_invalid_hdu_key_in_contains(self):
808808

809809
for key in bad_keys:
810810
assert not (key in hdulist)
811+
812+
def test_iteration_of_lazy_loaded_hdulist(self):
813+
"""
814+
Regression test for https://github.com/astropy/astropy/issues/5585
815+
"""
816+
hdulist = fits.HDUList(fits.PrimaryHDU())
817+
hdulist.append(fits.ImageHDU(name='SCI'))
818+
hdulist.append(fits.ImageHDU(name='SCI'))
819+
hdulist.append(fits.ImageHDU(name='nada'))
820+
hdulist.append(fits.ImageHDU(name='SCI'))
821+
822+
filename = self.temp('many_extension.fits')
823+
hdulist.writeto(filename)
824+
f = fits.open(filename)
825+
826+
# Check that all extensions are read if f is not sliced
827+
all_exts = [ext for ext in f]
828+
assert len(all_exts) == 5
829+
830+
# Reload the file to ensure we are still lazy loading
831+
f.close()
832+
f = fits.open(filename)
833+
834+
# Try a simple slice with no conditional on the ext. This is essentially
835+
# the reported failure.
836+
all_exts_but_zero = [ext for ext in f[1:]]
837+
assert len(all_exts_but_zero) == 4
838+
839+
# Reload the file to ensure we are still lazy loading
840+
f.close()
841+
f = fits.open(filename)
842+
843+
# Check whether behavior is proper if the upper end of the slice is not
844+
# omitted.
845+
read_exts = [ext for ext in f[1:4] if ext.header['EXTNAME'] == 'SCI']
846+
assert len(read_exts) == 2

0 commit comments

Comments
 (0)