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 leap second update when using a non english locale #11062
Conversation
How interesting! There should be a change log. Not sure about a test, I'll let @mhvk decide. |
Hmm, this is not yet done... Putting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, sorry to have caused what must have been a hard bug to track down! I wish one could just tell strptime
to use a given locale, but for some reason that is not possible.
I think we need to be a bit careful about the solution (though perhaps the worry about threading problems is overblown in this case...)
.github/workflows/ci_workflows.yml
Outdated
@@ -70,7 +70,7 @@ jobs: | |||
python-version: ${{ matrix.python }} | |||
- name: Install language-pack-de and tzdata | |||
if: startsWith(matrix.os, 'ubuntu') | |||
run: sudo apt-get install language-pack-de tzdata | |||
run: sudo apt-get install language-pack-de language-pack-fr tzdata |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might it make sense to use German for the tests so that we don't have to install another language pack (they're fairly large so this will slow down things).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another test using it (
astropy/astropy/wcs/tests/test_wcsprm.py
Lines 865 to 876 in a68f456
def test_locale(): | |
try: | |
with _set_locale('fr_FR'): | |
header = get_pkg_data_contents('data/locale.hdr', | |
encoding='binary') | |
with pytest.warns(FITSFixedWarning): | |
w = _wcs.Wcsprm(header) | |
assert re.search("[0-9]+,[0-9]*", w.to_header()) is None | |
except locale.Error: | |
pytest.xfail( | |
"Can't set to 'fr_FR' locale, perhaps because it is not installed " | |
"on this system") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can get rid of german instead!
astropy/utils/iers/iers.py
Outdated
scale='tai', format='iso', | ||
out_subfmt='date') | ||
try: | ||
locale.setlocale(locale.LC_TIME, 'C') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, locale
is quite explicit about not being thread safe. There is a suggested solution at https://stackoverflow.com/questions/18593661/how-do-i-strftime-a-date-object-in-a-different-locale, though I wonder if we are not better off just parsing it ourselves (as also suggested in various places). I think with a bit smarter regex this may just be possible........
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, the thing is that when using _set_locale
here the tests are stuck in a deadlock...
Maybe another soution to parse the date would be better:
If, when coding a module for general use, you need a locale independent version of an operation that is affected by the locale (such as certain formats used with time.strftime()), you will have to find a way to do it without using the standard library routine.
https://docs.python.org/3/library/locale.html#background-details-hints-tips-and-caveats
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that just changing the regex
to return the three elements and then doing a simply lookup on the first three characters of the month should do it:
astropy/astropy/utils/iers/iers.py
Line 876 in a68f456
_re_expires = re.compile(r'^#.*File expires on[:\s]+(\d+\s\w+\s\d+)\s*$') |
12d58c9
to
5e32975
Compare
day, month, year = match.groups()[0].split() | ||
month_nb = MONTH_ABBR.index(month[:3]) + 1 | ||
expires = Time(f'{year}-{month_nb:02d}-{day}', | ||
scale='tai', out_subfmt='date') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhvk - Something like this ? 🔝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
About locale tests, the one in
|
With the PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good! Does need a changelog entry... And maybe squash the commits?
- A locale test in test_wcsprm already use fr_FR and was not run - The tests in test_read and test_misc were not run on the CI, probably because of the missing encoding So instead of installing two locales in the CI environment, migrate all test to use fr_FR.
dcaf0ec
to
15bd061
Compare
@mhvk - Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, merging...
Fix leap second update when using a non english locale
Fix leap second update when using a non english locale
I'm working on a documentation, and for every build I see the leap second update with an expiration warning:
I already saw this in the past, so this is not new, but I never took the time to investigate... I can reproduce with various documentations (1, 2, 3), but the weird thing is that there is no warning when importing directly astropy.
Putting some prints in
LeapSeconds.auto_open
gives this for a normal import:which is fine. And this for a Sphinx build:
So when running with Sphinx, there is a parsing error with the
Leap_Second.dat
file, which lead to this:%B
is the Locale's full month name, but in both case my locale iffr_FR
.Diving deeper leads to _strptime.py where
_getlang
returnsNone
in one case andfr_FR
in the other, the latter resulting in a regex with French month names 🎉And I guess Sphinx is setting
LC_TIME
to the default locale, which triggers the error in_strptime.py
.