From 77867455ab938329caac65e3dc6c7ab827a552bb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 13 Jun 2013 09:48:15 +0300 Subject: Issue #18048: Rename test_pep263.py to test_source_encoding.py. --- Lib/test/test_source_encoding.py | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Lib/test/test_source_encoding.py (limited to 'Lib/test/test_source_encoding.py') diff --git a/Lib/test/test_source_encoding.py b/Lib/test/test_source_encoding.py new file mode 100644 index 0000000000..1290bc70de --- /dev/null +++ b/Lib/test/test_source_encoding.py @@ -0,0 +1,80 @@ +# -*- coding: koi8-r -*- + +import unittest +from test import support + +class PEP263Test(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.assertRaisesRegexp(SyntaxError, 'fake'): + compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec') + with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'): + compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', + 'dummy', 'exec') + with self.assertRaisesRegexp(SyntaxError, 'BOM'): + compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', + 'dummy', 'exec') + with self.assertRaisesRegexp(SyntaxError, 'fake'): + compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') + with self.assertRaisesRegexp(SyntaxError, 'BOM'): + compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') + + +def test_main(): + support.run_unittest(PEP263Test) + +if __name__=="__main__": + test_main() -- cgit v1.2.1 From 2c890c090e9d7565fa5f0a991480a161a893f7bb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 13 Jun 2013 09:50:42 +0300 Subject: Issue #18048: Rename test_coding.py to test_source_encoding.py. --- Lib/test/test_source_encoding.py | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Lib/test/test_source_encoding.py (limited to 'Lib/test/test_source_encoding.py') diff --git a/Lib/test/test_source_encoding.py b/Lib/test/test_source_encoding.py new file mode 100644 index 0000000000..dfd5431dee --- /dev/null +++ b/Lib/test/test_source_encoding.py @@ -0,0 +1,65 @@ +import test.support, unittest +from test.support import TESTFN, unlink, unload +import importlib, os, sys + +class CodingTest(unittest.TestCase): + 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 = {} + exec('# coding: cp949\na = 5\n', d) + self.assertEqual(d['a'], 5) + + 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) + sys.path.insert(0, os.curdir) + filename = TESTFN + ".py" + f = open(filename, "w") + try: + 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() + + importlib.invalidate_caches() + __import__(TESTFN) + finally: + f.close() + unlink(filename) + unlink(filename + "c") + unload(TESTFN) + del sys.path[0] + + 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, "", "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)) + + +def test_main(): + test.support.run_unittest(CodingTest) + +if __name__ == "__main__": + test_main() -- cgit v1.2.1 From 8fc2f21cb2c0122c000dbc79fd87ecbfd1740790 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 16 Jun 2013 11:37:57 -0400 Subject: Stop using the deprecated unittest.TestCase.assertRaisesRegexp() --- Lib/test/test_source_encoding.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Lib/test/test_source_encoding.py') diff --git a/Lib/test/test_source_encoding.py b/Lib/test/test_source_encoding.py index 0e539d5767..c2faee195e 100644 --- a/Lib/test/test_source_encoding.py +++ b/Lib/test/test_source_encoding.py @@ -62,17 +62,17 @@ class SourceEncodingTest(unittest.TestCase): 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.assertRaisesRegexp(SyntaxError, 'fake'): + with self.assertRaisesRegex(SyntaxError, 'fake'): compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec') - with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'): + with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'): compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') - with self.assertRaisesRegexp(SyntaxError, 'BOM'): + with self.assertRaisesRegex(SyntaxError, 'BOM'): compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') - with self.assertRaisesRegexp(SyntaxError, 'fake'): + with self.assertRaisesRegex(SyntaxError, 'fake'): compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') - with self.assertRaisesRegexp(SyntaxError, 'BOM'): + with self.assertRaisesRegex(SyntaxError, 'BOM'): compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') def test_bad_coding(self): -- cgit v1.2.1