Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 1 | |
| 2 | import test.test_support, unittest |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame^] | 3 | import os, sys |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 4 | |
| 5 | class CodingTest(unittest.TestCase): |
| 6 | def test_bad_coding(self): |
| 7 | module_name = 'bad_coding' |
Neal Norwitz | db83eb3 | 2005-12-18 05:29:30 +0000 | [diff] [blame] | 8 | self.verify_bad_module(module_name) |
| 9 | |
| 10 | def test_bad_coding2(self): |
| 11 | module_name = 'bad_coding2' |
| 12 | self.verify_bad_module(module_name) |
| 13 | |
| 14 | def verify_bad_module(self, module_name): |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 15 | self.assertRaises(SyntaxError, __import__, 'test.' + module_name) |
| 16 | |
| 17 | path = os.path.dirname(__file__) |
| 18 | filename = os.path.join(path, module_name + '.py') |
Hye-Shik Chang | 49d90bc | 2007-08-13 13:11:39 +0000 | [diff] [blame] | 19 | fp = open(filename, encoding='utf-8') |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 20 | text = fp.read() |
| 21 | fp.close() |
| 22 | self.assertRaises(SyntaxError, compile, text, filename, 'exec') |
| 23 | |
Neal Norwitz | f7f28fc | 2007-08-11 21:31:25 +0000 | [diff] [blame] | 24 | def test_exec_valid_coding(self): |
| 25 | d = {} |
| 26 | exec('# coding: cp949\na = 5\n', d) |
| 27 | self.assertEqual(d['a'], 5) |
| 28 | |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame^] | 29 | def test_file_parse(self): |
| 30 | # issue1134: all encodings outside latin-1 and utf-8 fail on |
| 31 | # multiline strings and long lines (>512 columns) |
| 32 | sys.path.insert(0, ".") |
| 33 | filename = test.test_support.TESTFN+".py" |
| 34 | f = open(filename, "w") |
| 35 | try: |
| 36 | f.write("# -*- coding: cp1252 -*-\n") |
| 37 | f.write("'''A short string\n") |
| 38 | f.write("'''\n") |
| 39 | f.write("'A very long string %s'\n" % ("X" * 1000)) |
| 40 | f.close() |
| 41 | |
| 42 | __import__(test.test_support.TESTFN) |
| 43 | finally: |
| 44 | f.close() |
| 45 | os.remove(test.test_support.TESTFN+".py") |
| 46 | os.remove(test.test_support.TESTFN+".pyc") |
| 47 | sys.path.pop(0) |
| 48 | |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 49 | def test_main(): |
| 50 | test.test_support.run_unittest(CodingTest) |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | test_main() |