churchyard / rpms / python3

Forked from rpms/python3 3 years ago
Clone

Blame 00273-fix-localeconv-encoding-for-LC_NUMERIC.patch

40bb48b
diff --git a/Doc/library/locale.rst b/Doc/library/locale.rst
40bb48b
index b04442bc162..9a0c570533a 100644
40bb48b
--- a/Doc/library/locale.rst
40bb48b
+++ b/Doc/library/locale.rst
40bb48b
@@ -147,6 +147,16 @@ The :mod:`locale` module defines the following exception and functions:
40bb48b
    | ``CHAR_MAX`` | Nothing is specified in this locale.    |
40bb48b
    +--------------+-----------------------------------------+
40bb48b
 
40bb48b
+   The function sets temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC``
40bb48b
+   locale to decode ``decimal_point`` and ``thousands_sep`` byte strings if
40bb48b
+   they are non-ASCII or longer than 1 byte, and the ``LC_NUMERIC`` locale is
40bb48b
+   different than the ``LC_CTYPE`` locale. This temporary change affects other
40bb48b
+   threads.
40bb48b
+
40bb48b
+   .. versionchanged:: 3.6.5
40bb48b
+      The function now sets temporarily the ``LC_CTYPE`` locale to the
40bb48b
+      ``LC_NUMERIC`` locale in some cases.
40bb48b
+
40bb48b
 
40bb48b
 .. function:: nl_langinfo(option)
40bb48b
 
40bb48b
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
40bb48b
index 196a4c00056..d8a1647e8b5 100644
40bb48b
--- a/Doc/library/stdtypes.rst
40bb48b
+++ b/Doc/library/stdtypes.rst
40bb48b
@@ -1599,6 +1599,20 @@ expression support in the :mod:`re` module).
40bb48b
    See :ref:`formatstrings` for a description of the various formatting options
40bb48b
    that can be specified in format strings.
40bb48b
 
40bb48b
+   .. note::
40bb48b
+      When formatting a number (:class:`int`, :class:`float`, :class:`float`
40bb48b
+      and subclasses) with the ``n`` type (ex: ``'{:n}'.format(1234)``), the
40bb48b
+      function sets temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC``
40bb48b
+      locale to decode ``decimal_point`` and ``thousands_sep`` fields of
40bb48b
+      :c:func:`localeconv` if they are non-ASCII or longer than 1 byte, and the
40bb48b
+      ``LC_NUMERIC`` locale is different than the ``LC_CTYPE`` locale. This
40bb48b
+      temporary change affects other threads.
40bb48b
+
40bb48b
+   .. versionchanged:: 3.6.5
40bb48b
+      When formatting a number with the ``n`` type, the function sets
40bb48b
+      temporarily the ``LC_CTYPE`` locale to the ``LC_NUMERIC`` locale in some
40bb48b
+      cases.
40bb48b
+
40bb48b
 
40bb48b
 .. method:: str.format_map(mapping)
40bb48b
 
40bb48b
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
40bb48b
index 847b50140a6..f83508c9250 100644
40bb48b
--- a/Doc/whatsnew/3.6.rst
40bb48b
+++ b/Doc/whatsnew/3.6.rst
40bb48b
@@ -2346,3 +2346,11 @@ It has been replaced by the new ``make regen-all`` target.
40bb48b
 (Contributed by Victor Stinner in :issue:`23404`.)
40bb48b
 
40bb48b
 .. versionchanged:: 3.6.2
40bb48b
+
40bb48b
+
40bb48b
+Notable changes in Python 3.6.5
40bb48b
+===============================
40bb48b
+
40bb48b
+The :func:`locale.localeconv` function now sets temporarily the ``LC_CTYPE``
40bb48b
+locale to the ``LC_NUMERIC`` locale in some cases.
40bb48b
+(Contributed by Victor Stinner in :issue:`31900`.)
40bb48b
diff --git a/Include/fileutils.h b/Include/fileutils.h
40bb48b
index 900c70faad7..875715df97a 100644
40bb48b
--- a/Include/fileutils.h
40bb48b
+++ b/Include/fileutils.h
40bb48b
@@ -119,6 +119,11 @@ PyAPI_FUNC(int) _Py_get_blocking(int fd);
40bb48b
 PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
40bb48b
 #endif   /* !MS_WINDOWS */
40bb48b
 
40bb48b
+PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
40bb48b
+    PyObject **decimal_point,
40bb48b
+    PyObject **thousands_sep,
40bb48b
+    const char **grouping);
40bb48b
+
40bb48b
 #endif   /* Py_LIMITED_API */
40bb48b
 
40bb48b
 #ifdef __cplusplus
40bb48b
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
40bb48b
index 71c9146ccb8..95b370b1ad0 100644
40bb48b
--- a/Modules/_localemodule.c
40bb48b
+++ b/Modules/_localemodule.c
40bb48b
@@ -171,12 +171,6 @@ PyLocale_localeconv(PyObject* self)
40bb48b
         RESULT(#i, x); \
40bb48b
     } while (0)
40bb48b
 
40bb48b
-    /* Numeric information */
40bb48b
-    RESULT_STRING(decimal_point);
40bb48b
-    RESULT_STRING(thousands_sep);
40bb48b
-    x = copy_grouping(l->grouping);
40bb48b
-    RESULT("grouping", x);
40bb48b
-
40bb48b
     /* Monetary information */
40bb48b
     RESULT_STRING(int_curr_symbol);
40bb48b
     RESULT_STRING(currency_symbol);
40bb48b
@@ -195,6 +189,32 @@ PyLocale_localeconv(PyObject* self)
40bb48b
     RESULT_INT(n_sep_by_space);
40bb48b
     RESULT_INT(p_sign_posn);
40bb48b
     RESULT_INT(n_sign_posn);
40bb48b
+
40bb48b
+    /* Numeric information */
40bb48b
+    PyObject *decimal_point, *thousands_sep;
40bb48b
+    const char *grouping;
40bb48b
+    if (_Py_GetLocaleconvNumeric(&decimal_point,
40bb48b
+                                 &thousands_sep,
40bb48b
+                                 &grouping) < 0) {
40bb48b
+        goto failed;
40bb48b
+    }
40bb48b
+
40bb48b
+    if (PyDict_SetItemString(result, "decimal_point", decimal_point) < 0) {
40bb48b
+        Py_DECREF(decimal_point);
40bb48b
+        Py_DECREF(thousands_sep);
40bb48b
+        goto failed;
40bb48b
+    }
40bb48b
+    Py_DECREF(decimal_point);
40bb48b
+
40bb48b
+    if (PyDict_SetItemString(result, "thousands_sep", thousands_sep) < 0) {
40bb48b
+        Py_DECREF(thousands_sep);
40bb48b
+        goto failed;
40bb48b
+    }
40bb48b
+    Py_DECREF(thousands_sep);
40bb48b
+
40bb48b
+    x = copy_grouping(grouping);
40bb48b
+    RESULT("grouping", x);
40bb48b
+
40bb48b
     return result;
40bb48b
 
40bb48b
   failed:
40bb48b
diff --git a/Python/fileutils.c b/Python/fileutils.c
40bb48b
index 97505e5bc6d..14dd81b03f0 100644
40bb48b
--- a/Python/fileutils.c
40bb48b
+++ b/Python/fileutils.c
40bb48b
@@ -1597,3 +1597,80 @@ _Py_set_blocking(int fd, int blocking)
40bb48b
     return -1;
40bb48b
 }
40bb48b
 #endif
40bb48b
+
40bb48b
+
40bb48b
+int
40bb48b
+_Py_GetLocaleconvNumeric(PyObject **decimal_point, PyObject **thousands_sep,
40bb48b
+                         const char **grouping)
40bb48b
+{
40bb48b
+    int res = -1;
40bb48b
+
40bb48b
+    struct lconv *lc = localeconv();
40bb48b
+
40bb48b
+    int change_locale = 0;
40bb48b
+    if (decimal_point != NULL &&
40bb48b
+        (strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127))
40bb48b
+    {
40bb48b
+        change_locale = 1;
40bb48b
+    }
40bb48b
+    if (thousands_sep != NULL &&
40bb48b
+        (strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127))
40bb48b
+    {
40bb48b
+        change_locale = 1;
40bb48b
+    }
40bb48b
+
40bb48b
+    /* Keep a copy of the LC_CTYPE locale */
40bb48b
+    char *oldloc = NULL, *loc = NULL;
40bb48b
+    if (change_locale) {
40bb48b
+        oldloc = setlocale(LC_CTYPE, NULL);
40bb48b
+        if (!oldloc) {
40bb48b
+            PyErr_SetString(PyExc_RuntimeWarning, "faild to get LC_CTYPE locale");
40bb48b
+            return -1;
40bb48b
+        }
40bb48b
+
40bb48b
+        oldloc = _PyMem_Strdup(oldloc);
40bb48b
+        if (!oldloc) {
40bb48b
+            PyErr_NoMemory();
40bb48b
+            return -1;
40bb48b
+        }
40bb48b
+
40bb48b
+        loc = setlocale(LC_NUMERIC, NULL);
40bb48b
+        if (loc != NULL && strcmp(loc, oldloc) == 0) {
40bb48b
+            loc = NULL;
40bb48b
+        }
40bb48b
+
40bb48b
+        if (loc != NULL) {
40bb48b
+            /* Only set the locale temporarilty the LC_CTYPE locale
40bb48b
+               if LC_NUMERIC locale is different than LC_CTYPE locale and
40bb48b
+               decimal_point and/or thousands_sep are non-ASCII or longer than
40bb48b
+               1 byte */
40bb48b
+            setlocale(LC_CTYPE, loc);
40bb48b
+        }
40bb48b
+    }
40bb48b
+
40bb48b
+    if (decimal_point != NULL) {
40bb48b
+        *decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
40bb48b
+        if (*decimal_point == NULL) {
40bb48b
+            goto error;
40bb48b
+        }
40bb48b
+    }
40bb48b
+    if (thousands_sep != NULL) {
40bb48b
+        *thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
40bb48b
+        if (*thousands_sep == NULL) {
40bb48b
+            goto error;
40bb48b
+        }
40bb48b
+    }
40bb48b
+
40bb48b
+    if (grouping != NULL) {
40bb48b
+        *grouping = lc->grouping;
40bb48b
+    }
40bb48b
+
40bb48b
+    res = 0;
40bb48b
+
40bb48b
+error:
40bb48b
+    if (loc != NULL) {
40bb48b
+        setlocale(LC_CTYPE, oldloc);
40bb48b
+    }
40bb48b
+    PyMem_Free(oldloc);
40bb48b
+    return res;
40bb48b
+}
40bb48b
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
40bb48b
index d2be76f1e1a..d3ef650e6ce 100644
40bb48b
--- a/Python/formatter_unicode.c
40bb48b
+++ b/Python/formatter_unicode.c
40bb48b
@@ -707,18 +707,11 @@ get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
40bb48b
 {
40bb48b
     switch (type) {
40bb48b
     case LT_CURRENT_LOCALE: {
40bb48b
-        struct lconv *locale_data = localeconv();
40bb48b
-        locale_info->decimal_point = PyUnicode_DecodeLocale(
40bb48b
-                                         locale_data->decimal_point,
40bb48b
-                                         NULL);
40bb48b
-        if (locale_info->decimal_point == NULL)
40bb48b
+        if (_Py_GetLocaleconvNumeric(&locale_info->decimal_point,
40bb48b
+                                     &locale_info->thousands_sep,
40bb48b
+                                     &locale_info->grouping) < 0) {
40bb48b
             return -1;
40bb48b
-        locale_info->thousands_sep = PyUnicode_DecodeLocale(
40bb48b
-                                         locale_data->thousands_sep,
40bb48b
-                                         NULL);
40bb48b
-        if (locale_info->thousands_sep == NULL)
40bb48b
-            return -1;
40bb48b
-        locale_info->grouping = locale_data->grouping;
40bb48b
+        }
40bb48b
         break;
40bb48b
     }
40bb48b
     case LT_DEFAULT_LOCALE: