|
1 | | -import test.support, unittest |
| 1 | +# -*- coding: koi8-r -*- |
| 2 | + |
| 3 | +import unittest |
2 | 4 | from test.support import TESTFN, unlink, unload |
3 | | -import importlib, os, sys |
| 5 | +import importlib |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +class SourceEncodingTest(unittest.TestCase): |
| 10 | + |
| 11 | + def test_pep263(self): |
| 12 | + self.assertEqual( |
| 13 | + "ðÉÔÏÎ".encode("utf-8"), |
| 14 | + b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' |
| 15 | + ) |
| 16 | + self.assertEqual( |
| 17 | + "\ð".encode("utf-8"), |
| 18 | + b'\\\xd0\x9f' |
| 19 | + ) |
| 20 | + |
| 21 | + def test_compilestring(self): |
| 22 | + # see #1882 |
| 23 | + c = compile(b"\n# coding: utf-8\nu = '\xc3\xb3'\n", "dummy", "exec") |
| 24 | + d = {} |
| 25 | + exec(c, d) |
| 26 | + self.assertEqual(d['u'], '\xf3') |
| 27 | + |
| 28 | + def test_issue2301(self): |
| 29 | + try: |
| 30 | + compile(b"# coding: cp932\nprint '\x94\x4e'", "dummy", "exec") |
| 31 | + except SyntaxError as v: |
| 32 | + self.assertEqual(v.text, "print '\u5e74'\n") |
| 33 | + else: |
| 34 | + self.fail() |
| 35 | + |
| 36 | + def test_issue4626(self): |
| 37 | + c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec") |
| 38 | + d = {} |
| 39 | + exec(c, d) |
| 40 | + self.assertEqual(d['\xc6'], '\xc6') |
| 41 | + |
| 42 | + def test_issue3297(self): |
| 43 | + c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec") |
| 44 | + d = {} |
| 45 | + exec(c, d) |
| 46 | + self.assertEqual(d['a'], d['b']) |
| 47 | + self.assertEqual(len(d['a']), len(d['b'])) |
| 48 | + self.assertEqual(ascii(d['a']), ascii(d['b'])) |
| 49 | + |
| 50 | + def test_issue7820(self): |
| 51 | + # Ensure that check_bom() restores all bytes in the right order if |
| 52 | + # check_bom() fails in pydebug mode: a buffer starts with the first |
| 53 | + # byte of a valid BOM, but next bytes are different |
| 54 | + |
| 55 | + # one byte in common with the UTF-16-LE BOM |
| 56 | + self.assertRaises(SyntaxError, eval, b'\xff\x20') |
| 57 | + |
| 58 | + # two bytes in common with the UTF-8 BOM |
| 59 | + self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20') |
| 60 | + |
| 61 | + def test_error_message(self): |
| 62 | + compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
| 63 | + compile(b'\xef\xbb\xbf\n', 'dummy', 'exec') |
| 64 | + compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec') |
| 65 | + with self.assertRaisesRegexp(SyntaxError, 'fake'): |
| 66 | + compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 67 | + with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'): |
| 68 | + compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 69 | + 'dummy', 'exec') |
| 70 | + with self.assertRaisesRegexp(SyntaxError, 'BOM'): |
| 71 | + compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 72 | + 'dummy', 'exec') |
| 73 | + with self.assertRaisesRegexp(SyntaxError, 'fake'): |
| 74 | + compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 75 | + with self.assertRaisesRegexp(SyntaxError, 'BOM'): |
| 76 | + compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
4 | 77 |
|
5 | | -class CodingTest(unittest.TestCase): |
6 | 78 | def test_bad_coding(self): |
7 | 79 | module_name = 'bad_coding' |
8 | 80 | self.verify_bad_module(module_name) |
@@ -58,8 +130,5 @@ def test_error_from_string(self): |
58 | 130 | self.assertTrue(c.exception.args[0].startswith(expected)) |
59 | 131 |
|
60 | 132 |
|
61 | | -def test_main(): |
62 | | - test.support.run_unittest(CodingTest) |
63 | | - |
64 | 133 | if __name__ == "__main__": |
65 | | - test_main() |
| 134 | + unittest.main() |
0 commit comments