Home | History | Annotate | Download | only in test
      1 # -*- coding: koi8-r -*-
      2 
      3 import unittest
      4 from test.test_support import run_unittest, rmtree, captured_stdout
      5 import script_helper
      6 import os
      7 import tempfile
      8 
      9 class MiscSourceEncodingTest(unittest.TestCase):
     10 
     11     def test_pep263(self):
     12         self.assertEqual(
     13             u"".encode("utf-8"),
     14             '\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
     15         )
     16         self.assertEqual(
     17             u"\".encode("utf-8"),
     18             '\\\xd0\x9f'
     19         )
     20 
     21     def test_compilestring(self):
     22         # see #1882
     23         c = compile("\n# coding: utf-8\nu = u'\xc3\xb3'\n", "dummy", "exec")
     24         d = {}
     25         exec c in d
     26         self.assertEqual(d['u'], u'\xf3')
     27 
     28     def test_issue3297(self):
     29         c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec")
     30         d = {}
     31         exec(c, d)
     32         self.assertEqual(d['a'], d['b'])
     33         self.assertEqual(len(d['a']), len(d['b']))
     34 
     35     def test_issue7820(self):
     36         # Ensure that check_bom() restores all bytes in the right order if
     37         # check_bom() fails in pydebug mode: a buffer starts with the first
     38         # byte of a valid BOM, but next bytes are different
     39 
     40         # one byte in common with the UTF-16-LE BOM
     41         self.assertRaises(SyntaxError, eval, '\xff\x20')
     42 
     43         # two bytes in common with the UTF-8 BOM
     44         self.assertRaises(SyntaxError, eval, '\xef\xbb\x20')
     45 
     46     def test_error_message(self):
     47         compile('# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
     48         compile('\xef\xbb\xbf\n', 'dummy', 'exec')
     49         compile('\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
     50         with self.assertRaisesRegexp(SyntaxError, 'fake'):
     51             compile('# -*- coding: fake -*-\n', 'dummy', 'exec')
     52         with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
     53             compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
     54                     'dummy', 'exec')
     55         with self.assertRaisesRegexp(SyntaxError, 'BOM'):
     56             compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
     57                     'dummy', 'exec')
     58         with self.assertRaisesRegexp(SyntaxError, 'fake'):
     59             compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
     60         with self.assertRaisesRegexp(SyntaxError, 'BOM'):
     61             compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
     62 
     63     def test_non_unicode_codec(self):
     64         with self.assertRaisesRegexp(SyntaxError,
     65                                      'codec did not return a unicode'):
     66             from test import bad_coding3
     67 
     68 
     69     def test_bad_coding(self):
     70         module_name = 'bad_coding'
     71         self.verify_bad_module(module_name)
     72 
     73     def test_bad_coding2(self):
     74         module_name = 'bad_coding2'
     75         self.verify_bad_module(module_name)
     76 
     77     def verify_bad_module(self, module_name):
     78         self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
     79 
     80         path = os.path.dirname(__file__)
     81         filename = os.path.join(path, module_name + '.py')
     82         with open(filename) as fp:
     83             text = fp.read()
     84         self.assertRaises(SyntaxError, compile, text, filename, 'exec')
     85 
     86     def test_error_from_string(self):
     87         # See http://bugs.python.org/issue6289
     88         input = u"# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
     89         with self.assertRaises(SyntaxError) as c:
     90             compile(input, "<string>", "exec")
     91         expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
     92                    "ordinal not in range(128)"
     93         self.assertTrue(c.exception.args[0].startswith(expected))
     94 
     95 
     96 class AbstractSourceEncodingTest:
     97 
     98     def test_first_coding_line(self):
     99         src = ('#coding:iso8859-15\n'
    100                'print(repr(u"\xc3\xa4"))\n')
    101         self.check_script_output(src, r"u'\xc3\u20ac'")
    102 
    103     def test_second_coding_line(self):
    104         src = ('#\n'
    105                '#coding:iso8859-15\n'
    106                'print(repr(u"\xc3\xa4"))\n')
    107         self.check_script_output(src, r"u'\xc3\u20ac'")
    108 
    109     def test_double_coding_line(self):
    110         # If the first line matches the second line is ignored.
    111         src = ('#coding:iso8859-15\n'
    112                '#coding:latin1\n'
    113                'print(repr(u"\xc3\xa4"))\n')
    114         self.check_script_output(src, r"u'\xc3\u20ac'")
    115 
    116     def test_double_coding_same_line(self):
    117         src = ('#coding:iso8859-15 coding:latin1\n'
    118                'print(repr(u"\xc3\xa4"))\n')
    119         self.check_script_output(src, r"u'\xc3\u20ac'")
    120 
    121     def test_first_non_utf8_coding_line(self):
    122         src = ('#coding:iso-8859-15 \xa4\n'
    123                'print(repr(u"\xc3\xa4"))\n')
    124         self.check_script_output(src, r"u'\xc3\u20ac'")
    125 
    126     def test_second_non_utf8_coding_line(self):
    127         src = ('\n'
    128                '#coding:iso-8859-15 \xa4\n'
    129                'print(repr(u"\xc3\xa4"))\n')
    130         self.check_script_output(src, r"u'\xc3\u20ac'")
    131 
    132     def test_utf8_bom(self):
    133         src = ('\xef\xbb\xbfprint(repr(u"\xc3\xa4"))\n')
    134         self.check_script_output(src, r"u'\xe4'")
    135 
    136     def test_utf8_bom_and_utf8_coding_line(self):
    137         src = ('\xef\xbb\xbf#coding:utf-8\n'
    138                'print(repr(u"\xc3\xa4"))\n')
    139         self.check_script_output(src, r"u'\xe4'")
    140 
    141 
    142 class BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
    143 
    144     def check_script_output(self, src, expected):
    145         with captured_stdout() as stdout:
    146             exec(src)
    147         out = stdout.getvalue().encode('latin1')
    148         self.assertEqual(out.rstrip(), expected)
    149 
    150 
    151 class FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
    152 
    153     def check_script_output(self, src, expected):
    154         tmpd = tempfile.mkdtemp()
    155         try:
    156             fn = os.path.join(tmpd, 'test.py')
    157             with open(fn, 'wb') as fp:
    158                 fp.write(src)
    159             rc, out, err = script_helper.assert_python_ok(fn)
    160         finally:
    161             rmtree(tmpd)
    162         self.assertEqual(out.rstrip(), expected)
    163 
    164 
    165 def test_main():
    166     run_unittest(MiscSourceEncodingTest, BytesSourceEncodingTest, FileSourceEncodingTest)
    167 
    168 if __name__ == "__main__":
    169     test_main()
    170