Skip to content

Commit e7895ca

Browse files
committed
Card: test the value type and card image before reading two config items
Reading the value of a card parsed from a header read two configuration items: Card.value read conf.strip_header_whitespace for every value, and Card._check_if_rvkc read conf.enable_record_valued_keyword_cards for every card image. Each read goes through ConfigItem.__get__, which is slow (see astropy#12504), yet only a str value is stripped and only an image containing ': ' can be a record-valued keyword card. Test the type and the image first. Where the config can change the result it is still read at that moment, so conf.set_temp works as before. Reading every value of a 412-card header of mostly numbers does 61 config reads instead of 824 and takes 1.4 ms instead of 2.4 ms. The value setter and Card.comment are not changed and still read the config each time.
1 parent 7732926 commit e7895ca

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎astropy/io/fits/card.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ def value(self):
297297
else:
298298
self._value = value = UNDEFINED
299299

300-
if conf.strip_header_whitespace and isinstance(value, str):
300+
# Test the type first, as reading the config is slow (see #12504)
301+
if isinstance(value, str) and conf.strip_header_whitespace:
301302
value = value.rstrip()
302303

303304
return value
@@ -626,6 +627,11 @@ def _check_if_rvkc(self, *args):
626627
self._check_if_rvkc('DP1.AXIS.1', 2)
627628
self._check_if_rvkc('DP1 = AXIS.1: 2')
628629
"""
630+
# A card image without ': ' cannot be a RVKC; testing for it first
631+
# avoids reading the config, which is slow (see #12504), for most cards
632+
if len(args) == 1 and ": " not in args[0]:
633+
return False
634+
629635
if not conf.enable_record_valued_keyword_cards:
630636
return False
631637

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,29 @@ def test_header_strip_whitespace(self):
23972397
assert h.cards["FOO"].image.rstrip() == "FOO = 'Bar '"
23982398
assert h.cards["QUX"].image.rstrip() == "QUX = 'Bar '"
23992399

2400+
def test_card_value_config_reads(self, monkeypatch):
2401+
"""
2402+
Reading the configuration is slow (see
2403+
https://github.com/astropy/astropy/issues/12504), so reading the
2404+
values of a parsed header should read it only for the cards it can
2405+
affect: here `fits.conf.strip_header_whitespace`, for the str value.
2406+
"""
2407+
2408+
reads = []
2409+
2410+
class CountingConf:
2411+
def __getattr__(self, name):
2412+
reads.append(name)
2413+
return getattr(fits.conf, name)
2414+
2415+
monkeypatch.setattr(fits.card, "conf", CountingConf())
2416+
2417+
h = fits.Header.fromstring(
2418+
"A = 1\nB = 2.5\nC = T\nD = 'Bar '", sep="\n"
2419+
)
2420+
assert [card.value for card in h.cards] == [1, 2.5, True, "Bar"]
2421+
assert reads == ["strip_header_whitespace"]
2422+
24002423
def test_keep_duplicate_history_in_orig_header(self):
24012424
"""
24022425
Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/156
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Reading the values of a parsed FITS header is faster. When the value of a
2+
card is read, ``strip_header_whitespace`` is now read from the configuration
3+
only if the value is a string, and ``enable_record_valued_keyword_cards`` only
4+
if the card image contains ``': '``.

0 commit comments

Comments
 (0)