-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
|
|
@@ -7,7 +8,7 @@ | |
| import locale | ||
| import sys | ||
| import unittest | ||
| from platform import uname | ||
| from platform import uname, libc_ver | ||
|
|
||
| from test import support | ||
|
|
||
|
|
@@ -271,6 +272,70 @@ 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', | ||
| ] | ||
| tested = False | ||
| for loc in legacy_locales: | ||
| locs = (loc.partition('.')[0] + '.UTF-8', loc) | ||
| values = [] | ||
| for l in locs: | ||
| try: | ||
| setlocale(LC_TIME, l) | ||
| 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(values[0][name], values[1][name]) | ||
| if not tested: | ||
| self.skipTest('no suitable locale pairs') | ||
|
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. Currently, when the test fails, it generates a long output which can be hard to debug (I modified the code to inject a bug on purpose): An alternative is to compare a single value rather than comparing two arrays: @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.
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']
if hasattr(locale, 'T_FMT_AMPM'):
names.append('T_FMT_AMPM')
if hasattr(locale, 'ALT_DIGITS'):
names.append('ALT_DIGITS')
items = [(name, getattr(locale, name)) for name in names]
# The same language in a Unicode and a legacy encoding.
variants = [
('ja_JP.UTF-8', 'ja_JP.EUC-JP'),
('fr_FR.UTF-8', 'fr_FR.ISO8859-1'),
('el_GR.UTF-8', 'el_GR.ISO8859-7'),
]
tested = False
for locs in variants:
values = []
for loc in locs:
try:
setlocale(LC_TIME, loc)
except Error:
continue
values.append({name: nl_langinfo(item) for name, item in items})
if len(values) < 2:
continue
tested = True
for name, item in items:
with self.subTest(locales=locs, name=name):
self.assertEqual(values[0][name], values[1][name])
if not tested:
self.skipTest('no suitable locale pairs') |
||
|
|
||
| def test_float_parsing(self): | ||
| # Bug #1391872: Test whether float parsing is okay on European | ||
| # locales. | ||
|
|
||
| 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. |
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?