]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #18202: Fix minor bugs and cleanup test_source_encoding.py.
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 19 Jun 2013 07:29:58 +0000 (10:29 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 19 Jun 2013 07:29:58 +0000 (10:29 +0300)
1  2 
Lib/test/test_source_encoding.py

index c2faee195e7fe91beb78b0eb538c165e5d0623f3,0000000000000000000000000000000000000000..cd9d2b374c735f316cee040b5439721c597b0369
mode 100644,000000..100644
--- /dev/null
@@@ -1,134 -1,0 +1,135 @@@
-         exec('# coding: cp949\na = 5\n', d)
-         self.assertEqual(d['a'], 5)
 +# -*- coding: koi8-r -*-
 +
 +import unittest
 +from test.support import TESTFN, unlink, unload
 +import importlib
 +import os
 +import sys
 +
 +class SourceEncodingTest(unittest.TestCase):
 +
 +    def test_pep263(self):
 +        self.assertEqual(
 +            "ðÉÔÏÎ".encode("utf-8"),
 +            b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
 +        )
 +        self.assertEqual(
 +            "\ð".encode("utf-8"),
 +            b'\\\xd0\x9f'
 +        )
 +
 +    def test_compilestring(self):
 +        # see #1882
 +        c = compile(b"\n# coding: utf-8\nu = '\xc3\xb3'\n", "dummy", "exec")
 +        d = {}
 +        exec(c, d)
 +        self.assertEqual(d['u'], '\xf3')
 +
 +    def test_issue2301(self):
 +        try:
 +            compile(b"# coding: cp932\nprint '\x94\x4e'", "dummy", "exec")
 +        except SyntaxError as v:
 +            self.assertEqual(v.text, "print '\u5e74'\n")
 +        else:
 +            self.fail()
 +
 +    def test_issue4626(self):
 +        c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec")
 +        d = {}
 +        exec(c, d)
 +        self.assertEqual(d['\xc6'], '\xc6')
 +
 +    def test_issue3297(self):
 +        c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec")
 +        d = {}
 +        exec(c, d)
 +        self.assertEqual(d['a'], d['b'])
 +        self.assertEqual(len(d['a']), len(d['b']))
 +        self.assertEqual(ascii(d['a']), ascii(d['b']))
 +
 +    def test_issue7820(self):
 +        # Ensure that check_bom() restores all bytes in the right order if
 +        # check_bom() fails in pydebug mode: a buffer starts with the first
 +        # byte of a valid BOM, but next bytes are different
 +
 +        # one byte in common with the UTF-16-LE BOM
 +        self.assertRaises(SyntaxError, eval, b'\xff\x20')
 +
 +        # two bytes in common with the UTF-8 BOM
 +        self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
 +
 +    def test_error_message(self):
 +        compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
 +        compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
 +        compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
 +        with self.assertRaisesRegex(SyntaxError, 'fake'):
 +            compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
 +        with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'):
 +            compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
 +                    'dummy', 'exec')
 +        with self.assertRaisesRegex(SyntaxError, 'BOM'):
 +            compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
 +                    'dummy', 'exec')
 +        with self.assertRaisesRegex(SyntaxError, 'fake'):
 +            compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
 +        with self.assertRaisesRegex(SyntaxError, 'BOM'):
 +            compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
 +
 +    def test_bad_coding(self):
 +        module_name = 'bad_coding'
 +        self.verify_bad_module(module_name)
 +
 +    def test_bad_coding2(self):
 +        module_name = 'bad_coding2'
 +        self.verify_bad_module(module_name)
 +
 +    def verify_bad_module(self, module_name):
 +        self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
 +
 +        path = os.path.dirname(__file__)
 +        filename = os.path.join(path, module_name + '.py')
 +        with open(filename, "rb") as fp:
 +            bytes = fp.read()
 +        self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
 +
 +    def test_exec_valid_coding(self):
 +        d = {}
-         sys.path.insert(0, os.curdir)
++        exec(b'# coding: cp949\na = "\xaa\xa7"\n', d)
++        self.assertEqual(d['a'], '\u3047')
 +
 +    def test_file_parse(self):
 +        # issue1134: all encodings outside latin-1 and utf-8 fail on
 +        # multiline strings and long lines (>512 columns)
 +        unload(TESTFN)
-         f = open(filename, "w")
 +        filename = TESTFN + ".py"
-             f.write("# -*- coding: cp1252 -*-\n")
-             f.write("'''A short string\n")
-             f.write("'''\n")
-             f.write("'A very long string %s'\n" % ("X" * 1000))
-             f.close()
++        f = open(filename, "w", encoding="cp1252")
++        sys.path.insert(0, os.curdir)
 +        try:
-             f.close()
++            with f:
++                f.write("# -*- coding: cp1252 -*-\n")
++                f.write("'''A short string\n")
++                f.write("'''\n")
++                f.write("'A very long string %s'\n" % ("X" * 1000))
 +
 +            importlib.invalidate_caches()
 +            __import__(TESTFN)
 +        finally:
-             del sys.path[0]
++            del sys.path[0]
 +            unlink(filename)
 +            unlink(filename + "c")
++            unlink(filename + "o")
 +            unload(TESTFN)
-         self.assertTrue(c.exception.args[0].startswith(expected))
 +
 +    def test_error_from_string(self):
 +        # See http://bugs.python.org/issue6289
 +        input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
 +        with self.assertRaises(SyntaxError) as c:
 +            compile(input, "<string>", "exec")
 +        expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
 +                   "ordinal not in range(128)"
++        self.assertTrue(c.exception.args[0].startswith(expected),
++                        msg=c.exception.args[0])
 +
 +
 +if __name__ == "__main__":
 +    unittest.main()