Skip to content

Commit

Permalink
fix has_quote detection to handle free-form string values in header c…
Browse files Browse the repository at this point in the history
…ards (#421)

* fix has_quote detection to handle free-form string values in header cards.

* add a test case for the free-form string issue

* add braces {} for readability

* add extra blank lines for pytest linter

* fix more 'flake8' complaints

* Update fitsio/fitsio_pywrap.c

* fix whitespace to match existing style

* add CHANGES.md entry

---------

Co-authored-by: Matthew R. Becker <[email protected]>
  • Loading branch information
dstndstn and beckermr authored Feb 20, 2025
1 parent 8cad13b commit 94595f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
version 1.2.6 (not yet released)
-------------

Bug Fixes

- Fix bug parsing header cards with free-form strings

version 1.2.5
-------------

Expand All @@ -9,7 +16,7 @@ New Features

Bug Fixes

- Fig bug slicing tables that have TBIT columns
- Fix bug slicing tables that have TBIT columns

version 1.2.4
-------------
Expand Down
14 changes: 12 additions & 2 deletions fitsio/fitsio_pywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4444,8 +4444,18 @@ PyFITSObject_read_header(struct PyFITSObject* self, PyObject* args) {
is_string_value=0;
}
} else {
int j;
has_equals = (card[8] == '=') ? 1 : 0;
has_quote = (card[10] == '\'') ? 1 : 0;
has_quote = 0;
// Look for 0 or more space characters followed by a quote character.
for (j=10; j<FLEN_CARD; j++) {
if (card[j] == '\'') {
has_quote = 1;
break;
} else if (card[j] != ' ') {
break;
}
}
if (has_equals && has_quote) {
is_string_value=1;
} else {
Expand Down Expand Up @@ -4511,7 +4521,7 @@ PyFITSObject_read_header(struct PyFITSObject* self, PyObject* args) {

} else {

// if its a stringwe just store it.
// if it's a string we just store it.
if (is_string_value) {
convert_to_ascii(longstr);
add_string_to_dict(dict,"value",longstr);
Expand Down
17 changes: 17 additions & 0 deletions fitsio/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
from ..hdu.base import INVALID_HDR_CHARS


def test_free_form_string():
with tempfile.TemporaryDirectory() as tmpdir:
fname = os.path.join(tmpdir, 'test.fits')
with open(fname, 'w') as f:
s = ("SIMPLE = T / Standard FITS " + # noqa
"BITPIX = 16 / number of bits per data pixel " + # noqa
"NAXIS = 0 / number of data axes " + # noqa
"EXTEND = T / File contains extensions " + # noqa
"PHOTREF = 'previous MegaCam' / Source: cum.photcat " + # noqa
"EXTRA = 7 / need another line following PHOTREF " + # noqa
"END " # noqa
)
f.write(s + ' ' * (2880-len(s)))
hdr = read_header(fname)
assert hdr['PHOTREF'] == 'previous MegaCam'


def test_add_delete_and_update_records():
# Build a FITSHDR from a few records (no need to write on disk)
# Record names have to be in upper case to match with FITSHDR.add_record
Expand Down

0 comments on commit 94595f1

Please sign in to comment.