Skip to content

Commit 8a0e7f4

Browse files
committed
[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 9228efd commit 8a0e7f4

2 files changed

Lines changed: 57 additions & 25 deletions

File tree

‎astropy/io/fits/card.py‎

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

10911091
def _verify(self, option='warn'):
1092-
self._verified = True
1093-
1094-
errs = _ErrList([])
1092+
errs = []
10951093
fix_text = f'Fixed {self.keyword!r} card to meet the FITS standard.'
10961094

10971095
# Don't try to verify cards that already don't meet any recognizable
10981096
# standard
10991097
if self._invalid:
1100-
return errs
1098+
return _ErrList(errs)
11011099

11021100
# verify the equal sign position
11031101
if (self.keyword not in self._commentary_keywords and
11041102
(self._image and self._image[:9].upper() != 'HIERARCH ' and
11051103
self._image.find('=') != 8)):
1106-
errs.append(self.run_option(
1107-
option,
1104+
errs.append(dict(
11081105
err_text='Card {!r} is not FITS standard (equal sign not '
11091106
'at column 8).'.format(self.keyword),
11101107
fix_text=fix_text,
@@ -1123,8 +1120,7 @@ def _verify(self, option='warn'):
11231120
keyword = self._split()[0]
11241121
if keyword != keyword.upper():
11251122
# Keyword should be uppercase unless it's a HIERARCH card
1126-
errs.append(self.run_option(
1127-
option,
1123+
errs.append(dict(
11281124
err_text=f'Card keyword {keyword!r} is not upper case.',
11291125
fix_text=fix_text,
11301126
fix=self._fix_keyword))
@@ -1134,8 +1130,7 @@ def _verify(self, option='warn'):
11341130
keyword = keyword.split('.', 1)[0]
11351131

11361132
if not self._keywd_FSC_RE.match(keyword):
1137-
errs.append(self.run_option(
1138-
option,
1133+
errs.append(dict(
11391134
err_text=f'Illegal keyword name {keyword!r}',
11401135
fixable=False))
11411136

@@ -1145,35 +1140,39 @@ def _verify(self, option='warn'):
11451140
# For commentary keywords all that needs to be ensured is that it
11461141
# contains only printable ASCII characters
11471142
if not self._ascii_text_re.match(valuecomment):
1148-
errs.append(self.run_option(
1149-
option,
1143+
errs.append(dict(
11501144
err_text='Unprintable string {!r}; commentary cards may '
11511145
'only contain printable ASCII characters'.format(
11521146
valuecomment),
11531147
fixable=False))
11541148
else:
1155-
m = self._value_FSC_RE.match(valuecomment)
1156-
if not m:
1157-
errs.append(self.run_option(
1158-
option,
1159-
err_text='Card {!r} is not FITS standard (invalid value '
1160-
'string: {!r}).'.format(self.keyword, valuecomment),
1161-
fix_text=fix_text,
1162-
fix=self._fix_value))
1149+
if not self._valuemodified:
1150+
m = self._value_FSC_RE.match(valuecomment)
1151+
# If the value of a card was replaced before the card was ever
1152+
# even verified, the new value can be considered valid, so we
1153+
# don't bother verifying the old value. See
1154+
# https://github.com/astropy/astropy/issues/5408
1155+
if m is None:
1156+
errs.append(dict(
1157+
err_text=f'Card {self.keyword!r} is not FITS standard '
1158+
f'(invalid value string: {valuecomment!r}).',
1159+
fix_text=fix_text,
1160+
fix=self._fix_value))
11631161

11641162
# verify the comment (string), it is never fixable
11651163
m = self._value_NFSC_RE.match(valuecomment)
11661164
if m is not None:
11671165
comment = m.group('comm')
11681166
if comment is not None:
11691167
if not self._ascii_text_re.match(comment):
1170-
errs.append(self.run_option(
1171-
option,
1172-
err_text=('Unprintable string {!r}; header comments '
1173-
'may only contain printable ASCII '
1174-
'characters'.format(comment)),
1168+
errs.append(dict(
1169+
err_text=f'Unprintable string {comment!r}; header '
1170+
'comments may only contain printable '
1171+
'ASCII characters',
11751172
fixable=False))
11761173

1174+
errs = _ErrList([self.run_option(option, **err) for err in errs])
1175+
self._verified = True
11771176
return errs
11781177

11791178
def _itersubcards(self):

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,39 @@ def test_set_keyword_with_space(self):
23882388
assert hdr['KEY2'] == 4
23892389
assert hdr['KEY2 '] == 4
23902390

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

23922425
class TestRecordValuedKeywordCards(FitsTestCase):
23932426
"""

0 commit comments

Comments
 (0)