Repository navigation
Closed
Labels
Affects-devPRs and issues that do not impact an existing Astropy releasePRs and issues that do not impact an existing Astropy releaseBugio.fits
Description
@mwcraig @embray - I'm running into the following failure in glue due to the recent changes in io.fits:
def _read_next_hdu(self):
"""
Lazily load a single HDU from the fileobj or data string the `HDUList`
was opened from, unless no further HDUs are found.
Returns True if a new HDU was loaded, or False otherwise.
"""
if self._read_all:
return False
saved_compression_enabled = compressed.COMPRESSION_ENABLED
fileobj, data, kwargs = self._file, self._data, self._open_kwargs
try:
self._in_read_next_hdu = True
if ('disable_image_compression' in kwargs and
kwargs['disable_image_compression']):
compressed.COMPRESSION_ENABLED = False
# read all HDUs
try:
if fileobj is not None:
try:
# Make sure we're back to the end of the last read
# HDU
if len(self) > 0:
last = self[len(self) - 1]
if last._data_offset is not None:
offset = last._data_offset + last._data_size
fileobj.seek(offset, os.SEEK_SET)
hdu = _BaseHDU.readfrom(fileobj, **kwargs)
except EOFError:
self._read_all = True
return False
except ValueError:
# A ValueError can occur when trying to perform I/O
# on a closed file
if fileobj.closed:
self._read_all = True
return False
else:
raise
except IOError:
if fileobj.writeonly:
self._read_all = True
return False
else:
raise
else:
if not data:
self._read_all = True
return False
hdu = _BaseHDU.fromstring(data, **kwargs)
self._data = data[hdu._data_offset + hdu._data_size:]
super(HDUList, self).append(hdu)
if len(self) == 1:
# Check for an extension HDU and update the EXTEND
# keyword of the primary HDU accordingly
self.update_extend()
hdu._new = False
if 'checksum' in kwargs:
hdu._output_checksum = kwargs['checksum']
# check in the case there is extra space after the last HDU or
# corrupted HDU
except (VerifyError, ValueError) as exc:
warnings.warn(
'Error validating header for HDU #{} (note: Astropy '
'uses zero-based indexing).\n{}\n'
'There may be extra bytes after the last HDU or the '
'file is corrupted.'.format(
> len(hdulist), indent(str(exc))), VerifyWarning)
E NameError: global name 'hdulist' is not defined
../../../miniconda/envs/test/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.py:1150: NameError
============================ pytest-warning summary ============================Metadata
Metadata
Assignees
Labels
Affects-devPRs and issues that do not impact an existing Astropy releasePRs and issues that do not impact an existing Astropy releaseBugio.fits
Activity
[-]Regression in glue due to undefined variable[/-][+]Regression in io.fits due to undefined variable[/+]mwcraig commentedon Dec 12, 2016
I can fix up later today as part of #5589 or someone could push to that branch. Looks like an easy fix, but a regression test should be added for it too.
Or a separate PR, of course.
mwcraig commentedon Dec 12, 2016
@astrofrog -- do you have a short way to trigger this error? Ideally I'd add a test for it (the fix is easy) but I'm not having nay luck hitting that chunk of the code.
astrofrog commentedon Dec 12, 2016
@mwcraig - yep, any file that is not a FITS file that has unicode:
mwcraig commentedon Dec 12, 2016
Thanks! Never would have come up with that...
embray commentedon Dec 13, 2016
Definitely an under-tested corner case :P
astrofrog commentedon Dec 13, 2016
@embray @mwcraig - just for some background, I use
fits.open(filename)as a way to determine if a file is a FITS file. There might be a better way?mwcraig commentedon Dec 13, 2016
There is
astropy.io.fits.connect.is_fitsatastropy/astropy/io/fits/connect.py
Line 50 in a2d0fe7