Skip to content

Commit 5f959c4

Browse files
authored
[3.6] bpo-31900: Fix localeconv() encoding for LC_NUMERIC (#4174) (#5192)
* Add _Py_GetLocaleconvNumeric() function: decode decimal_point and thousands_sep fields of localeconv() from the LC_NUMERIC encoding, rather than decoding from the LC_CTYPE encoding. * Modify locale.localeconv() and "n" formatter of str.format() (for int, float and complex to use _Py_GetLocaleconvNumeric() internally. (cherry picked from commit cb064fc)
1 parent fb8569e commit 5f959c4

File tree

8 files changed

+153
-17
lines changed

8 files changed

+153
-17
lines changed

Doc/library/locale.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ The :mod:`locale` module defines the following exception and functions:
147147
| ``CHAR_MAX`` | Nothing is specified in this locale. |
148148
+--------------+-----------------------------------------+
149149

150+
The function sets temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC``
151+
locale to decode ``decimal_point`` and ``thousands_sep`` byte strings if
152+
they are non-ASCII or longer than 1 byte, and the ``LC_NUMERIC`` locale is
153+
different than the ``LC_CTYPE`` locale. This temporary change affects other
154+
threads.
155+
156+
.. versionchanged:: 3.6.5
157+
The function now sets temporarily the ``LC_CTYPE`` locale to the
158+
``LC_NUMERIC`` locale in some cases.
159+
150160

151161
.. function:: nl_langinfo(option)
152162

Doc/library/stdtypes.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,20 @@ expression support in the :mod:`re` module).
15991599
See :ref:`formatstrings` for a description of the various formatting options
16001600
that can be specified in format strings.
16011601

1602+
.. note::
1603+
When formatting a number (:class:`int`, :class:`float`, :class:`float`
1604+
and subclasses) with the ``n`` type (ex: ``'{:n}'.format(1234)``), the
1605+
function sets temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC``
1606+
locale to decode ``decimal_point`` and ``thousands_sep`` fields of
1607+
:c:func:`localeconv` if they are non-ASCII or longer than 1 byte, and the
1608+
``LC_NUMERIC`` locale is different than the ``LC_CTYPE`` locale. This
1609+
temporary change affects other threads.
1610+
1611+
.. versionchanged:: 3.6.5
1612+
When formatting a number with the ``n`` type, the function sets
1613+
temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale in some
1614+
cases.
1615+
16021616

16031617
.. method:: str.format_map(mapping)
16041618

Doc/whatsnew/3.6.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,3 +2346,11 @@ It has been replaced by the new ``make regen-all`` target.
23462346
(Contributed by Victor Stinner in :issue:`23404`.)
23472347

23482348
.. versionchanged:: 3.6.2
2349+
2350+
2351+
Notable changes in Python 3.6.5
2352+
===============================
2353+
2354+
The :func:`locale.localeconv` function now sets temporarily the ``LC_CTYPE``
2355+
locale to the ``LC_NUMERIC`` locale in some cases.
2356+
(Contributed by Victor Stinner in :issue:`31900`.)

Include/fileutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ PyAPI_FUNC(int) _Py_get_blocking(int fd);
119119
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
120120
#endif /* !MS_WINDOWS */
121121

122+
PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
123+
PyObject **decimal_point,
124+
PyObject **thousands_sep,
125+
const char **grouping);
126+
122127
#endif /* Py_LIMITED_API */
123128

124129
#ifdef __cplusplus
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The :func:`locale.localeconv` function now sets temporarily the ``LC_CTYPE``
2+
locale to the ``LC_NUMERIC`` locale to decode ``decimal_point`` and
3+
``thousands_sep`` byte strings if they are non-ASCII or longer than 1 byte, and
4+
the ``LC_NUMERIC`` locale is different than the ``LC_CTYPE`` locale. This
5+
temporary change affects other threads.
6+
7+
Same change for the :meth:`str.format` method when formatting a number
8+
(:class:`int`, :class:`float`, :class:`float` and subclasses) with the ``n``
9+
type (ex: ``'{:n}'.format(1234)``).

Modules/_localemodule.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ PyLocale_localeconv(PyObject* self)
171171
RESULT(#i, x); \
172172
} while (0)
173173

174-
/* Numeric information */
175-
RESULT_STRING(decimal_point);
176-
RESULT_STRING(thousands_sep);
177-
x = copy_grouping(l->grouping);
178-
RESULT("grouping", x);
179-
180174
/* Monetary information */
181175
RESULT_STRING(int_curr_symbol);
182176
RESULT_STRING(currency_symbol);
@@ -195,6 +189,32 @@ PyLocale_localeconv(PyObject* self)
195189
RESULT_INT(n_sep_by_space);
196190
RESULT_INT(p_sign_posn);
197191
RESULT_INT(n_sign_posn);
192+
193+
/* Numeric information */
194+
PyObject *decimal_point, *thousands_sep;
195+
const char *grouping;
196+
if (_Py_GetLocaleconvNumeric(&decimal_point,
197+
&thousands_sep,
198+
&grouping) < 0) {
199+
goto failed;
200+
}
201+
202+
if (PyDict_SetItemString(result, "decimal_point", decimal_point) < 0) {
203+
Py_DECREF(decimal_point);
204+
Py_DECREF(thousands_sep);
205+
goto failed;
206+
}
207+
Py_DECREF(decimal_point);
208+
209+
if (PyDict_SetItemString(result, "thousands_sep", thousands_sep) < 0) {
210+
Py_DECREF(thousands_sep);
211+
goto failed;
212+
}
213+
Py_DECREF(thousands_sep);
214+
215+
x = copy_grouping(grouping);
216+
RESULT("grouping", x);
217+
198218
return result;
199219

200220
failed:

Python/fileutils.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,3 +1597,80 @@ _Py_set_blocking(int fd, int blocking)
15971597
return -1;
15981598
}
15991599
#endif
1600+
1601+
1602+
int
1603+
_Py_GetLocaleconvNumeric(PyObject **decimal_point, PyObject **thousands_sep,
1604+
const char **grouping)
1605+
{
1606+
int res = -1;
1607+
1608+
struct lconv *lc = localeconv();
1609+
1610+
int change_locale = 0;
1611+
if (decimal_point != NULL &&
1612+
(strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127))
1613+
{
1614+
change_locale = 1;
1615+
}
1616+
if (thousands_sep != NULL &&
1617+
(strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127))
1618+
{
1619+
change_locale = 1;
1620+
}
1621+
1622+
/* Keep a copy of the LC_CTYPE locale */
1623+
char *oldloc = NULL, *loc = NULL;
1624+
if (change_locale) {
1625+
oldloc = setlocale(LC_CTYPE, NULL);
1626+
if (!oldloc) {
1627+
PyErr_SetString(PyExc_RuntimeWarning, "faild to get LC_CTYPE locale");
1628+
return -1;
1629+
}
1630+
1631+
oldloc = _PyMem_Strdup(oldloc);
1632+
if (!oldloc) {
1633+
PyErr_NoMemory();
1634+
return -1;
1635+
}
1636+
1637+
loc = setlocale(LC_NUMERIC, NULL);
1638+
if (loc != NULL && strcmp(loc, oldloc) == 0) {
1639+
loc = NULL;
1640+
}
1641+
1642+
if (loc != NULL) {
1643+
/* Only set the locale temporarilty the LC_CTYPE locale
1644+
if LC_NUMERIC locale is different than LC_CTYPE locale and
1645+
decimal_point and/or thousands_sep are non-ASCII or longer than
1646+
1 byte */
1647+
setlocale(LC_CTYPE, loc);
1648+
}
1649+
}
1650+
1651+
if (decimal_point != NULL) {
1652+
*decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
1653+
if (*decimal_point == NULL) {
1654+
goto error;
1655+
}
1656+
}
1657+
if (thousands_sep != NULL) {
1658+
*thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
1659+
if (*thousands_sep == NULL) {
1660+
goto error;
1661+
}
1662+
}
1663+
1664+
if (grouping != NULL) {
1665+
*grouping = lc->grouping;
1666+
}
1667+
1668+
res = 0;
1669+
1670+
error:
1671+
if (loc != NULL) {
1672+
setlocale(LC_CTYPE, oldloc);
1673+
}
1674+
PyMem_Free(oldloc);
1675+
return res;
1676+
}

Python/formatter_unicode.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,11 @@ get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
707707
{
708708
switch (type) {
709709
case LT_CURRENT_LOCALE: {
710-
struct lconv *locale_data = localeconv();
711-
locale_info->decimal_point = PyUnicode_DecodeLocale(
712-
locale_data->decimal_point,
713-
NULL);
714-
if (locale_info->decimal_point == NULL)
710+
if (_Py_GetLocaleconvNumeric(&locale_info->decimal_point,
711+
&locale_info->thousands_sep,
712+
&locale_info->grouping) < 0) {
715713
return -1;
716-
locale_info->thousands_sep = PyUnicode_DecodeLocale(
717-
locale_data->thousands_sep,
718-
NULL);
719-
if (locale_info->thousands_sep == NULL)
720-
return -1;
721-
locale_info->grouping = locale_data->grouping;
714+
}
722715
break;
723716
}
724717
case LT_DEFAULT_LOCALE:

0 commit comments

Comments
 (0)