From 5f6d3a0fd659d2cc1feacbae08a708858f1ce5ce Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Fri, 10 Nov 2017 11:41:50 +0100 Subject: [PATCH] Fix legacy locale coercion tests on platforms that already have a default C.UTF-8 locale --- Lib/test/test_c_locale_coercion.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Lib/test/test_c_locale_coercion.py b/Lib/test/test_c_locale_coercion.py index 635c98faced9cd..5944e5f6b3e5be 100644 --- a/Lib/test/test_c_locale_coercion.py +++ b/Lib/test/test_c_locale_coercion.py @@ -160,11 +160,13 @@ def get_child_details(cls, env_vars): AVAILABLE_TARGETS = None CLI_COERCION_TARGET = None CLI_COERCION_WARNING = None +DEFAULT_LC_CTYPE = None def setUpModule(): global AVAILABLE_TARGETS global CLI_COERCION_TARGET global CLI_COERCION_WARNING + global DEFAULT_LC_CTYPE if AVAILABLE_TARGETS is not None: # initialization already done @@ -181,6 +183,22 @@ def setUpModule(): CLI_COERCION_TARGET = AVAILABLE_TARGETS[0] CLI_COERCION_WARNING = CLI_COERCION_WARNING_FMT.format(CLI_COERCION_TARGET) + # Determine the platform's default LC_CTYPE from the default locale. + # POSIX provides that there *exists* a default locale to provide when + # no environment variables are set, or are set to empty strings. However, + # it does not specify that this default *must* be the "POSIX" locale, or + # how the platform selects the default. In most cases it will be set to + # "C"/"POSIX" but this is not necessarily the case. + result, py_cmd = run_python_until_end( + "-c", "import locale; print(locale.setlocale(locale.LC_CTYPE))", + __isolated=True, + LANG="", LC_ALL="", LC_CTYPE="", PYTHONCOERCECLOCALE="0" + ) + if not result.rc == 0: + result.fail(py_cmd) + + DEFAULT_LC_CTYPE = result.out.decode("ascii").strip().upper() + class _LocaleHandlingTestCase(unittest.TestCase): # Base class to check expected locale handling behaviour @@ -301,6 +319,11 @@ def _check_c_locale_coercion(self, # See https://bugs.python.org/issue30672 for discussion if locale_to_set == "POSIX": continue + elif locale_to_set == "" and DEFAULT_LC_CTYPE == "C.UTF-8": + # The default locale is already C.UTF-8 so the subsequent tests + # don't behave as expected (they assume the default LC_CTYPE will + # be "C") + continue with self.subTest(env_var=env_var, nominal_locale=locale_to_set, PYTHONCOERCECLOCALE=coerce_c_locale):