blob: 598d980b2a67e289dfefb23de2265ee229c13b0e [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
Benjamin Peterson130786f2009-02-14 17:00:16 +000058def test_main():
59 support.run_unittest(PEP263Test)
60
61if __name__=="__main__":
62 test_main()