-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-152905: Decode LC_TIME items in nl_langinfo() from glibc wide data #152911
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
Merged
serhiy-storchaka
merged 6 commits into
python:main
from
serhiy-storchaka:gh-152905-nl-langinfo-wide
Jul 9, 2026
+152
−28
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e03cae6
gh-152905: Decode LC_TIME items in nl_langinfo() from glibc wide data
serhiy-storchaka 3311532
gh-152905: Skip the new nl_langinfo test on non-glibc
serhiy-storchaka 85d729d
gh-152905: Address review of nl_langinfo wide LC_TIME decoding
serhiy-storchaka a27263d
Apply suggestion.
serhiy-storchaka 575cf43
Merge branch 'main' into gh-152905-nl-langinfo-wide
serhiy-storchaka d338d0b
gh-152905: Tolerate equivalent-space substitution in the encoding test
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import _locale | ||
| from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, LC_TIME, localeconv, Error) | ||
| try: | ||
| from _locale import (RADIXCHAR, THOUSEP, nl_langinfo) | ||
|
|
@@ -6,8 +7,9 @@ | |
|
|
||
| import locale | ||
| import sys | ||
| import unicodedata | ||
| import unittest | ||
| from platform import uname | ||
| from platform import uname, libc_ver | ||
|
|
||
| from test import support | ||
|
|
||
|
|
@@ -271,6 +273,79 @@ def test_era_nl_langinfo(self): | |
| if not tested: | ||
| self.skipTest('no suitable locales') | ||
|
|
||
| @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") | ||
| @unittest.skipUnless(libc_ver()[0] == 'glibc', | ||
| "wide nl_langinfo variants are glibc-specific") | ||
| def test_nl_langinfo_encoding_independent(self): | ||
| # gh-152905: The LC_TIME text items are decoded independently of the | ||
| # LC_CTYPE encoding (on glibc via the wide nl_langinfo variants), so | ||
| # the same locale in different encodings yields identical strings. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please mention that ERA has no wide character variant and so is not test. |
||
| # ERA has no wide variant and is not tested. | ||
| self.addCleanup(setlocale, LC_TIME, setlocale(LC_TIME)) | ||
|
|
||
| names = [f'MON_{i}' for i in range(1, 13)] | ||
| names += [f'ABMON_{i}' for i in range(1, 13)] | ||
| names += [f'DAY_{i}' for i in range(1, 8)] | ||
| names += [f'ABDAY_{i}' for i in range(1, 8)] | ||
| names += ['AM_STR', 'PM_STR', 'D_T_FMT', 'D_FMT', 'T_FMT'] | ||
| names += [name for name in ('T_FMT_AMPM', 'ERA_D_FMT', 'ERA_D_T_FMT', | ||
| 'ERA_T_FMT', 'ALT_DIGITS', '_DATE_FMT') | ||
| if hasattr(_locale, name)] | ||
| items = [(name, getattr(_locale, name)) for name in names] | ||
|
|
||
| # Legacy (non-UTF-8) locales, compared against their UTF-8 variant. | ||
| legacy_locales = [ | ||
| 'en_US.ISO8859-1', | ||
| 'es_ES.ISO8859-1', | ||
| 'fr_FR.ISO8859-1', | ||
| 'de_DE.ISO8859-1', | ||
| 'pl_PL.ISO8859-2', | ||
| 'mt_MT.ISO8859-3', | ||
| 'ar_SA.ISO8859-6', | ||
| 'el_GR.ISO8859-7', | ||
| 'he_IL.ISO8859-8', | ||
| 'tr_TR.ISO8859-9', | ||
| 'lt_LT.ISO8859-13', | ||
| 'cy_GB.ISO8859-14', | ||
| 'et_EE.ISO8859-15', | ||
| 'uk_UA.KOI8-U', | ||
| 'bg_BG.CP1251', | ||
| 'ja_JP.EUC-JP', | ||
| 'ko_KR.EUC-KR', | ||
| 'zh_CN.GB2312', | ||
| 'zh_TW.BIG5', | ||
| 'th_TH.TIS-620', | ||
| ] | ||
|
|
||
| # An 8-bit locale substitutes an equivalent for a space it cannot | ||
| # encode (e.g. es_ES has U+202F in UTF-8 but U+00A0 in ISO-8859-1), | ||
| # so fold spaces before comparing. | ||
| def fold_spaces(s): | ||
| return ''.join(' ' if unicodedata.category(c) == 'Zs' else c | ||
| for c in s) | ||
|
|
||
| tested = False | ||
| for legacy_locale in legacy_locales: | ||
| locs = (legacy_locale.partition('.')[0] + '.UTF-8', legacy_locale) | ||
| values = [] | ||
| for loc in locs: | ||
| try: | ||
| setlocale(LC_TIME, loc) | ||
| except Error: | ||
| break | ||
| values.append({name: nl_langinfo(item) for name, item in items}) | ||
| if len(values) < 2: | ||
| continue | ||
| tested = True | ||
|
|
||
| # The result must not depend on the locale encoding. | ||
| for name, item in items: | ||
| with self.subTest(locales=locs, name=name): | ||
| self.assertEqual(fold_spaces(values[0][name]), | ||
| fold_spaces(values[1][name])) | ||
| if not tested: | ||
| self.skipTest('no suitable locale pairs') | ||
|
serhiy-storchaka marked this conversation as resolved.
|
||
|
|
||
| def test_float_parsing(self): | ||
| # Bug #1391872: Test whether float parsing is okay on European | ||
| # locales. | ||
|
|
||
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-07-01-23-17-27.gh-issue-152905.wLnGqR.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| On glibc, :func:`locale.nl_langinfo` now decodes the ``LC_TIME`` items (such | ||
| as the month and day names) using the wide locale data, so the result no | ||
| longer depends on the ``LC_CTYPE`` encoding. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Except of
ERA, no?