blob: f9db0b40f008eb2f28c84c4355f64e5c7d6f4538 [file] [log] [blame]
Neal Norwitz40d37812005-10-02 01:48:49 +00001
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002import test.support, unittest
Florent Xicluna8fbddf12010-03-17 20:29:51 +00003from test.support import TESTFN, unlink, unload
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +00004import os, sys
Neal Norwitz40d37812005-10-02 01:48:49 +00005
6class CodingTest(unittest.TestCase):
7 def test_bad_coding(self):
8 module_name = 'bad_coding'
Neal Norwitzdb83eb32005-12-18 05:29:30 +00009 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 Norwitz40d37812005-10-02 01:48:49 +000016 self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
17
18 path = os.path.dirname(__file__)
19 filename = os.path.join(path, module_name + '.py')
Florent Xicluna8fbddf12010-03-17 20:29:51 +000020 with open(filename, "rb") as fp:
21 bytes = fp.read()
Benjamin Petersonf5b52242009-03-02 23:31:26 +000022 self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
Neal Norwitz40d37812005-10-02 01:48:49 +000023
Neal Norwitzf7f28fc2007-08-11 21:31:25 +000024 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'Arc65f9ace2007-11-15 23:19:43 +000029 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)
Florent Xicluna8fbddf12010-03-17 20:29:51 +000032 unload(TESTFN)
33 sys.path.insert(0, os.curdir)
Amaury Forgeot d'Arcdd9e3b82007-11-16 00:56:23 +000034 filename = TESTFN + ".py"
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000035 f = open(filename, "w")
36 try:
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))
41 f.close()
42
Amaury Forgeot d'Arcdd9e3b82007-11-16 00:56:23 +000043 __import__(TESTFN)
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000044 finally:
45 f.close()
Florent Xicluna8fbddf12010-03-17 20:29:51 +000046 unlink(filename)
47 unlink(filename + "c")
48 unload(TESTFN)
49 del sys.path[0]
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000050
Benjamin Peterson0289b152009-06-28 17:22:03 +000051 def test_error_from_string(self):
52 # See http://bugs.python.org/issue6289
53 input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
Florent Xicluna8fbddf12010-03-17 20:29:51 +000054 with self.assertRaises(SyntaxError) as c:
Benjamin Peterson0289b152009-06-28 17:22:03 +000055 compile(input, "<string>", "exec")
Florent Xicluna8fbddf12010-03-17 20:29:51 +000056 expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
57 "ordinal not in range(128)"
58 self.assertTrue(c.exception.args[0].startswith(expected))
59
Benjamin Peterson0289b152009-06-28 17:22:03 +000060
Neal Norwitz40d37812005-10-02 01:48:49 +000061def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000062 test.support.run_unittest(CodingTest)
Neal Norwitz40d37812005-10-02 01:48:49 +000063
64if __name__ == "__main__":
65 test_main()