|
7 | 7 | import sys |
8 | 8 | import textwrap |
9 | 9 | import unittest |
| 10 | +from test import support |
10 | 11 | from test.support.script_helper import assert_python_ok, assert_python_failure |
11 | 12 |
|
12 | 13 |
|
13 | 14 | MS_WINDOWS = (sys.platform == 'win32') |
14 | 15 |
|
15 | 16 |
|
16 | 17 | class UTF8ModeTests(unittest.TestCase): |
17 | | - # Override PYTHONUTF8 and PYTHONLEGACYWINDOWSFSENCODING environment |
18 | | - # variables by default |
19 | | - DEFAULT_ENV = {'PYTHONUTF8': '', 'PYTHONLEGACYWINDOWSFSENCODING': ''} |
| 18 | + DEFAULT_ENV = { |
| 19 | + 'PYTHONUTF8': '', |
| 20 | + 'PYTHONLEGACYWINDOWSFSENCODING': '', |
| 21 | + 'PYTHONCOERCECLOCALE': '0', |
| 22 | + } |
20 | 23 |
|
21 | 24 | def posix_locale(self): |
22 | 25 | loc = locale.setlocale(locale.LC_CTYPE, None) |
@@ -53,7 +56,7 @@ def test_xoption(self): |
53 | 56 | self.assertEqual(out, '0') |
54 | 57 |
|
55 | 58 | if MS_WINDOWS: |
56 | | - # PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 |
| 59 | + # PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 Mode |
57 | 60 | # and has the priority over -X utf8 |
58 | 61 | out = self.get_output('-X', 'utf8', '-c', code, |
59 | 62 | PYTHONLEGACYWINDOWSFSENCODING='1') |
@@ -201,6 +204,25 @@ def test_locale_getpreferredencoding(self): |
201 | 204 | out = self.get_output('-X', 'utf8', '-c', code, LC_ALL='C') |
202 | 205 | self.assertEqual(out, 'UTF-8 UTF-8') |
203 | 206 |
|
| 207 | + @unittest.skipIf(MS_WINDOWS, 'test specific to Unix') |
| 208 | + def test_cmd_line(self): |
| 209 | + arg = 'h\xe9\u20ac'.encode('utf-8') |
| 210 | + arg_utf8 = arg.decode('utf-8') |
| 211 | + arg_ascii = arg.decode('ascii', 'surrogateescape') |
| 212 | + code = 'import locale, sys; print("%s:%s" % (locale.getpreferredencoding(), ascii(sys.argv[1:])))' |
| 213 | + |
| 214 | + def check(utf8_opt, expected, **kw): |
| 215 | + out = self.get_output('-X', utf8_opt, '-c', code, arg, **kw) |
| 216 | + args = out.partition(':')[2].rstrip() |
| 217 | + self.assertEqual(args, ascii(expected), out) |
| 218 | + |
| 219 | + check('utf8', [arg_utf8]) |
| 220 | + if sys.platform == 'darwin' or support.is_android: |
| 221 | + c_arg = arg_utf8 |
| 222 | + else: |
| 223 | + c_arg = arg_ascii |
| 224 | + check('utf8=0', [c_arg], LC_ALL='C') |
| 225 | + |
204 | 226 |
|
205 | 227 | if __name__ == "__main__": |
206 | 228 | unittest.main() |
0 commit comments