Back to index

python3.2  3.2.2
test_coding.py
Go to the documentation of this file.
00001 
00002 import test.support, unittest
00003 from test.support import TESTFN, unlink, unload
00004 import os, sys
00005 
00006 class CodingTest(unittest.TestCase):
00007     def test_bad_coding(self):
00008         module_name = 'bad_coding'
00009         self.verify_bad_module(module_name)
00010 
00011     def test_bad_coding2(self):
00012         module_name = 'bad_coding2'
00013         self.verify_bad_module(module_name)
00014 
00015     def verify_bad_module(self, module_name):
00016         self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
00017 
00018         path = os.path.dirname(__file__)
00019         filename = os.path.join(path, module_name + '.py')
00020         with open(filename, "rb") as fp:
00021             bytes = fp.read()
00022         self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
00023 
00024     def test_exec_valid_coding(self):
00025         d = {}
00026         exec('# coding: cp949\na = 5\n', d)
00027         self.assertEqual(d['a'], 5)
00028 
00029     def test_file_parse(self):
00030         # issue1134: all encodings outside latin-1 and utf-8 fail on
00031         # multiline strings and long lines (>512 columns)
00032         unload(TESTFN)
00033         sys.path.insert(0, os.curdir)
00034         filename = TESTFN + ".py"
00035         f = open(filename, "w")
00036         try:
00037             f.write("# -*- coding: cp1252 -*-\n")
00038             f.write("'''A short string\n")
00039             f.write("'''\n")
00040             f.write("'A very long string %s'\n" % ("X" * 1000))
00041             f.close()
00042 
00043             __import__(TESTFN)
00044         finally:
00045             f.close()
00046             unlink(filename)
00047             unlink(filename + "c")
00048             unload(TESTFN)
00049             del sys.path[0]
00050 
00051     def test_error_from_string(self):
00052         # See http://bugs.python.org/issue6289
00053         input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
00054         with self.assertRaises(SyntaxError) as c:
00055             compile(input, "<string>", "exec")
00056         expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
00057                    "ordinal not in range(128)"
00058         self.assertTrue(c.exception.args[0].startswith(expected))
00059 
00060 
00061 def test_main():
00062     test.support.run_unittest(CodingTest)
00063 
00064 if __name__ == "__main__":
00065     test_main()