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 3.7 doesn't call setlocale(LC_CTYPE, "") at
+ # startup and so the LC_CTYPE locale uses Latin1 encoding by default,
+ # whereas getpreferredencoding() returns the ANSI code page. Set
+ # temporarily the LC_CTYPE locale to the user preferred encoding to
+ # ensure that it uses the ANSI code page.
+ oldloc = locale.setlocale(locale.LC_CTYPE, None)
+ locale.setlocale(locale.LC_CTYPE, "")
+ self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldloc)
+
+ # Get the current locale encoding
+ enc = locale.getpreferredencoding(False)
+
# Search non-ASCII letter
for i in range(128, 256):
try:
--- /dev/null
+Fix ``test_re.test_locale_flag()``: use ``locale.getpreferredencoding()``
+rather than ``locale.getlocale()`` to get the locale encoding. With some
+locales, ``locale.getlocale()`` returns the wrong encoding. On Windows, set
+temporarily the ``LC_CTYPE`` locale to the user preferred encoding to ensure
+that it uses the ANSI code page, to be consistent with
+``locale.getpreferredencoding()``.