[Python-checkins] bpo-37335: Fix test_c_locale_coercion to handle any ASCII alias (GH-14449)

Victor Stinner webhook-mailer at python.org
Tue Jul 2 06:46:05 EDT 2019


https://github.com/python/cpython/commit/c53173aa00689aa1be17ce5406289718f6b30532
commit: c53173aa00689aa1be17ce5406289718f6b30532
branch: 3.7
author: Jakub Kulík <Kulikjak at gmail.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-07-02T12:46:00+02:00
summary:

bpo-37335: Fix test_c_locale_coercion to handle any ASCII alias (GH-14449)

Fix unexpected ASCII aliases in locale coercion tests: normalize encoding
names with codecs.lookup(encoding).name.

files:
A Misc/NEWS.d/next/Tests/2019-06-28-16-54-46.bpo-37335.LLzOx8.rst
M Lib/test/test_c_locale_coercion.py

diff --git a/Lib/test/test_c_locale_coercion.py b/Lib/test/test_c_locale_coercion.py
index f2351fa5a2ea..94391c87995a 100644
--- a/Lib/test/test_c_locale_coercion.py
+++ b/Lib/test/test_c_locale_coercion.py
@@ -97,11 +97,11 @@ def _set_locale_in_subprocess(locale_name):
 class EncodingDetails(_EncodingDetails):
     # XXX (ncoghlan): Using JSON for child state reporting may be less fragile
     CHILD_PROCESS_SCRIPT = ";".join([
-        "import sys, os",
-        "print(sys.getfilesystemencoding())",
-        "print(sys.stdin.encoding + ':' + sys.stdin.errors)",
-        "print(sys.stdout.encoding + ':' + sys.stdout.errors)",
-        "print(sys.stderr.encoding + ':' + sys.stderr.errors)",
+        "import sys, os, codecs",
+        "print(codecs.lookup(sys.getfilesystemencoding()).name)",
+        "print(codecs.lookup(sys.stdin.encoding).name + ':' + sys.stdin.errors)",
+        "print(codecs.lookup(sys.stdout.encoding).name + ':' + sys.stdout.errors)",
+        "print(codecs.lookup(sys.stderr.encoding).name + ':' + sys.stderr.errors)",
         "print(os.environ.get('LANG', 'not set'))",
         "print(os.environ.get('LC_CTYPE', 'not set'))",
         "print(os.environ.get('LC_ALL', 'not set'))",
@@ -116,28 +116,15 @@ def get_expected_details(cls, coercion_expected, fs_encoding, stream_encoding, e
         stream_info = 2*[_stream.format("surrogateescape")]
         # stderr should always use backslashreplace
         stream_info.append(_stream.format("backslashreplace"))
-        expected_lang = env_vars.get("LANG", "not set").lower()
+        expected_lang = env_vars.get("LANG", "not set")
         if coercion_expected:
-            expected_lc_ctype = CLI_COERCION_TARGET.lower()
+            expected_lc_ctype = CLI_COERCION_TARGET
         else:
-            expected_lc_ctype = env_vars.get("LC_CTYPE", "not set").lower()
-        expected_lc_all = env_vars.get("LC_ALL", "not set").lower()
+            expected_lc_ctype = env_vars.get("LC_CTYPE", "not set")
+        expected_lc_all = env_vars.get("LC_ALL", "not set")
         env_info = expected_lang, expected_lc_ctype, expected_lc_all
         return dict(cls(fs_encoding, *stream_info, *env_info)._asdict())
 
-    @staticmethod
-    def _handle_output_variations(data):
-        """Adjust the output to handle platform specific idiosyncrasies
-
-        * Some platforms report ASCII as ANSI_X3.4-1968
-        * Some platforms report ASCII as US-ASCII
-        * Some platforms report UTF-8 instead of utf-8
-        """
-        data = data.replace(b"ANSI_X3.4-1968", b"ascii")
-        data = data.replace(b"US-ASCII", b"ascii")
-        data = data.lower()
-        return data
-
     @classmethod
     def get_child_details(cls, env_vars):
         """Retrieves fsencoding and standard stream details from a child process
@@ -157,8 +144,7 @@ def get_child_details(cls, env_vars):
         if not result.rc == 0:
             result.fail(py_cmd)
         # All subprocess outputs in this test case should be pure ASCII
-        adjusted_output = cls._handle_output_variations(result.out)
-        stdout_lines = adjusted_output.decode("ascii").splitlines()
+        stdout_lines = result.out.decode("ascii").splitlines()
         child_encoding_details = dict(cls(*stdout_lines)._asdict())
         stderr_lines = result.err.decode("ascii").rstrip().splitlines()
         return child_encoding_details, stderr_lines
diff --git a/Misc/NEWS.d/next/Tests/2019-06-28-16-54-46.bpo-37335.LLzOx8.rst b/Misc/NEWS.d/next/Tests/2019-06-28-16-54-46.bpo-37335.LLzOx8.rst
new file mode 100644
index 000000000000..71fad15dd8e8
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-06-28-16-54-46.bpo-37335.LLzOx8.rst
@@ -0,0 +1,2 @@
+Improve locale coercion tests by using codec lookup instead of more fragile
+replace().



More information about the Python-checkins mailing list