|
1 | 1 | # -*- coding: koi8-r -*- |
2 | 2 |
|
3 | 3 | import unittest |
4 | | -from test.support import TESTFN, unlink, unload, rmtree |
| 4 | +from test.support import TESTFN, unlink, unload, rmtree, script_helper, captured_stdout |
5 | 5 | import importlib |
6 | 6 | import os |
7 | 7 | import sys |
8 | 8 | import subprocess |
| 9 | +import tempfile |
9 | 10 |
|
10 | | -class SourceEncodingTest(unittest.TestCase): |
| 11 | +class MiscSourceEncodingTest(unittest.TestCase): |
11 | 12 |
|
12 | 13 | def test_pep263(self): |
13 | 14 | self.assertEqual( |
@@ -142,5 +143,83 @@ def test_error_from_string(self): |
142 | 143 | msg=c.exception.args[0]) |
143 | 144 |
|
144 | 145 |
|
| 146 | +class AbstractSourceEncodingTest: |
| 147 | + |
| 148 | + def test_default_coding(self): |
| 149 | + src = (b'print(ascii("\xc3\xa4"))\n') |
| 150 | + self.check_script_output(src, br"'\xe4'") |
| 151 | + |
| 152 | + def test_first_coding_line(self): |
| 153 | + src = (b'#coding:iso8859-15\n' |
| 154 | + b'print(ascii("\xc3\xa4"))\n') |
| 155 | + self.check_script_output(src, br"'\xc3\u20ac'") |
| 156 | + |
| 157 | + def test_second_coding_line(self): |
| 158 | + src = (b'#\n' |
| 159 | + b'#coding:iso8859-15\n' |
| 160 | + b'print(ascii("\xc3\xa4"))\n') |
| 161 | + self.check_script_output(src, br"'\xc3\u20ac'") |
| 162 | + |
| 163 | + def test_third_coding_line(self): |
| 164 | + # Only first two lines are tested for a magic comment. |
| 165 | + src = (b'#\n' |
| 166 | + b'#\n' |
| 167 | + b'#coding:iso8859-15\n' |
| 168 | + b'print(ascii("\xc3\xa4"))\n') |
| 169 | + self.check_script_output(src, br"'\xe4'") |
| 170 | + |
| 171 | + def test_double_coding_line(self): |
| 172 | + # If the first line matches the second line is ignored. |
| 173 | + src = (b'#coding:iso8859-15\n' |
| 174 | + b'#coding:latin1\n' |
| 175 | + b'print(ascii("\xc3\xa4"))\n') |
| 176 | + self.check_script_output(src, br"'\xc3\u20ac'") |
| 177 | + |
| 178 | + def test_double_coding_same_line(self): |
| 179 | + src = (b'#coding:iso8859-15 coding:latin1\n' |
| 180 | + b'print(ascii("\xc3\xa4"))\n') |
| 181 | + self.check_script_output(src, br"'\xc3\xa4'") |
| 182 | + |
| 183 | + def test_first_non_utf8_coding_line(self): |
| 184 | + src = (b'#coding:iso-8859-15 \xa4\n' |
| 185 | + b'print(ascii("\xc3\xa4"))\n') |
| 186 | + self.check_script_output(src, br"'\xc3\u20ac'") |
| 187 | + |
| 188 | + def test_second_non_utf8_coding_line(self): |
| 189 | + src = (b'\n' |
| 190 | + b'#coding:iso-8859-15 \xa4\n' |
| 191 | + b'print(ascii("\xc3\xa4"))\n') |
| 192 | + self.check_script_output(src, br"'\xc3\u20ac'") |
| 193 | + |
| 194 | + def test_utf8_bom(self): |
| 195 | + src = (b'\xef\xbb\xbfprint(ascii("\xc3\xa4"))\n') |
| 196 | + self.check_script_output(src, br"'\xe4'") |
| 197 | + |
| 198 | + def test_utf8_bom_and_utf8_coding_line(self): |
| 199 | + src = (b'\xef\xbb\xbf#coding:utf-8\n' |
| 200 | + b'print(ascii("\xc3\xa4"))\n') |
| 201 | + self.check_script_output(src, br"'\xe4'") |
| 202 | + |
| 203 | + |
| 204 | +class BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase): |
| 205 | + |
| 206 | + def check_script_output(self, src, expected): |
| 207 | + with captured_stdout() as stdout: |
| 208 | + exec(src) |
| 209 | + out = stdout.getvalue().encode('latin1') |
| 210 | + self.assertEqual(out.rstrip(), expected) |
| 211 | + |
| 212 | + |
| 213 | +class FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase): |
| 214 | + |
| 215 | + def check_script_output(self, src, expected): |
| 216 | + with tempfile.TemporaryDirectory() as tmpd: |
| 217 | + fn = os.path.join(tmpd, 'test.py') |
| 218 | + with open(fn, 'wb') as fp: |
| 219 | + fp.write(src) |
| 220 | + res = script_helper.assert_python_ok(fn) |
| 221 | + self.assertEqual(res.out.rstrip(), expected) |
| 222 | + |
| 223 | + |
145 | 224 | if __name__ == "__main__": |
146 | 225 | unittest.main() |
0 commit comments