test_utf8source.py 1.15 KB
Newer Older
Martin v. Löwis's avatar
Martin v. Löwis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# This file is marked as binary in the CVS, to prevent MacCVS from recoding it.

import unittest

class PEP3120Test(unittest.TestCase):

    def test_pep3120(self):
        self.assertEqual(
            "Питон".encode("utf-8"),
            b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
        )
        self.assertEqual(
            "\П".encode("utf-8"),
            b'\\\xd0\x9f'
        )

    def test_badsyntax(self):
        try:
            import test.badsyntax_pep3120
        except SyntaxError as msg:
21 22
            msg = str(msg).lower()
            self.assertTrue('utf-8' in msg)
Martin v. Löwis's avatar
Martin v. Löwis committed
23 24 25
        else:
            self.fail("expected exception didn't occur")

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

class BuiltinCompileTests(unittest.TestCase):

    # Issue 3574.
    def test_latin1(self):
        # Allow compile() to read Latin-1 source.
        source_code = '# coding: Latin-1\nu = "Ç"\n'.encode("Latin-1")
        try:
            code = compile(source_code, '<dummy>', 'exec')
        except SyntaxError:
            self.fail("compile() cannot handle Latin-1 source")
        ns = {}
        exec(code, ns)
        self.assertEqual('Ç', ns['u'])


42 43
if __name__ == "__main__":
    unittest.main()