Skip to content

Commit 42de8a2

Browse files
saimnastrofrog
authored andcommitted
Merge pull request #11487 from embray/issue-11486
Fix issue #11486
1 parent 1b5074a commit 42de8a2

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎astropy/io/fits/card.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ def _split(self):
807807
else:
808808
image = self.image
809809

810-
# Split cards with CONTINUE cards
810+
# Split cards with CONTINUE cards or commentary keywords with long
811+
# values
811812
if len(self._image) > self.length:
812813
values = []
813814
comments = []
@@ -817,6 +818,10 @@ def _split(self):
817818
if keyword is None:
818819
keyword = kw
819820

821+
if keyword in self._commentary_keywords:
822+
values.append(vc)
823+
continue
824+
820825
# Should match a string followed by a comment; if not it
821826
# might be an invalid Card, so we just take it verbatim
822827
m = self._strg_comment_RE.match(vc)
@@ -832,7 +837,11 @@ def _split(self):
832837
if comment:
833838
comments.append(comment.rstrip())
834839

835-
valuecomment = f"'{''.join(values)}' / {' '.join(comments)}"
840+
if keyword in self._commentary_keywords:
841+
valuecomment = ''.join(values)
842+
else:
843+
# CONTINUE card
844+
valuecomment = f"'{''.join(values)}' / {' '.join(comments)}"
836845
return keyword, valuecomment
837846

838847
if self.keyword in self._special_keywords:
@@ -1162,16 +1171,21 @@ def _itersubcards(self):
11621171
If the card image is greater than 80 characters, it should consist of a
11631172
normal card followed by one or more CONTINUE card. This method returns
11641173
the subcards that make up this logical card.
1174+
1175+
This can also support the case where a HISTORY or COMMENT card has a
1176+
long value that is stored internally as multiple concatenated card
1177+
images.
11651178
"""
11661179

11671180
ncards = len(self._image) // Card.length
11681181

11691182
for idx in range(0, Card.length * ncards, Card.length):
11701183
card = Card.fromstring(self._image[idx:idx + Card.length])
1171-
if idx > 0 and card.keyword.upper() != 'CONTINUE':
1184+
if idx > 0 and card.keyword.upper() not in self._special_keywords:
11721185
raise VerifyError(
11731186
'Long card images must have CONTINUE cards after '
1174-
'the first card.')
1187+
'the first card or have commentary keywords like '
1188+
'HISTORY or COMMENT.')
11751189

11761190
if not isinstance(card.value, str):
11771191
raise VerifyError('CONTINUE cards must have string values.')

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,30 @@ def test_newlines_in_commentary(self):
23892389
else:
23902390
c.verify('exception')
23912391

2392+
def test_long_commentary_card_appended_to_header(self):
2393+
"""
2394+
If a HISTORY or COMMENT card with a too-long value is appended to a
2395+
header with Header.append (as opposed to assigning to hdr['HISTORY']
2396+
it fails verification.
2397+
2398+
Regression test for https://github.com/astropy/astropy/issues/11486
2399+
"""
2400+
2401+
header = fits.Header()
2402+
value = 'abc' * 90
2403+
# this is what Table does when saving its history metadata key to a
2404+
# FITS file
2405+
header.append(('history', value))
2406+
assert len(header.cards) == 1
2407+
2408+
# Test Card._split() directly since this was the main problem area
2409+
key, val = header.cards[0]._split()
2410+
assert key == 'HISTORY' and val == value
2411+
2412+
# Try writing adding this header to an HDU and writing it to a file
2413+
hdu = fits.PrimaryHDU(header=header)
2414+
hdu.writeto(self.temp('test.fits'), overwrite=True)
2415+
23922416
def test_header_fromstring_bytes(self):
23932417
"""
23942418
Test reading a Header from a `bytes` string.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed regression introduced in Astropy 4.0.5 and 4.2.1 with verification of
2+
FITS headers with HISTORY or COMMENT cards with long (> 72 characters)
3+
values.

0 commit comments

Comments
 (0)