blob: 989c7a85d2ff8428a1562e0e579f8e7eaf494da2 [file] [log] [blame]
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +03001import unittest
Florent Xicluna8fbddf12010-03-17 20:29:51 +00002from test.support import TESTFN, unlink, unload
Brett Cannonafbdc132012-04-14 15:06:17 -04003import importlib, os, sys
Neal Norwitz40d37812005-10-02 01:48:49 +00004
5class CodingTest(unittest.TestCase):
6 def test_bad_coding(self):
7 module_name = 'bad_coding'
Neal Norwitzdb83eb32005-12-18 05:29:30 +00008 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 Norwitz40d37812005-10-02 01:48:49 +000015 self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
16
17 path = os.path.dirname(__file__)
18 filename = os.path.join(path, module_name + '.py')
Florent Xicluna8fbddf12010-03-17 20:29:51 +000019 with open(filename, "rb") as fp:
20 bytes = fp.read()
Benjamin Petersonf5b52242009-03-02 23:31:26 +000021 self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
Neal Norwitz40d37812005-10-02 01:48:49 +000022
Neal Norwitzf7f28fc2007-08-11 21:31:25 +000023 def test_exec_valid_coding(self):
24 d = {}
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030025 exec(b'# coding: cp949\na = "\xaa\xa7"\n', d)
26 self.assertEqual(d['a'], '\u3047')
Neal Norwitzf7f28fc2007-08-11 21:31:25 +000027
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000028 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 Xicluna8fbddf12010-03-17 20:29:51 +000031 unload(TESTFN)
Amaury Forgeot d'Arcdd9e3b82007-11-16 00:56:23 +000032 filename = TESTFN + ".py"
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030033 f = open(filename, "w", encoding="cp1252")
34 sys.path.insert(0, os.curdir)
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000035 try:
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030036 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'Arc65f9ace2007-11-15 23:19:43 +000041
Brett Cannonafbdc132012-04-14 15:06:17 -040042 importlib.invalidate_caches()
Amaury Forgeot d'Arcdd9e3b82007-11-16 00:56:23 +000043 __import__(TESTFN)
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +000044 finally:
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030045 del sys.path[0]
Florent Xicluna8fbddf12010-03-17 20:29:51 +000046 unlink(filename)
47 unlink(filename + "c")
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030048 unlink(filename + "o")
Florent Xicluna8fbddf12010-03-17 20:29:51 +000049 unload(TESTFN)
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)"
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030058 self.assertTrue(c.exception.args[0].startswith(expected),
59 msg=c.exception.args[0])
Florent Xicluna8fbddf12010-03-17 20:29:51 +000060
Benjamin Peterson0289b152009-06-28 17:22:03 +000061
Neal Norwitz40d37812005-10-02 01:48:49 +000062if __name__ == "__main__":
Serhiy Storchaka0a3cdf02013-06-19 10:23:35 +030063 unittest.main()