Skip to content

Commit 97ed17c

Browse files
pythongh-124969: Make locale.nl_langinfo(locale.ALT_DIGITS) returning a string again
This is a follow up of pythonGH-124974. Only Glibc needed a fix. Now the returned value is a string consisting of semicolon-separated symbols on all Posix platforms.
1 parent ed24702 commit 97ed17c

4 files changed

Lines changed: 47 additions & 27 deletions

File tree

Doc/library/locale.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ 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 (or a tuple for
162-
``ALT_DIGITS``). This function is not
161+
Return some locale-specific information as a string. This function is not
163162
available on all systems, and the set of possible options might also vary
164163
across platforms. The possible argument values are numbers, for which
165164
symbolic constants are available in the locale module.
@@ -312,7 +311,9 @@ The :mod:`locale` module defines the following exception and functions:
312311

313312
.. data:: ALT_DIGITS
314313

315-
Get a tuple of up to 100 strings used to represent the values 0 to 99.
314+
Get a string consisting of up to 100 semicolon-separated symbols used
315+
to represent the values 0 to 99 in a locale-specific way.
316+
In most locales this is an empty string.
316317

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

Lib/test/test__locale.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
2727
'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US',
2828
'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
29-
'ru_RU.KOI8-R', 'ko_KR.eucKR']
29+
'ru_RU.KOI8-R', 'ko_KR.eucKR',
30+
'ja_JP', 'lzh_TW', 'my_MM', 'or_IN', 'shn_MM',
31+
'ar_AE', 'bn_IN', 'ks_IN', 'mr_IN', 'th_TH',
32+
]
3033

3134
def setUpModule():
3235
global candidate_locales
@@ -78,7 +81,7 @@ def accept(loc):
7881
'C': (0, {}),
7982
'en_US': (0, {}),
8083
'fa_IR': (100, {0: '\u06f0\u06f0', 10: '\u06f1\u06f0', 99: '\u06f9\u06f9'}),
81-
'ja_JP': (100, {0: '\u3007', 10: '\u5341', 99: '\u4e5d\u5341\u4e5d'}),
84+
'ja_JP': (100, {1: '\u4e00', 10: '\u5341', 99: '\u4e5d\u5341\u4e5d'}),
8285
'lzh_TW': (32, {0: '\u3007', 10: '\u5341', 31: '\u5345\u4e00'}),
8386
'my_MM': (100, {0: '\u1040\u1040', 10: '\u1041\u1040', 99: '\u1049\u1049'}),
8487
'or_IN': (100, {0: '\u0b66', 10: '\u0b67\u0b66', 99: '\u0b6f\u0b6f'}),
@@ -199,21 +202,27 @@ def test_lc_numeric_basic(self):
199202
def test_alt_digits_nl_langinfo(self):
200203
# Test nl_langinfo(ALT_DIGITS)
201204
tested = False
202-
for loc, (count, samples) in known_alt_digits.items():
205+
for loc in candidate_locales:
203206
with self.subTest(locale=loc):
204207
try:
205208
setlocale(LC_TIME, loc)
206209
except Error:
207210
self.skipTest(f'no locale {loc!r}')
208211
continue
212+
209213
with self.subTest(locale=loc):
210214
alt_digits = nl_langinfo(locale.ALT_DIGITS)
211-
self.assertIsInstance(alt_digits, tuple)
212-
if count and not alt_digits and support.is_apple:
213-
self.skipTest(f'ALT_DIGITS is not set for locale {loc!r} on Apple platforms')
214-
self.assertEqual(len(alt_digits), count)
215-
for i in samples:
216-
self.assertEqual(alt_digits[i], samples[i])
215+
self.assertIsInstance(alt_digits, str)
216+
alt_digits = alt_digits.split(';') if alt_digits else []
217+
if alt_digits:
218+
self.assertGreaterEqual(len(alt_digits), 10, alt_digits)
219+
if loc in known_alt_digits:
220+
count, samples = known_alt_digits[loc]
221+
if count and not alt_digits:
222+
self.skipTest(f'ALT_DIGITS is not set for locale {loc!r} on this platform')
223+
self.assertEqual(len(alt_digits), count, alt_digits)
224+
for i in samples:
225+
self.assertEqual(alt_digits[i], samples[i])
217226
tested = True
218227
if not tested:
219228
self.skipTest('no suitable locales')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``locale.nl_langinfo(locale.ALT_DIGITS)`` now returns a string again. The
2+
returned value consists of up to 100 semicolon-separated symbols.

Modules/_localemodule.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -667,28 +667,36 @@ _locale_nl_langinfo_impl(PyObject *module, int item)
667667
return NULL;
668668
}
669669
PyObject *pyresult;
670+
#ifdef __GLIBC__
670671
#ifdef ALT_DIGITS
671-
if (item == ALT_DIGITS) {
672-
/* The result is a sequence of up to 100 NUL-separated strings. */
673-
const char *s = result;
672+
if (item == ALT_DIGITS && *result) {
673+
/* According to the POSIX specification the result must be
674+
* a sequence of up to 100 semicolon-separated strings.
675+
* But in Glibc they are NUL-separated. */
676+
Py_ssize_t i = 0;
674677
int count = 0;
675-
for (; count < 100 && *s; count++) {
676-
s += strlen(s) + 1;
678+
for (; count < 100 && result[i]; count++) {
679+
i += strlen(result + i) + 1;
677680
}
678-
pyresult = PyTuple_New(count);
679-
if (pyresult != NULL) {
680-
for (int i = 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;
681+
char *buf = PyMem_Malloc(i);
682+
if (buf == NULL) {
683+
PyErr_NoMemory();
684+
pyresult = NULL;
685+
}
686+
else {
687+
memcpy(buf, result, i);
688+
/* Replace all NULs with semicolons. */
689+
i = 0;
690+
while (--count) {
691+
i += strlen(buf + i);
692+
buf[i++] = ';';
688693
}
694+
pyresult = PyUnicode_DecodeLocale(buf, NULL);
695+
PyMem_Free(buf);
689696
}
690697
}
691698
else
699+
#endif
692700
#endif
693701
{
694702
pyresult = PyUnicode_DecodeLocale(result, NULL);

0 commit comments

Comments
 (0)