Repository navigation
Handle non-ASCII characters in FITS header - #6084
Conversation
Fixes astropy#6074 and others. The non-ASCII characters are replaced by the U+FFFD REPLACEMENT CHARACTER, which will then emit verification errors but also allow to fix the problematic cards.
|
@pllim - And changelog is already here, yay ! (and with the correct PR number 😄 ) |
|
Putting milestone to 1.0.13 as it's a long standing issue, and it should be backportable. |
|
wow, that would be really nice to have! 🎉 It's a shame it has to do the encoding twice but it seems there's no "replace+warn" option in |
|
So this address the non-ASCII case, but there is also another issue (#5396) which is slightly different, the "FITS header values must contain standard printable ASCII characters" error (typically "\n" in the value). |
2fcdf8f to
7581078
Compare
|
Failure seems related but since you labeled this as WIP, maybe you expected it already: FWIW I am okay with replacing with "?" but will that violate some sort of FITS standards if this character occurs in a non-comment field (e.g., |
|
@pllim - Yes I realized after that I didn't test with Python 2, I'm looking into it. |
|
Ok, now that I have a better understanding of the difference between py2 and py3, it seems difficult to get the same behavior. For py2 the header string is not decoded, and this works because py2 works with bytes (str). For py3 the header string is decoded to ascii because the native type is now unicode. This is where it can break, and also where we replace non-ASCII characters. |
|
That's fine. We're removing Python 2 in v3 anyway, so that's okay by me. |
|
And the issue with fitsheader and Python 2 was due to the unicode_literals import 😢 |
| documentation. | ||
| """ | ||
| from __future__ import (absolute_import, division, print_function, | ||
| unicode_literals) |
There was a problem hiding this comment.
Is it okay to simply remove unicode_literals? It is used everywhere. There is even a special test marker to test both with and without unicode_literals in some cases (like table).
There was a problem hiding this comment.
It applies only to the file in which it is imported, so it's possible to have modules where it is not used. And most of the time it's useless, or it causes more issues than it solves. So here I think it's safe to remove it, and it fixes the uses of fitsheader with an invalid file.
|
Begone, invalid ASCII! |
Handle non-ASCII characters in FITS header
Handle non-ASCII characters in FITS header
Fixes #6074.
This just replace the non-ASCII characters by the U+FFFD REPLACEMENT CHARACTER (�) and emit a warning. I wonder if it is useful to do more: with this change, the header can be loaded and printed, which will emit verification errors which allow to easily spot the issue. Then it's up to the user to fix the problematic cards, and writing will crash if the cards are not fixed.
For instance, with the broken file from #2887 and this branch:
It's also possible to do more, maybe adding an option to replace these characters with a question mark, which would allow to write the fixed file without manual intervention. Or maybe this could be handled by the verification system. Thoughts ?
TODO: extract a broken header to add a unit test.