Benjamin Peterson | 95777bb | 2009-02-14 17:00:51 +0000 | [diff] [blame] | 1 | # -*- coding: koi8-r -*- |
Benjamin Peterson | 76afd3b | 2009-02-14 16:51:03 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
| 4 | from test import test_support |
| 5 | |
| 6 | class PEP263Test(unittest.TestCase): |
| 7 | |
| 8 | def test_pep263(self): |
| 9 | self.assertEqual( |
| 10 | u"ðÉÔÏÎ".encode("utf-8"), |
| 11 | '\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' |
| 12 | ) |
| 13 | self.assertEqual( |
| 14 | u"\ð".encode("utf-8"), |
| 15 | '\\\xd0\x9f' |
| 16 | ) |
| 17 | |
| 18 | def test_compilestring(self): |
| 19 | # see #1882 |
| 20 | c = compile("\n# coding: utf-8\nu = u'\xc3\xb3'\n", "dummy", "exec") |
| 21 | d = {} |
| 22 | exec c in d |
| 23 | self.assertEqual(d['u'], u'\xf3') |
| 24 | |
| 25 | |
Benjamin Peterson | ea0e3b0 | 2009-10-29 01:49:07 +0000 | [diff] [blame] | 26 | def test_issue3297(self): |
| 27 | c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec") |
| 28 | d = {} |
| 29 | exec(c, d) |
| 30 | self.assertEqual(d['a'], d['b']) |
| 31 | self.assertEqual(len(d['a']), len(d['b'])) |
| 32 | |
Victor Stinner | d23d393 | 2010-03-02 23:20:02 +0000 | [diff] [blame] | 33 | def test_issue7820(self): |
| 34 | # Ensure that check_bom() restores all bytes in the right order if |
| 35 | # check_bom() fails in pydebug mode: a buffer starts with the first |
| 36 | # byte of a valid BOM, but next bytes are different |
| 37 | |
| 38 | # one byte in common with the UTF-16-LE BOM |
| 39 | self.assertRaises(SyntaxError, eval, '\xff\x20') |
| 40 | |
| 41 | # two bytes in common with the UTF-8 BOM |
| 42 | self.assertRaises(SyntaxError, eval, '\xef\xbb\x20') |
| 43 | |
Serhiy Storchaka | 729ad5c | 2013-06-09 16:54:56 +0300 | [diff] [blame] | 44 | def test_error_message(self): |
| 45 | compile('# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
| 46 | compile('\xef\xbb\xbf\n', 'dummy', 'exec') |
| 47 | compile('\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec') |
| 48 | with self.assertRaisesRegexp(SyntaxError, 'fake'): |
| 49 | compile('# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 50 | with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'): |
| 51 | compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 52 | 'dummy', 'exec') |
| 53 | with self.assertRaisesRegexp(SyntaxError, 'BOM'): |
| 54 | compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', |
| 55 | 'dummy', 'exec') |
| 56 | with self.assertRaisesRegexp(SyntaxError, 'fake'): |
| 57 | compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 58 | with self.assertRaisesRegexp(SyntaxError, 'BOM'): |
| 59 | compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 60 | |
Benjamin Peterson | 22d9ee7 | 2013-12-28 10:33:58 -0600 | [diff] [blame] | 61 | def test_non_unicode_codec(self): |
| 62 | with self.assertRaisesRegexp(SyntaxError, |
| 63 | 'codec did not return a unicode'): |
| 64 | from test import bad_coding3 |
| 65 | |
Serhiy Storchaka | 729ad5c | 2013-06-09 16:54:56 +0300 | [diff] [blame] | 66 | |
Benjamin Peterson | 76afd3b | 2009-02-14 16:51:03 +0000 | [diff] [blame] | 67 | def test_main(): |
| 68 | test_support.run_unittest(PEP263Test) |
| 69 | |
| 70 | if __name__=="__main__": |
| 71 | test_main() |