Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when a PrimaryHDU has a "GROUPS = 1" keyword #14998

Merged
merged 1 commit into from Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions astropy/io/fits/hdu/base.py
Expand Up @@ -855,16 +855,7 @@
"""
# 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

Check warning on line 858 in astropy/io/fits/hdu/base.py

View check run for this annotation

Codecov / codecov/patch

astropy/io/fits/hdu/base.py#L858

Added line #L858 was not covered by tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not strictly needed since _NonstandardHDU comes after the other main extensions when finding the correct HDU class, but as the comment says this check is useless.


@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).