Skip to content

Commit ba283e7

Browse files
authored
Merge pull request #11487 from embray/issue-11486
Fix issue #11486
2 parents d41e5e4 + ccf6717 commit ba283e7

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
@@ -806,7 +806,8 @@ def _split(self):
806806
else:
807807
image = self.image
808808

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

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

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

837846
if self.keyword in self._special_keywords:
@@ -1159,16 +1168,21 @@ def _itersubcards(self):
11591168
If the card image is greater than 80 characters, it should consist of a
11601169
normal card followed by one or more CONTINUE card. This method returns
11611170
the subcards that make up this logical card.
1171+
1172+
This can also support the case where a HISTORY or COMMENT card has a
1173+
long value that is stored internally as multiple concatenated card
1174+
images.
11621175
"""
11631176

11641177
ncards = len(self._image) // Card.length
11651178

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

11731187
if not isinstance(card.value, str):
11741188
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
@@ -2375,6 +2375,30 @@ def test_newlines_in_commentary(self):
23752375
else:
23762376
c.verify('exception')
23772377

2378+
def test_long_commentary_card_appended_to_header(self):
2379+
"""
2380+
If a HISTORY or COMMENT card with a too-long value is appended to a
2381+
header with Header.append (as opposed to assigning to hdr['HISTORY']
2382+
it fails verification.
2383+
2384+
Regression test for https://github.com/astropy/astropy/issues/11486
2385+
"""
2386+
2387+
header = fits.Header()
2388+
value = 'abc' * 90
2389+
# this is what Table does when saving its history metadata key to a
2390+
# FITS file
2391+
header.append(('history', value))
2392+
assert len(header.cards) == 1
2393+
2394+
# Test Card._split() directly since this was the main problem area
2395+
key, val = header.cards[0]._split()
2396+
assert key == 'HISTORY' and val == value
2397+
2398+
# Try writing adding this header to an HDU and writing it to a file
2399+
hdu = fits.PrimaryHDU(header=header)
2400+
hdu.writeto(self.temp('test.fits'), overwrite=True)
2401+
23782402
def test_header_fromstring_bytes(self):
23792403
"""
23802404
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)