Skip to content

Commit c53173a

Browse files
kulikjakvstinner
authored andcommitted
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.
1 parent bf82cd3 commit c53173a

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

Lib/test/test_c_locale_coercion.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def _set_locale_in_subprocess(locale_name):
9797
class EncodingDetails(_EncodingDetails):
9898
# XXX (ncoghlan): Using JSON for child state reporting may be less fragile
9999
CHILD_PROCESS_SCRIPT = ";".join([
100-
"import sys, os",
101-
"print(sys.getfilesystemencoding())",
102-
"print(sys.stdin.encoding + ':' + sys.stdin.errors)",
103-
"print(sys.stdout.encoding + ':' + sys.stdout.errors)",
104-
"print(sys.stderr.encoding + ':' + sys.stderr.errors)",
100+
"import sys, os, codecs",
101+
"print(codecs.lookup(sys.getfilesystemencoding()).name)",
102+
"print(codecs.lookup(sys.stdin.encoding).name + ':' + sys.stdin.errors)",
103+
"print(codecs.lookup(sys.stdout.encoding).name + ':' + sys.stdout.errors)",
104+
"print(codecs.lookup(sys.stderr.encoding).name + ':' + sys.stderr.errors)",
105105
"print(os.environ.get('LANG', 'not set'))",
106106
"print(os.environ.get('LC_CTYPE', 'not set'))",
107107
"print(os.environ.get('LC_ALL', 'not set'))",
@@ -116,28 +116,15 @@ def get_expected_details(cls, coercion_expected, fs_encoding, stream_encoding, e
116116
stream_info = 2*[_stream.format("surrogateescape")]
117117
# stderr should always use backslashreplace
118118
stream_info.append(_stream.format("backslashreplace"))
119-
expected_lang = env_vars.get("LANG", "not set").lower()
119+
expected_lang = env_vars.get("LANG", "not set")
120120
if coercion_expected:
121-
expected_lc_ctype = CLI_COERCION_TARGET.lower()
121+
expected_lc_ctype = CLI_COERCION_TARGET
122122
else:
123-
expected_lc_ctype = env_vars.get("LC_CTYPE", "not set").lower()
124-
expected_lc_all = env_vars.get("LC_ALL", "not set").lower()
123+
expected_lc_ctype = env_vars.get("LC_CTYPE", "not set")
124+
expected_lc_all = env_vars.get("LC_ALL", "not set")
125125
env_info = expected_lang, expected_lc_ctype, expected_lc_all
126126
return dict(cls(fs_encoding, *stream_info, *env_info)._asdict())
127127

128-
@staticmethod
129-
def _handle_output_variations(data):
130-
"""Adjust the output to handle platform specific idiosyncrasies
131-
132-
* Some platforms report ASCII as ANSI_X3.4-1968
133-
* Some platforms report ASCII as US-ASCII
134-
* Some platforms report UTF-8 instead of utf-8
135-
"""
136-
data = data.replace(b"ANSI_X3.4-1968", b"ascii")
137-
data = data.replace(b"US-ASCII", b"ascii")
138-
data = data.lower()
139-
return data
140-
141128
@classmethod
142129
def get_child_details(cls, env_vars):
143130
"""Retrieves fsencoding and standard stream details from a child process
@@ -157,8 +144,7 @@ def get_child_details(cls, env_vars):
157144
if not result.rc == 0:
158145
result.fail(py_cmd)
159146
# All subprocess outputs in this test case should be pure ASCII
160-
adjusted_output = cls._handle_output_variations(result.out)
161-
stdout_lines = adjusted_output.decode("ascii").splitlines()
147+
stdout_lines = result.out.decode("ascii").splitlines()
162148
child_encoding_details = dict(cls(*stdout_lines)._asdict())
163149
stderr_lines = result.err.decode("ascii").rstrip().splitlines()
164150
return child_encoding_details, stderr_lines
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve locale coercion tests by using codec lookup instead of more fragile
2+
replace().

0 commit comments

Comments
 (0)