Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1 | # -*- coding: koi8-r -*- |
Benjamin Peterson | 130786f | 2009-02-14 17:00:16 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
| 4 | from test import support |
| 5 | |
| 6 | class PEP263Test(unittest.TestCase): |
| 7 | |
| 8 | def test_pep263(self): |
| 9 | self.assertEqual( |
| 10 | "ðÉÔÏÎ".encode("utf-8"), |
| 11 | b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' |
| 12 | ) |
| 13 | self.assertEqual( |
| 14 | "\ð".encode("utf-8"), |
| 15 | b'\\\xd0\x9f' |
| 16 | ) |
| 17 | |
| 18 | def test_compilestring(self): |
| 19 | # see #1882 |
| 20 | c = compile(b"\n# coding: utf-8\nu = '\xc3\xb3'\n", "dummy", "exec") |
| 21 | d = {} |
| 22 | exec(c, d) |
| 23 | self.assertEqual(d['u'], '\xf3') |
| 24 | |
| 25 | def test_issue2301(self): |
| 26 | try: |
| 27 | compile(b"# coding: cp932\nprint '\x94\x4e'", "dummy", "exec") |
| 28 | except SyntaxError as v: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 29 | self.assertEqual(v.text, "print '\u5e74'\n") |
Benjamin Peterson | 130786f | 2009-02-14 17:00:16 +0000 | [diff] [blame] | 30 | else: |
| 31 | self.fail() |
| 32 | |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 33 | def test_issue4626(self): |
| 34 | c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec") |
| 35 | d = {} |
| 36 | exec(c, d) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 37 | self.assertEqual(d['\xc6'], '\xc6') |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 38 | |
Benjamin Peterson | b2e796a | 2009-10-28 21:59:39 +0000 | [diff] [blame] | 39 | def test_issue3297(self): |
| 40 | c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec") |
| 41 | d = {} |
| 42 | exec(c, d) |
| 43 | self.assertEqual(d['a'], d['b']) |
| 44 | self.assertEqual(len(d['a']), len(d['b'])) |
| 45 | self.assertEqual(ascii(d['a']), ascii(d['b'])) |
| 46 | |
Victor Stinner | 6aa278e | 2010-03-03 00:18:49 +0000 | [diff] [blame] | 47 | def test_issue7820(self): |
| 48 | # Ensure that check_bom() restores all bytes in the right order if |
| 49 | # check_bom() fails in pydebug mode: a buffer starts with the first |
| 50 | # byte of a valid BOM, but next bytes are different |
| 51 | |
| 52 | # one byte in common with the UTF-16-LE BOM |
| 53 | self.assertRaises(SyntaxError, eval, b'\xff\x20') |
| 54 | |
| 55 | # two bytes in common with the UTF-8 BOM |
| 56 | self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20') |
| 57 | |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 58 | def test_error_message(self): |
| 59 | compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
| 60 | compile(b'\xef\xbb\xbf\n', 'dummy', 'exec') |
| 61 | compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec') |
R David Murray | c170f37 | 2013-06-30 11:46:32 -0400 | [diff] [blame] | 62 | with self.assertRaisesRegex(SyntaxError, 'fake'): |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 63 | compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec') |
R David Murray | c170f37 | 2013-06-30 11:46:32 -0400 | [diff] [blame] | 64 | with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'): |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 65 | compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 66 | 'dummy', 'exec') |
R David Murray | c170f37 | 2013-06-30 11:46:32 -0400 | [diff] [blame] | 67 | with self.assertRaisesRegex(SyntaxError, 'BOM'): |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 68 | compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 69 | 'dummy', 'exec') |
R David Murray | c170f37 | 2013-06-30 11:46:32 -0400 | [diff] [blame] | 70 | with self.assertRaisesRegex(SyntaxError, 'fake'): |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 71 | compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
R David Murray | c170f37 | 2013-06-30 11:46:32 -0400 | [diff] [blame] | 72 | with self.assertRaisesRegex(SyntaxError, 'BOM'): |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 73 | compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 74 | |
| 75 | |
Benjamin Peterson | 130786f | 2009-02-14 17:00:16 +0000 | [diff] [blame] | 76 | def test_main(): |
| 77 | support.run_unittest(PEP263Test) |
| 78 | |
| 79 | if __name__=="__main__": |
| 80 | test_main() |