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
"output_verify" option forced to "exception" when using fits.open() in a "with" block #9682
Comments
@DBerke , since you have a patch in mind, if you don't mind, a pull request would keep the conversation going. cc @saimn and @MSeifert04 |
I agree that for consistency it would be better to be able to pass |
Thanks, I didn't know about Good question. I think giving It's a bit weird, though, in that This is the most backward-compatible solution that comes to mind for me (in that everything will work exactly the same as it has before if you don't know about the new |
Description
Astropy has different behavior when using the
fits.open()
command in the following two ways (which I expected to be equivalent):Expected behavior
In both cases, I expected Astropy to warn me about the non-compliant header card, but not raise an exception (as I don't care about the value of the card in question, and can't do anything about it since it's archival data).
Actual behavior
The expected behavior works in the first code form, but not the second; in the second case, I got a warning as expected from
flush()
, but then astropy raised an exception (astropy.io.fits.verify.VerifyError
) upon exiting thewith
block. (This also occurs when using the "ignore" option, without the warning obviously.)I first found this bug in 3.2.3, as I'd been using the
file = fits.open
...file.close()
form with explicitoutput_verify='warn'
, and changed it to awith
block, when I suddenly started getting theVerifyError
s. The bug persisted in 4.1.dev27006 when I cloned the repo.Steps to Reproduce
I don't have specific steps to reproduce, but from looking at the code in
astropy/io/fits/hud/hdulist.py
I think I know why it's happening: theHDUList.close
method takes anoutput_verify
argument, but theHDUList.__exit__
method which handles leaving awith
block only callsself.close
without any arguments. Thus, leaving awith
block essentially callsclose
with its defaultoutput_verify="exception"
argument, regardless of what the user may have used in the interim. This makes it impossible to usewith fits.open...
with a less restrictiveoutput_verify
option than 'exception'.I don't think having 'exception' be the default when using a
with
block is bad, per se (errors should never pass silently, unless explicitly silenced!), it would just be nice to be able to change it. I'm not quite sure how to do so; perhaps having an optionaloutput_verify
argument toHDUList.__init__
which could set some sort of instance-spcificself._verify_level
attribute, the value of which could be passed by__exit__
to theclose
method? Leave the default as 'exception' and the current behavior would still be the default, while also allowing the user to set whatever level of verification they want. I could try and take a stab at it myself to see if that would work.System Details
Darwin-16.7.0-x86_64-i386-64bit
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)]
Numpy 1.16.4
Scipy 1.3.0
astropy 3.2.3
The text was updated successfully, but these errors were encountered: