1import unittest 2 3class PEP3120Test(unittest.TestCase): 4 5 def test_pep3120(self): 6 self.assertEqual( 7 "Питон".encode("utf-8"), 8 b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' 9 ) 10 self.assertEqual( 11 "\П".encode("utf-8"), 12 b'\\\xd0\x9f' 13 ) 14 15 def test_badsyntax(self): 16 try: 17 import test.tokenizedata.badsyntax_pep3120 18 except SyntaxError as msg: 19 msg = str(msg).lower() 20 self.assertTrue('utf-8' in msg) 21 else: 22 self.fail("expected exception didn't occur") 23 24 25class BuiltinCompileTests(unittest.TestCase): 26 27 # Issue 3574. 28 def test_latin1(self): 29 # Allow compile() to read Latin-1 source. 30 source_code = '# coding: Latin-1\nu = "Ç"\n'.encode("Latin-1") 31 try: 32 code = compile(source_code, '<dummy>', 'exec') 33 except SyntaxError: 34 self.fail("compile() cannot handle Latin-1 source") 35 ns = {} 36 exec(code, ns) 37 self.assertEqual('Ç', ns['u']) 38 39 40if __name__ == "__main__": 41 unittest.main() 42