Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 1 | import unittest |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 2 | from test.support import TESTFN, unlink, unload |
Brett Cannon | afbdc13 | 2012-04-14 15:06:17 -0400 | [diff] [blame] | 3 | import importlib, 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') |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 19 | with open(filename, "rb") as fp: |
| 20 | bytes = fp.read() |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 21 | self.assertRaises(SyntaxError, compile, bytes, filename, 'exec') |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 22 | |
Neal Norwitz | f7f28fc | 2007-08-11 21:31:25 +0000 | [diff] [blame] | 23 | def test_exec_valid_coding(self): |
| 24 | d = {} |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 25 | exec(b'# coding: cp949\na = "\xaa\xa7"\n', d) |
| 26 | self.assertEqual(d['a'], '\u3047') |
Neal Norwitz | f7f28fc | 2007-08-11 21:31:25 +0000 | [diff] [blame] | 27 | |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 28 | def test_file_parse(self): |
| 29 | # issue1134: all encodings outside latin-1 and utf-8 fail on |
| 30 | # multiline strings and long lines (>512 columns) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 31 | unload(TESTFN) |
Amaury Forgeot d'Arc | dd9e3b8 | 2007-11-16 00:56:23 +0000 | [diff] [blame] | 32 | filename = TESTFN + ".py" |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 33 | f = open(filename, "w", encoding="cp1252") |
| 34 | sys.path.insert(0, os.curdir) |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 35 | try: |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 36 | with f: |
| 37 | f.write("# -*- coding: cp1252 -*-\n") |
| 38 | f.write("'''A short string\n") |
| 39 | f.write("'''\n") |
| 40 | f.write("'A very long string %s'\n" % ("X" * 1000)) |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 41 | |
Brett Cannon | afbdc13 | 2012-04-14 15:06:17 -0400 | [diff] [blame] | 42 | importlib.invalidate_caches() |
Amaury Forgeot d'Arc | dd9e3b8 | 2007-11-16 00:56:23 +0000 | [diff] [blame] | 43 | __import__(TESTFN) |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 44 | finally: |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 45 | del sys.path[0] |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 46 | unlink(filename) |
| 47 | unlink(filename + "c") |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 48 | unlink(filename + "o") |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 49 | unload(TESTFN) |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 50 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 51 | def test_error_from_string(self): |
| 52 | # See http://bugs.python.org/issue6289 |
| 53 | input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8') |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 54 | with self.assertRaises(SyntaxError) as c: |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 55 | compile(input, "<string>", "exec") |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 56 | expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \ |
| 57 | "ordinal not in range(128)" |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 58 | self.assertTrue(c.exception.args[0].startswith(expected), |
| 59 | msg=c.exception.args[0]) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 60 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 61 | |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 62 | if __name__ == "__main__": |
Serhiy Storchaka | 0a3cdf0 | 2013-06-19 10:23:35 +0300 | [diff] [blame] | 63 | unittest.main() |