Skip to content

Commit

Permalink
Merge pull request #15031 from meeseeksmachine/auto-backport-of-pr-14…
Browse files Browse the repository at this point in the history
…998-on-v5.3.x

Backport PR #14998 on branch v5.3.x (Fix error when a PrimaryHDU has a "GROUPS = 1" keyword)
  • Loading branch information
saimn committed Jul 6, 2023
2 parents 52a60cd + 6296908 commit d064114
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
11 changes: 1 addition & 10 deletions astropy/io/fits/hdu/base.py
Expand Up @@ -855,16 +855,7 @@ def match_header(cls, header):
"""
# The SIMPLE keyword must be in the first card
card = header.cards[0]

# The check that 'GROUPS' is missing is a bit redundant, since the
# match_header for GroupsHDU will always be called before this one.
if card.keyword == "SIMPLE":
if "GROUPS" not in header and card.value is False:
return True
else:
raise InvalidHDUException
else:
return False
return card.keyword == "SIMPLE" and card.value is False

@property
def size(self):
Expand Down
2 changes: 1 addition & 1 deletion astropy/io/fits/hdu/image.py
Expand Up @@ -1142,7 +1142,7 @@ def match_header(cls, header):
# keyword to be True/False, have to check the value
return (
card.keyword == "SIMPLE"
and ("GROUPS" not in header or header["GROUPS"] != True) # noqa: E712
and ("GROUPS" not in header or header["GROUPS"] is not True)
and card.value
)

Expand Down
8 changes: 8 additions & 0 deletions astropy/io/fits/tests/test_groups.py
Expand Up @@ -243,3 +243,11 @@ def test_group_bad_naxis(self):
assert len(hdul) == 1
assert hdul[0].header["GROUPS"]
assert hdul[0].data is None

def test_not_groups_file(self):
hdu = fits.PrimaryHDU()
hdu.header["GROUPS"] = (1, "not a groups HDU")
hdu.writeto(self.temp("not_groups.fits"))
with fits.open(self.temp("not_groups.fits")) as hdul:
assert hdul[0].header["GROUPS"] == 1
assert hdul[0].header.comments["GROUPS"] == "not a groups HDU"
2 changes: 2 additions & 0 deletions docs/changes/io.fits/14998.bugfix.rst
@@ -0,0 +1,2 @@
Fix crash when a PrimaryHDU has a GROUPS keyword with a non-boolean value (i.e.
not a random-groups HDU).

0 comments on commit d064114

Please sign in to comment.