diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 0b710e3766..dac18cc268 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1516,8 +1516,17 @@ class ReTests(unittest.TestCase): self.assertRaises(re.error, re.compile, r'(?au)\w') def test_locale_flag(self): - import locale - _, enc = locale.getlocale(locale.LC_CTYPE) + # On Windows, Python doesn't set LC_CTYPE to the user preferred + # locale and so LC_CTYPE is the C locale which is different than + # the ANSI code page returned by getpreferredencoding(). + # Set temporarily the LC_CTYPE locale to the user preferred locale + # to get the correct encoding name. + oldloc = locale.setlocale(locale.LC_CTYPE, None) + locale.setlocale(locale.LC_CTYPE, "") + self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldloc) + + enc = locale.getpreferredencoding() + # Search non-ASCII letter for i in range(128, 256): try: diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index eb050ff112..b5c54e3c7f 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4576,6 +4576,47 @@ get_main_config(PyObject *self, PyObject *Py_UNUSED(args)) } +static unsigned int sre_lower_ascii(unsigned int ch) +{ + return ((ch) < 128 ? Py_TOLOWER(ch) : ch); +} + +static unsigned int sre_upper_ascii(unsigned int ch) +{ + return ((ch) < 128 ? Py_TOUPPER(ch) : ch); +} + +static PyObject * +victor2(unsigned int ch) +{ + char str[2]; + str[0] = (char)ch; + str[1] = 0; + wchar_t wstr[2]; + memset(wstr, 0, sizeof(wstr)); + + size_t res = mbstowcs(wstr, str, 1); + if (res == (size_t)-1) { + return PyUnicode_FromString(""); + } + return PyUnicode_FromWideChar(wstr, 1); +} + +static PyObject * +victor(PyObject *self, PyObject *args) +{ + unsigned int ch; + if (!PyArg_ParseTuple(args, "I", &ch)) { + return NULL; + } + unsigned int up = sre_upper_ascii(ch); + unsigned int low = sre_lower_ascii(ch); + return Py_BuildValue("(NNNNN)", + PyLong_FromUnsignedLong(up), PyLong_FromUnsignedLong(low), + victor2(ch), victor2(up), victor2(low)); +} + + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, @@ -4805,6 +4846,7 @@ static PyMethodDef TestMethods[] = { {"get_global_config", get_global_config, METH_NOARGS}, {"get_core_config", get_core_config, METH_NOARGS}, {"get_main_config", get_main_config, METH_NOARGS}, + {"victor", victor, METH_VARARGS}, {NULL, NULL} /* sentinel */ };