1 | n/a | # -*- coding: koi8-r -*- |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | from test.support import TESTFN, unlink, unload, rmtree, script_helper, captured_stdout |
---|
5 | n/a | import importlib |
---|
6 | n/a | import os |
---|
7 | n/a | import sys |
---|
8 | n/a | import subprocess |
---|
9 | n/a | import tempfile |
---|
10 | n/a | |
---|
11 | n/a | class MiscSourceEncodingTest(unittest.TestCase): |
---|
12 | n/a | |
---|
13 | n/a | def test_pep263(self): |
---|
14 | n/a | self.assertEqual( |
---|
15 | n/a | "ðÉÔÏÎ".encode("utf-8"), |
---|
16 | n/a | b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' |
---|
17 | n/a | ) |
---|
18 | n/a | self.assertEqual( |
---|
19 | n/a | "\ð".encode("utf-8"), |
---|
20 | n/a | b'\\\xd0\x9f' |
---|
21 | n/a | ) |
---|
22 | n/a | |
---|
23 | n/a | def test_compilestring(self): |
---|
24 | n/a | # see #1882 |
---|
25 | n/a | c = compile(b"\n# coding: utf-8\nu = '\xc3\xb3'\n", "dummy", "exec") |
---|
26 | n/a | d = {} |
---|
27 | n/a | exec(c, d) |
---|
28 | n/a | self.assertEqual(d['u'], '\xf3') |
---|
29 | n/a | |
---|
30 | n/a | def test_issue2301(self): |
---|
31 | n/a | try: |
---|
32 | n/a | compile(b"# coding: cp932\nprint '\x94\x4e'", "dummy", "exec") |
---|
33 | n/a | except SyntaxError as v: |
---|
34 | n/a | self.assertEqual(v.text, "print '\u5e74'\n") |
---|
35 | n/a | else: |
---|
36 | n/a | self.fail() |
---|
37 | n/a | |
---|
38 | n/a | def test_issue4626(self): |
---|
39 | n/a | c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec") |
---|
40 | n/a | d = {} |
---|
41 | n/a | exec(c, d) |
---|
42 | n/a | self.assertEqual(d['\xc6'], '\xc6') |
---|
43 | n/a | |
---|
44 | n/a | def test_issue3297(self): |
---|
45 | n/a | c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec") |
---|
46 | n/a | d = {} |
---|
47 | n/a | exec(c, d) |
---|
48 | n/a | self.assertEqual(d['a'], d['b']) |
---|
49 | n/a | self.assertEqual(len(d['a']), len(d['b'])) |
---|
50 | n/a | self.assertEqual(ascii(d['a']), ascii(d['b'])) |
---|
51 | n/a | |
---|
52 | n/a | def test_issue7820(self): |
---|
53 | n/a | # Ensure that check_bom() restores all bytes in the right order if |
---|
54 | n/a | # check_bom() fails in pydebug mode: a buffer starts with the first |
---|
55 | n/a | # byte of a valid BOM, but next bytes are different |
---|
56 | n/a | |
---|
57 | n/a | # one byte in common with the UTF-16-LE BOM |
---|
58 | n/a | self.assertRaises(SyntaxError, eval, b'\xff\x20') |
---|
59 | n/a | |
---|
60 | n/a | # two bytes in common with the UTF-8 BOM |
---|
61 | n/a | self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20') |
---|
62 | n/a | |
---|
63 | n/a | def test_20731(self): |
---|
64 | n/a | sub = subprocess.Popen([sys.executable, |
---|
65 | n/a | os.path.join(os.path.dirname(__file__), |
---|
66 | n/a | 'coding20731.py')], |
---|
67 | n/a | stderr=subprocess.PIPE) |
---|
68 | n/a | err = sub.communicate()[1] |
---|
69 | n/a | self.assertEqual(sub.returncode, 0) |
---|
70 | n/a | self.assertNotIn(b'SyntaxError', err) |
---|
71 | n/a | |
---|
72 | n/a | def test_error_message(self): |
---|
73 | n/a | compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
---|
74 | n/a | compile(b'\xef\xbb\xbf\n', 'dummy', 'exec') |
---|
75 | n/a | compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec') |
---|
76 | n/a | with self.assertRaisesRegex(SyntaxError, 'fake'): |
---|
77 | n/a | compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec') |
---|
78 | n/a | with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'): |
---|
79 | n/a | compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
---|
80 | n/a | 'dummy', 'exec') |
---|
81 | n/a | with self.assertRaisesRegex(SyntaxError, 'BOM'): |
---|
82 | n/a | compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
---|
83 | n/a | 'dummy', 'exec') |
---|
84 | n/a | with self.assertRaisesRegex(SyntaxError, 'fake'): |
---|
85 | n/a | compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
---|
86 | n/a | with self.assertRaisesRegex(SyntaxError, 'BOM'): |
---|
87 | n/a | compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
---|
88 | n/a | |
---|
89 | n/a | def test_bad_coding(self): |
---|
90 | n/a | module_name = 'bad_coding' |
---|
91 | n/a | self.verify_bad_module(module_name) |
---|
92 | n/a | |
---|
93 | n/a | def test_bad_coding2(self): |
---|
94 | n/a | module_name = 'bad_coding2' |
---|
95 | n/a | self.verify_bad_module(module_name) |
---|
96 | n/a | |
---|
97 | n/a | def verify_bad_module(self, module_name): |
---|
98 | n/a | self.assertRaises(SyntaxError, __import__, 'test.' + module_name) |
---|
99 | n/a | |
---|
100 | n/a | path = os.path.dirname(__file__) |
---|
101 | n/a | filename = os.path.join(path, module_name + '.py') |
---|
102 | n/a | with open(filename, "rb") as fp: |
---|
103 | n/a | bytes = fp.read() |
---|
104 | n/a | self.assertRaises(SyntaxError, compile, bytes, filename, 'exec') |
---|
105 | n/a | |
---|
106 | n/a | def test_exec_valid_coding(self): |
---|
107 | n/a | d = {} |
---|
108 | n/a | exec(b'# coding: cp949\na = "\xaa\xa7"\n', d) |
---|
109 | n/a | self.assertEqual(d['a'], '\u3047') |
---|
110 | n/a | |
---|
111 | n/a | def test_file_parse(self): |
---|
112 | n/a | # issue1134: all encodings outside latin-1 and utf-8 fail on |
---|
113 | n/a | # multiline strings and long lines (>512 columns) |
---|
114 | n/a | unload(TESTFN) |
---|
115 | n/a | filename = TESTFN + ".py" |
---|
116 | n/a | f = open(filename, "w", encoding="cp1252") |
---|
117 | n/a | sys.path.insert(0, os.curdir) |
---|
118 | n/a | try: |
---|
119 | n/a | with f: |
---|
120 | n/a | f.write("# -*- coding: cp1252 -*-\n") |
---|
121 | n/a | f.write("'''A short string\n") |
---|
122 | n/a | f.write("'''\n") |
---|
123 | n/a | f.write("'A very long string %s'\n" % ("X" * 1000)) |
---|
124 | n/a | |
---|
125 | n/a | importlib.invalidate_caches() |
---|
126 | n/a | __import__(TESTFN) |
---|
127 | n/a | finally: |
---|
128 | n/a | del sys.path[0] |
---|
129 | n/a | unlink(filename) |
---|
130 | n/a | unlink(filename + "c") |
---|
131 | n/a | unlink(filename + "o") |
---|
132 | n/a | unload(TESTFN) |
---|
133 | n/a | rmtree('__pycache__') |
---|
134 | n/a | |
---|
135 | n/a | def test_error_from_string(self): |
---|
136 | n/a | # See http://bugs.python.org/issue6289 |
---|
137 | n/a | input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8') |
---|
138 | n/a | with self.assertRaises(SyntaxError) as c: |
---|
139 | n/a | compile(input, "<string>", "exec") |
---|
140 | n/a | expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \ |
---|
141 | n/a | "ordinal not in range(128)" |
---|
142 | n/a | self.assertTrue(c.exception.args[0].startswith(expected), |
---|
143 | n/a | msg=c.exception.args[0]) |
---|
144 | n/a | |
---|
145 | n/a | |
---|
146 | n/a | class AbstractSourceEncodingTest: |
---|
147 | n/a | |
---|
148 | n/a | def test_default_coding(self): |
---|
149 | n/a | src = (b'print(ascii("\xc3\xa4"))\n') |
---|
150 | n/a | self.check_script_output(src, br"'\xe4'") |
---|
151 | n/a | |
---|
152 | n/a | def test_first_coding_line(self): |
---|
153 | n/a | src = (b'#coding:iso8859-15\n' |
---|
154 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
155 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
156 | n/a | |
---|
157 | n/a | def test_second_coding_line(self): |
---|
158 | n/a | src = (b'#\n' |
---|
159 | n/a | b'#coding:iso8859-15\n' |
---|
160 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
161 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
162 | n/a | |
---|
163 | n/a | def test_third_coding_line(self): |
---|
164 | n/a | # Only first two lines are tested for a magic comment. |
---|
165 | n/a | src = (b'#\n' |
---|
166 | n/a | b'#\n' |
---|
167 | n/a | b'#coding:iso8859-15\n' |
---|
168 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
169 | n/a | self.check_script_output(src, br"'\xe4'") |
---|
170 | n/a | |
---|
171 | n/a | def test_double_coding_line(self): |
---|
172 | n/a | # If the first line matches the second line is ignored. |
---|
173 | n/a | src = (b'#coding:iso8859-15\n' |
---|
174 | n/a | b'#coding:latin1\n' |
---|
175 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
176 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
177 | n/a | |
---|
178 | n/a | def test_double_coding_same_line(self): |
---|
179 | n/a | src = (b'#coding:iso8859-15 coding:latin1\n' |
---|
180 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
181 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
182 | n/a | |
---|
183 | n/a | def test_first_non_utf8_coding_line(self): |
---|
184 | n/a | src = (b'#coding:iso-8859-15 \xa4\n' |
---|
185 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
186 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
187 | n/a | |
---|
188 | n/a | def test_second_non_utf8_coding_line(self): |
---|
189 | n/a | src = (b'\n' |
---|
190 | n/a | b'#coding:iso-8859-15 \xa4\n' |
---|
191 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
192 | n/a | self.check_script_output(src, br"'\xc3\u20ac'") |
---|
193 | n/a | |
---|
194 | n/a | def test_utf8_bom(self): |
---|
195 | n/a | src = (b'\xef\xbb\xbfprint(ascii("\xc3\xa4"))\n') |
---|
196 | n/a | self.check_script_output(src, br"'\xe4'") |
---|
197 | n/a | |
---|
198 | n/a | def test_utf8_bom_and_utf8_coding_line(self): |
---|
199 | n/a | src = (b'\xef\xbb\xbf#coding:utf-8\n' |
---|
200 | n/a | b'print(ascii("\xc3\xa4"))\n') |
---|
201 | n/a | self.check_script_output(src, br"'\xe4'") |
---|
202 | n/a | |
---|
203 | n/a | |
---|
204 | n/a | class BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase): |
---|
205 | n/a | |
---|
206 | n/a | def check_script_output(self, src, expected): |
---|
207 | n/a | with captured_stdout() as stdout: |
---|
208 | n/a | exec(src) |
---|
209 | n/a | out = stdout.getvalue().encode('latin1') |
---|
210 | n/a | self.assertEqual(out.rstrip(), expected) |
---|
211 | n/a | |
---|
212 | n/a | |
---|
213 | n/a | class FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase): |
---|
214 | n/a | |
---|
215 | n/a | def check_script_output(self, src, expected): |
---|
216 | n/a | with tempfile.TemporaryDirectory() as tmpd: |
---|
217 | n/a | fn = os.path.join(tmpd, 'test.py') |
---|
218 | n/a | with open(fn, 'wb') as fp: |
---|
219 | n/a | fp.write(src) |
---|
220 | n/a | res = script_helper.assert_python_ok(fn) |
---|
221 | n/a | self.assertEqual(res.out.rstrip(), expected) |
---|
222 | n/a | |
---|
223 | n/a | |
---|
224 | n/a | if __name__ == "__main__": |
---|
225 | n/a | unittest.main() |
---|