Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass output_verify to close via fits.open #10030

Merged
merged 1 commit into from Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -394,6 +394,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)")