Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 1 | |
| 2 | import test.test_support, unittest |
| 3 | import os |
| 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') |
Florent Xicluna | 2b73c21 | 2010-03-17 19:05:04 +0000 | [diff] [blame^] | 19 | with open(filename) as fp: |
| 20 | text = fp.read() |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 21 | self.assertRaises(SyntaxError, compile, text, filename, 'exec') |
| 22 | |
Benjamin Peterson | 08a0bbc | 2009-06-16 00:29:31 +0000 | [diff] [blame] | 23 | def test_error_from_string(self): |
| 24 | # See http://bugs.python.org/issue6289 |
| 25 | input = u"# coding: ascii\n\N{SNOWMAN}".encode('utf-8') |
Florent Xicluna | 2b73c21 | 2010-03-17 19:05:04 +0000 | [diff] [blame^] | 26 | with self.assertRaises(SyntaxError) as c: |
Benjamin Peterson | 08a0bbc | 2009-06-16 00:29:31 +0000 | [diff] [blame] | 27 | compile(input, "<string>", "exec") |
Florent Xicluna | 2b73c21 | 2010-03-17 19:05:04 +0000 | [diff] [blame^] | 28 | expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \ |
| 29 | "ordinal not in range(128)" |
| 30 | self.assertTrue(c.exception.args[0].startswith(expected)) |
| 31 | |
Benjamin Peterson | 08a0bbc | 2009-06-16 00:29:31 +0000 | [diff] [blame] | 32 | |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 33 | def test_main(): |
| 34 | test.test_support.run_unittest(CodingTest) |
| 35 | |
| 36 | if __name__ == "__main__": |
| 37 | test_main() |