Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit21c04e1

Browse files
gh-124969: Fix locale.nl_langinfo(locale.ALT_DIGITS) (GH-124974)
Now it returns a tuple of up to 100 strings (an empty tuple on most locales).Previously it returned the first item of that tuple or an empty string.
1 parent92760bd commit21c04e1

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

‎Doc/library/locale.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ The :mod:`locale` module defines the following exception and functions:
158158

159159
..function::nl_langinfo(option)
160160

161-
Return some locale-specific information as a string. This function is not
161+
Return some locale-specific information as a string (or a tuple for
162+
``ALT_DIGITS``). This function is not
162163
available on all systems, and the set of possible options might also vary
163164
across platforms. The possible argument values are numbers, for which
164165
symbolic constants are available in the locale module.
@@ -311,8 +312,7 @@ The :mod:`locale` module defines the following exception and functions:
311312

312313
..data::ALT_DIGITS
313314

314-
Get a representation of up to 100 values used to represent the values
315-
0 to 99.
315+
Get a tuple of up to 100 strings used to represent the values 0 to 99.
316316

317317
The function temporarily sets the ``LC_CTYPE`` locale to the locale
318318
of the category that determines the requested value (``LC_TIME``,

‎Lib/test/test__locale.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from_localeimport (setlocale,LC_ALL,LC_CTYPE,LC_NUMERIC,localeconv,Error)
1+
from_localeimport (setlocale,LC_ALL,LC_CTYPE,LC_NUMERIC,LC_TIME,localeconv,Error)
22
try:
33
from_localeimport (RADIXCHAR,THOUSEP,nl_langinfo)
44
exceptImportError:
@@ -74,6 +74,17 @@ def accept(loc):
7474
'ps_AF': ('\u066b','\u066c'),
7575
}
7676

77+
known_alt_digits= {
78+
'C': (0, {}),
79+
'en_US': (0, {}),
80+
'fa_IR': (100, {0:'\u06f0\u06f0',10:'\u06f1\u06f0',99:'\u06f9\u06f9'}),
81+
'ja_JP': (100, {0:'\u3007',10:'\u5341',99:'\u4e5d\u5341\u4e5d'}),
82+
'lzh_TW': (32, {0:'\u3007',10:'\u5341',31:'\u5345\u4e00'}),
83+
'my_MM': (100, {0:'\u1040\u1040',10:'\u1041\u1040',99:'\u1049\u1049'}),
84+
'or_IN': (100, {0:'\u0b66',10:'\u0b67\u0b66',99:'\u0b6f\u0b6f'}),
85+
'shn_MM': (100, {0:'\u1090\u1090',10:'\u1091\u1090',99:'\u1099\u1099'}),
86+
}
87+
7788
ifsys.platform=='win32':
7889
# ps_AF doesn't work on Windows: see bpo-38324 (msg361830)
7990
delknown_numerics['ps_AF']
@@ -179,6 +190,34 @@ def test_lc_numeric_basic(self):
179190
ifnottested:
180191
self.skipTest('no suitable locales')
181192

193+
@unittest.skipUnless(nl_langinfo,"nl_langinfo is not available")
194+
@unittest.skipUnless(hasattr(locale,'ALT_DIGITS'),"requires locale.ALT_DIGITS")
195+
@unittest.skipIf(
196+
support.is_emscriptenorsupport.is_wasi,
197+
"musl libc issue on Emscripten, bpo-46390"
198+
)
199+
deftest_alt_digits_nl_langinfo(self):
200+
# Test nl_langinfo(ALT_DIGITS)
201+
tested=False
202+
forloc, (count,samples)inknown_alt_digits.items():
203+
withself.subTest(locale=loc):
204+
try:
205+
setlocale(LC_TIME,loc)
206+
exceptError:
207+
self.skipTest(f'no locale{loc!r}')
208+
continue
209+
withself.subTest(locale=loc):
210+
alt_digits=nl_langinfo(locale.ALT_DIGITS)
211+
self.assertIsInstance(alt_digits,tuple)
212+
ifcountandnotalt_digitsandsys.platform=='darwin':
213+
self.skipTest(f'ALT_DIGITS is not set for locale{loc!r} on macOS')
214+
self.assertEqual(len(alt_digits),count)
215+
foriinsamples:
216+
self.assertEqual(alt_digits[i],samples[i])
217+
tested=True
218+
ifnottested:
219+
self.skipTest('no suitable locales')
220+
182221
deftest_float_parsing(self):
183222
# Bug #1391872: Test whether float parsing is okay on European
184223
# locales.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``locale.nl_langinfo(locale.ALT_DIGITS)``. Now it returns a tuple of up
2+
to 100 strings (an empty tuple on most locales). Previously it returned the
3+
first item of that tuple or an empty string.

‎Modules/_localemodule.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,35 @@ _locale_nl_langinfo_impl(PyObject *module, int item)
666666
{
667667
returnNULL;
668668
}
669-
PyObject*unicode=PyUnicode_DecodeLocale(result,NULL);
669+
PyObject*pyresult;
670+
#ifdefALT_DIGITS
671+
if (item==ALT_DIGITS) {
672+
/* The result is a sequence of up to 100 NUL-separated strings. */
673+
constchar*s=result;
674+
intcount=0;
675+
for (;count<100&&*s;count++) {
676+
s+=strlen(s)+1;
677+
}
678+
pyresult=PyTuple_New(count);
679+
if (pyresult!=NULL) {
680+
for (inti=0;i<count;i++) {
681+
PyObject*unicode=PyUnicode_DecodeLocale(result,NULL);
682+
if (unicode==NULL) {
683+
Py_CLEAR(pyresult);
684+
break;
685+
}
686+
PyTuple_SET_ITEM(pyresult,i,unicode);
687+
result+=strlen(result)+1;
688+
}
689+
}
690+
}
691+
else
692+
#endif
693+
{
694+
pyresult=PyUnicode_DecodeLocale(result,NULL);
695+
}
670696
restore_locale(oldloc);
671-
returnunicode;
697+
returnpyresult;
672698
}
673699
}
674700
PyErr_SetString(PyExc_ValueError,"unsupported langinfo constant");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp