Skip to content

Commit

Permalink
Merge pull request #10030 from saimn/fits-output-verify
Browse files Browse the repository at this point in the history
Pass output_verify to close via fits.open
  • Loading branch information
saimn authored and bsipocz committed Mar 22, 2020
1 parent ab9481d commit 2b6cabb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -90,6 +90,9 @@ astropy.io.fits
- Fix checksum verification to process all HDUs instead of only the first one
because of the lazy loading feature. [#10012]

- Allow passing ``output_verify`` to ``.close`` when using the context manager.
[#10030]

astropy.io.registry
^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 9 additions & 1 deletion astropy/io/fits/hdu/hdulist.py
Expand Up @@ -127,6 +127,13 @@ def fitsopen(name, mode='readonly', memmap=None, save_backup=False,
back to integer values after performing floating point operations on
the data. Default is `False`.
output_verify : str
Output verification option. Must be one of ``"fix"``,
``"silentfix"``, ``"ignore"``, ``"warn"``, or
``"exception"``. May also be any combination of ``"fix"`` or
``"silentfix"`` with ``"+ignore"``, ``+warn``, or ``+exception"
(e.g. ``"fix+warn"``). See :ref:`verify` for more info.
Returns
-------
hdulist : an `HDUList` object
Expand Down Expand Up @@ -378,7 +385,8 @@ def __enter__(self):
return self

def __exit__(self, type, value, traceback):
self.close()
output_verify = self._open_kwargs.get('output_verify', 'exception')
self.close(output_verify=output_verify)

@classmethod
def fromfile(cls, fileobj, mode=None, memmap=None,
Expand Down
28 changes: 27 additions & 1 deletion astropy/io/fits/tests/test_hdulist.py
Expand Up @@ -10,7 +10,7 @@
import pytest
import numpy as np

from astropy.io.fits.verify import VerifyError
from astropy.io.fits.verify import VerifyError, VerifyWarning
from astropy.io import fits
from astropy.tests.helper import raises, catch_warnings, ignore_warnings
from astropy.utils.exceptions import AstropyUserWarning, AstropyDeprecationWarning
Expand Down Expand Up @@ -1057,3 +1057,29 @@ def test_write_hdulist_to_stream(self):
with subprocess.Popen(["cat"], stdin=subprocess.PIPE,
stdout=fout) as p:
hdulist.writeto(p.stdin)

def test_output_verify(self):
hdul = fits.HDUList([fits.PrimaryHDU()])
hdul[0].header['FOOBAR'] = 42
hdul.writeto(self.temp('test.fits'))

with open(self.temp('test.fits'), 'rb') as f:
data = f.read()
# create invalid card
data = data.replace(b'FOOBAR =', b'FOOBAR = ')
with open(self.temp('test2.fits'), 'wb') as f:
f.write(data)

with pytest.raises(VerifyError):
with fits.open(self.temp('test2.fits'), mode='update') as hdul:
hdul[0].header['MORE'] = 'here'
hdul.flush(output_verify='ignore')

with pytest.warns(VerifyWarning) as ww:
with fits.open(self.temp('test2.fits'), mode='update',
output_verify='fix+warn') as hdul:
hdul[0].header['MORE'] = 'here'
hdul.flush(output_verify='ignore')
assert len(ww) == 9
assert str(ww[1].message).startswith(
"Card 'FOOBAR ' is not FITS standard (equal sign not at column 8)")

0 comments on commit 2b6cabb

Please sign in to comment.