'Regression test for https://github.com/astropy/astropy/issues/5585'

def test_iteration_of_lazy_loaded_hdulist(self):

    hdulist = fits.HDUList(fits.PrimaryHDU())
    hdulist.append(fits.ImageHDU(name='SCI'))
    hdulist.append(fits.ImageHDU(name='SCI'))
    hdulist.append(fits.ImageHDU(name='nada'))
    hdulist.append(fits.ImageHDU(name='SCI'))
    filename = self.temp('many_extension.fits')
    hdulist.writeto(filename)
    f = fits.open(filename)
    all_exts = [ext for ext in f]
    assert (len(all_exts) == 5)
    f.close()
    f = fits.open(filename)
    all_exts_but_zero = [ext for ext in f[1:]]
    assert (len(all_exts_but_zero) == 4)
    f.close()
    f = fits.open(filename)
    read_exts = [ext for ext in f[1:4] if (ext.header['EXTNAME'] == 'SCI')]
    assert (len(read_exts) == 2)
