Skip to content

Commit af4c675

Browse files
embraynstarman
authored andcommitted
[astropy#5408] Add regression test and fix for astropy#5408
I had to rework how Card._verify works: Normally the run_option method, when passed a fix= argument, runs the fix code immediately. However, the fix code can have side-effects that modify the behavior of the later verifications, which makes reasoning about _verify very difficult. So I changed it so that it first collects a list of errors (and potentially errors to fix) and then runs the fixes collectively at the end. It would probably be better if that were the way verify() worked in general, but that is a larger refactoring job to leave for later (possibly can wait until astropy#3668).
1 parent da0982d commit af4c675

2 files changed

Lines changed: 56 additions & 25 deletions

File tree

‎astropy/io/fits/card.py‎

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,22 +1068,19 @@ def _format_long_commentary_image(self):
10681068
return ''.join(output)
10691069

10701070
def _verify(self, option='warn'):
1071-
self._verified = True
1072-
1073-
errs = _ErrList([])
1071+
errs = []
10741072
fix_text = f'Fixed {self.keyword!r} card to meet the FITS standard.'
10751073

10761074
# Don't try to verify cards that already don't meet any recognizable
10771075
# standard
10781076
if self._invalid:
1079-
return errs
1077+
return _ErrList(errs)
10801078

10811079
# verify the equal sign position
10821080
if (self.keyword not in self._commentary_keywords and
10831081
(self._image and self._image[:9].upper() != 'HIERARCH ' and
10841082
self._image.find('=') != 8)):
1085-
errs.append(self.run_option(
1086-
option,
1083+
errs.append(dict(
10871084
err_text='Card {!r} is not FITS standard (equal sign not '
10881085
'at column 8).'.format(self.keyword),
10891086
fix_text=fix_text,
@@ -1102,8 +1099,7 @@ def _verify(self, option='warn'):
11021099
keyword = self._split()[0]
11031100
if keyword != keyword.upper():
11041101
# Keyword should be uppercase unless it's a HIERARCH card
1105-
errs.append(self.run_option(
1106-
option,
1102+
errs.append(dict(
11071103
err_text=f'Card keyword {keyword!r} is not upper case.',
11081104
fix_text=fix_text,
11091105
fix=self._fix_keyword))
@@ -1113,8 +1109,7 @@ def _verify(self, option='warn'):
11131109
keyword = keyword.split('.', 1)[0]
11141110

11151111
if not self._keywd_FSC_RE.match(keyword):
1116-
errs.append(self.run_option(
1117-
option,
1112+
errs.append(dict(
11181113
err_text=f'Illegal keyword name {keyword!r}',
11191114
fixable=False))
11201115

@@ -1124,35 +1119,39 @@ def _verify(self, option='warn'):
11241119
# For commentary keywords all that needs to be ensured is that it
11251120
# contains only printable ASCII characters
11261121
if not self._ascii_text_re.match(valuecomment):
1127-
errs.append(self.run_option(
1128-
option,
1122+
errs.append(dict(
11291123
err_text='Unprintable string {!r}; commentary cards may '
11301124
'only contain printable ASCII characters'.format(
11311125
valuecomment),
11321126
fixable=False))
11331127
else:
1134-
m = self._value_FSC_RE.match(valuecomment)
1135-
if not m:
1136-
errs.append(self.run_option(
1137-
option,
1138-
err_text='Card {!r} is not FITS standard (invalid value '
1139-
'string: {!r}).'.format(self.keyword, valuecomment),
1140-
fix_text=fix_text,
1141-
fix=self._fix_value))
1128+
if not self._valuemodified:
1129+
m = self._value_FSC_RE.match(valuecomment)
1130+
# If the value of a card was replaced before the card was ever
1131+
# even verified, the new value can be considered valid, so we
1132+
# don't bother verifying the old value. See
1133+
# https://github.com/astropy/astropy/issues/5408
1134+
if m is None:
1135+
errs.append(dict(
1136+
err_text=f'Card {self.keyword!r} is not FITS standard '
1137+
f'(invalid value string: {valuecomment!r}).',
1138+
fix_text=fix_text,
1139+
fix=self._fix_value))
11421140

11431141
# verify the comment (string), it is never fixable
11441142
m = self._value_NFSC_RE.match(valuecomment)
11451143
if m is not None:
11461144
comment = m.group('comm')
11471145
if comment is not None:
11481146
if not self._ascii_text_re.match(comment):
1149-
errs.append(self.run_option(
1150-
option,
1151-
err_text=('Unprintable string {!r}; header comments '
1152-
'may only contain printable ASCII '
1153-
'characters'.format(comment)),
1147+
errs.append(dict(
1148+
err_text=f'Unprintable string {comment!r}; header '
1149+
'comments may only contain printable '
1150+
'ASCII characters',
11541151
fixable=False))
11551152

1153+
errs = _ErrList([self.run_option(option, **err) for err in errs])
1154+
self._verified = True
11561155
return errs
11571156

11581157
def _itersubcards(self):

‎astropy/io/fits/tests/test_header.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,38 @@ def test_strip(self):
23982398
hdr = hdr.copy(strip=True)
23992399
assert set(hdr) == {'HISTORY', 'FOO'}
24002400

2401+
def test_update_invalid_card(self):
2402+
"""
2403+
Regression test for https://github.com/astropy/astropy/issues/5408
2404+
2405+
Tests updating the value of a card that is malformatted (with an
2406+
invalid value literal).
2407+
2408+
This tests two ways of reproducing the problem, one working with a
2409+
Card object directly, and one when reading/writing a header containing
2410+
such an invalid card.
2411+
"""
2412+
2413+
card = fits.Card.fromstring('KW = INF / Comment')
2414+
card.value = 'FIXED'
2415+
assert tuple(card) == ('KW', 'FIXED', 'Comment')
2416+
card.verify('fix')
2417+
assert tuple(card) == ('KW', 'FIXED', 'Comment')
2418+
2419+
card = fits.Card.fromstring('KW = INF')
2420+
hdu = fits.PrimaryHDU()
2421+
# This is a loophole to write a header containing a malformatted card
2422+
card._verified = True
2423+
hdu.header.append(card)
2424+
hdu.header.tofile(self.temp('bogus.fits'))
2425+
2426+
with fits.open(self.temp('bogus.fits')) as hdul:
2427+
hdul[0].header['KW'] = -1
2428+
hdul.writeto(self.temp('bogus_fixed.fits'))
2429+
2430+
with fits.open(self.temp('bogus_fixed.fits')) as hdul:
2431+
assert hdul[0].header['KW'] == -1
2432+
24012433

24022434
class TestRecordValuedKeywordCards(FitsTestCase):
24032435
"""

0 commit comments

Comments
 (0)