blob: 324ae3861965d1f9500fe21872ac58fab61ac2ca [file] [log] [blame]
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001# -*- coding: koi8-r -*-
Benjamin Peterson130786f2009-02-14 17:00:16 +00002
3import unittest
4from test import support
5
6class 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 Melottib3aedd42010-11-20 19:04:17 +000029 self.assertEqual(v.text, "print '\u5e74'\n")
Benjamin Peterson130786f2009-02-14 17:00:16 +000030 else:
31 self.fail()
32
Benjamin Petersonf5b52242009-03-02 23:31:26 +000033 def test_issue4626(self):
34 c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec")
35 d = {}
36 exec(c, d)
Ezio Melottib3aedd42010-11-20 19:04:17 +000037 self.assertEqual(d['\xc6'], '\xc6')
Benjamin Petersonf5b52242009-03-02 23:31:26 +000038
Benjamin Petersonb2e796a2009-10-28 21:59:39 +000039 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 Stinner6aa278e2010-03-03 00:18:49 +000047 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 Storchaka3af14aa2013-06-09 16:51:52 +030058 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 Murrayc170f372013-06-30 11:46:32 -040062 with self.assertRaisesRegex(SyntaxError, 'fake'):
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +030063 compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
R David Murrayc170f372013-06-30 11:46:32 -040064 with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'):
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +030065 compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
66 'dummy', 'exec')
R David Murrayc170f372013-06-30 11:46:32 -040067 with self.assertRaisesRegex(SyntaxError, 'BOM'):
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +030068 compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
69 'dummy', 'exec')
R David Murrayc170f372013-06-30 11:46:32 -040070 with self.assertRaisesRegex(SyntaxError, 'fake'):
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +030071 compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
R David Murrayc170f372013-06-30 11:46:32 -040072 with self.assertRaisesRegex(SyntaxError, 'BOM'):
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +030073 compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
74
75
Benjamin Peterson130786f2009-02-14 17:00:16 +000076def test_main():
77 support.run_unittest(PEP263Test)
78
79if __name__=="__main__":
80 test_main()