Skip to content

Commit b135229

Browse files
committed
Fix issue with setting value/comment to an unparsed Card
Illustration of the issue, setting a value to a Card that was not yet parsed change its _modified status though it should not. ``` In [1]: %astropy Numpy 1.16.1 Astropy 3.2.dev23834 In [2]: card = fits.Card.fromstring('FOO = 10') In [3]: card.value = 10 In [4]: card._modified Out[4]: True In [5]: card = fits.Card.fromstring('FOO = 10') In [6]: card.value Out[6]: 10 In [7]: card.value = 10 In [8]: card._modified Out[8]: False ```
1 parent 1356a2a commit b135229

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

‎astropy/io/fits/card.py‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def __init__(self, keyword=None, value=None, comment=None, **kwargs):
161161
self._keyword = None
162162
self._value = None
163163
self._comment = None
164-
164+
self._valuestring = None
165165
self._image = None
166166

167167
# This attribute is set to False when creating the card from a card
@@ -196,7 +196,6 @@ def __init__(self, keyword=None, value=None, comment=None, **kwargs):
196196
self.comment = comment
197197

198198
self._modified = False
199-
self._valuestring = None
200199
self._valuemodified = False
201200

202201
def __repr__(self):
@@ -283,8 +282,7 @@ def value(self):
283282
if self._value is not None:
284283
value = self._value
285284
elif self._valuestring is not None or self._image:
286-
self._value = self._parse_value()
287-
value = self._value
285+
value = self._value = self._parse_value()
288286
else:
289287
self._value = value = ''
290288

@@ -302,7 +300,13 @@ def value(self, value):
302300

303301
if value is None:
304302
value = ''
305-
oldvalue = self._value
303+
try:
304+
oldvalue = self.value
305+
except VerifyError:
306+
# probably a parsing error, falling back to the internal _value
307+
# which should be None. This may happen while calling _fix_value.
308+
oldvalue = self._value
309+
306310
if oldvalue is None:
307311
oldvalue = ''
308312

@@ -423,7 +427,7 @@ def comment(self):
423427
self._comment = self._parse_comment()
424428
return self._comment
425429
else:
426-
self.comment = ''
430+
self._comment = ''
427431
return ''
428432

429433
@comment.setter
@@ -442,10 +446,16 @@ def comment(self, comment):
442446
raise ValueError(
443447
'FITS header comments must contain standard printable '
444448
'ASCII characters; {!r} contains characters not '
445-
'representable in ASCII or non-printable characters.'.format(
446-
comment))
449+
'representable in ASCII or non-printable characters.'
450+
.format(comment))
451+
452+
try:
453+
oldcomment = self.comment
454+
except VerifyError:
455+
# probably a parsing error, falling back to the internal _comment
456+
# which should be None.
457+
oldcomment = self._comment
447458

448-
oldcomment = self._comment
449459
if oldcomment is None:
450460
oldcomment = ''
451461
if comment != oldcomment:

0 commit comments

Comments
 (0)