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