bpo-38324: Fix test__locale.py Windows failures (GH-20529)

Use wide-char _W_* fields of lconv structure on Windows
Remove "ps_AF" from test__locale.known_numerics on Windows
This commit is contained in:
TIGirardi 2020-10-20 08:39:52 -03:00 committed by GitHub
parent d5d0521270
commit f2312037e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 5 deletions

View File

@ -72,6 +72,10 @@ known_numerics = {
'ps_AF': ('\u066b', '\u066c'), 'ps_AF': ('\u066b', '\u066c'),
} }
if sys.platform == 'win32':
# ps_AF doesn't work on Windows: see bpo-38324 (msg361830)
del known_numerics['ps_AF']
class _LocaleTests(unittest.TestCase): class _LocaleTests(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -0,0 +1 @@
Avoid Unicode errors when accessing certain locale data on Windows.

View File

@ -155,6 +155,7 @@ locale_is_ascii(const char *str)
static int static int
locale_decode_monetary(PyObject *dict, struct lconv *lc) locale_decode_monetary(PyObject *dict, struct lconv *lc)
{ {
#ifndef MS_WINDOWS
int change_locale; int change_locale;
change_locale = (!locale_is_ascii(lc->int_curr_symbol) change_locale = (!locale_is_ascii(lc->int_curr_symbol)
|| !locale_is_ascii(lc->currency_symbol) || !locale_is_ascii(lc->currency_symbol)
@ -190,12 +191,18 @@ locale_decode_monetary(PyObject *dict, struct lconv *lc)
} }
} }
#define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL)
#else /* MS_WINDOWS */
/* Use _W_* fields of Windows struct lconv */
#define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1)
#endif /* MS_WINDOWS */
int res = -1; int res = -1;
#define RESULT_STRING(ATTR) \ #define RESULT_STRING(ATTR) \
do { \ do { \
PyObject *obj; \ PyObject *obj; \
obj = PyUnicode_DecodeLocale(lc->ATTR, NULL); \ obj = GET_LOCALE_STRING(ATTR); \
if (obj == NULL) { \ if (obj == NULL) { \
goto done; \ goto done; \
} \ } \
@ -211,14 +218,17 @@ locale_decode_monetary(PyObject *dict, struct lconv *lc)
RESULT_STRING(mon_decimal_point); RESULT_STRING(mon_decimal_point);
RESULT_STRING(mon_thousands_sep); RESULT_STRING(mon_thousands_sep);
#undef RESULT_STRING #undef RESULT_STRING
#undef GET_LOCALE_STRING
res = 0; res = 0;
done: done:
#ifndef MS_WINDOWS
if (loc != NULL) { if (loc != NULL) {
setlocale(LC_CTYPE, oldloc); setlocale(LC_CTYPE, oldloc);
} }
PyMem_Free(oldloc); PyMem_Free(oldloc);
#endif
return res; return res;
} }
@ -258,9 +268,15 @@ _locale_localeconv_impl(PyObject *module)
Py_DECREF(obj); \ Py_DECREF(obj); \
} while (0) } while (0)
#ifdef MS_WINDOWS
/* Use _W_* fields of Windows struct lconv */
#define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1)
#else
#define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL)
#endif
#define RESULT_STRING(s)\ #define RESULT_STRING(s)\
do { \ do { \
x = PyUnicode_DecodeLocale(lc->s, NULL); \ x = GET_LOCALE_STRING(s); \
RESULT(#s, x); \ RESULT(#s, x); \
} while (0) } while (0)
@ -289,8 +305,10 @@ _locale_localeconv_impl(PyObject *module)
RESULT_INT(n_sign_posn); RESULT_INT(n_sign_posn);
/* Numeric information: LC_NUMERIC encoding */ /* Numeric information: LC_NUMERIC encoding */
PyObject *decimal_point, *thousands_sep; PyObject *decimal_point = NULL, *thousands_sep = NULL;
if (_Py_GetLocaleconvNumeric(lc, &decimal_point, &thousands_sep) < 0) { if (_Py_GetLocaleconvNumeric(lc, &decimal_point, &thousands_sep) < 0) {
Py_XDECREF(decimal_point);
Py_XDECREF(thousands_sep);
goto failed; goto failed;
} }
@ -319,6 +337,7 @@ _locale_localeconv_impl(PyObject *module)
#undef RESULT #undef RESULT
#undef RESULT_STRING #undef RESULT_STRING
#undef RESULT_INT #undef RESULT_INT
#undef GET_LOCALE_STRING
} }
#if defined(HAVE_WCSCOLL) #if defined(HAVE_WCSCOLL)

View File

@ -2047,6 +2047,7 @@ _Py_GetLocaleconvNumeric(struct lconv *lc,
assert(decimal_point != NULL); assert(decimal_point != NULL);
assert(thousands_sep != NULL); assert(thousands_sep != NULL);
#ifndef MS_WINDOWS
int change_locale = 0; int change_locale = 0;
if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) { if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) {
change_locale = 1; change_locale = 1;
@ -2085,14 +2086,20 @@ _Py_GetLocaleconvNumeric(struct lconv *lc,
} }
} }
#define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL)
#else /* MS_WINDOWS */
/* Use _W_* fields of Windows strcut lconv */
#define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1)
#endif /* MS_WINDOWS */
int res = -1; int res = -1;
*decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL); *decimal_point = GET_LOCALE_STRING(decimal_point);
if (*decimal_point == NULL) { if (*decimal_point == NULL) {
goto done; goto done;
} }
*thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL); *thousands_sep = GET_LOCALE_STRING(thousands_sep);
if (*thousands_sep == NULL) { if (*thousands_sep == NULL) {
goto done; goto done;
} }
@ -2100,11 +2107,15 @@ _Py_GetLocaleconvNumeric(struct lconv *lc,
res = 0; res = 0;
done: done:
#ifndef MS_WINDOWS
if (loc != NULL) { if (loc != NULL) {
setlocale(LC_CTYPE, oldloc); setlocale(LC_CTYPE, oldloc);
} }
PyMem_Free(oldloc); PyMem_Free(oldloc);
#endif
return res; return res;
#undef GET_LOCALE_STRING
} }
/* Our selection logic for which function to use is as follows: /* Our selection logic for which function to use is as follows: