»Core Development>Code coverage>Lib/test/test_source_encoding.py

Python code coverage for Lib/test/test_source_encoding.py

#countcontent
1n/a# -*- coding: koi8-r -*-
2n/a
3n/aimport unittest
4n/afrom test.support import TESTFN, unlink, unload, rmtree, script_helper, captured_stdout
5n/aimport importlib
6n/aimport os
7n/aimport sys
8n/aimport subprocess
9n/aimport tempfile
10n/a
11n/aclass MiscSourceEncodingTest(unittest.TestCase):
12n/a
13n/a def test_pep263(self):
14n/a self.assertEqual(
15n/a "ðÉÔÏÎ".encode("utf-8"),
16n/a b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
17n/a )
18n/a self.assertEqual(
19n/a "\ð".encode("utf-8"),
20n/a b'\\\xd0\x9f'
21n/a )
22n/a
23n/a def test_compilestring(self):
24n/a # see #1882
25n/a c = compile(b"\n# coding: utf-8\nu = '\xc3\xb3'\n", "dummy", "exec")
26n/a d = {}
27n/a exec(c, d)
28n/a self.assertEqual(d['u'], '\xf3')
29n/a
30n/a def test_issue2301(self):
31n/a try:
32n/a compile(b"# coding: cp932\nprint '\x94\x4e'", "dummy", "exec")
33n/a except SyntaxError as v:
34n/a self.assertEqual(v.text, "print '\u5e74'\n")
35n/a else:
36n/a self.fail()
37n/a
38n/a def test_issue4626(self):
39n/a c = compile("# coding=latin-1\n\u00c6 = '\u00c6'", "dummy", "exec")
40n/a d = {}
41n/a exec(c, d)
42n/a self.assertEqual(d['\xc6'], '\xc6')
43n/a
44n/a def test_issue3297(self):
45n/a c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec")
46n/a d = {}
47n/a exec(c, d)
48n/a self.assertEqual(d['a'], d['b'])
49n/a self.assertEqual(len(d['a']), len(d['b']))
50n/a self.assertEqual(ascii(d['a']), ascii(d['b']))
51n/a
52n/a def test_issue7820(self):
53n/a # Ensure that check_bom() restores all bytes in the right order if
54n/a # check_bom() fails in pydebug mode: a buffer starts with the first
55n/a # byte of a valid BOM, but next bytes are different
56n/a
57n/a # one byte in common with the UTF-16-LE BOM
58n/a self.assertRaises(SyntaxError, eval, b'\xff\x20')
59n/a
60n/a # two bytes in common with the UTF-8 BOM
61n/a self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
62n/a
63n/a def test_20731(self):
64n/a sub = subprocess.Popen([sys.executable,
65n/a os.path.join(os.path.dirname(__file__),
66n/a 'coding20731.py')],
67n/a stderr=subprocess.PIPE)
68n/a err = sub.communicate()[1]
69n/a self.assertEqual(sub.returncode, 0)
70n/a self.assertNotIn(b'SyntaxError', err)
71n/a
72n/a def test_error_message(self):
73n/a compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
74n/a compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
75n/a compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
76n/a with self.assertRaisesRegex(SyntaxError, 'fake'):
77n/a compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
78n/a with self.assertRaisesRegex(SyntaxError, 'iso-8859-15'):
79n/a compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
80n/a 'dummy', 'exec')
81n/a with self.assertRaisesRegex(SyntaxError, 'BOM'):
82n/a compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
83n/a 'dummy', 'exec')
84n/a with self.assertRaisesRegex(SyntaxError, 'fake'):
85n/a compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
86n/a with self.assertRaisesRegex(SyntaxError, 'BOM'):
87n/a compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
88n/a
89n/a def test_bad_coding(self):
90n/a module_name = 'bad_coding'
91n/a self.verify_bad_module(module_name)
92n/a
93n/a def test_bad_coding2(self):
94n/a module_name = 'bad_coding2'
95n/a self.verify_bad_module(module_name)
96n/a
97n/a def verify_bad_module(self, module_name):
98n/a self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
99n/a
100n/a path = os.path.dirname(__file__)
101n/a filename = os.path.join(path, module_name + '.py')
102n/a with open(filename, "rb") as fp:
103n/a bytes = fp.read()
104n/a self.assertRaises(SyntaxError, compile, bytes, filename, 'exec')
105n/a
106n/a def test_exec_valid_coding(self):
107n/a d = {}
108n/a exec(b'# coding: cp949\na = "\xaa\xa7"\n', d)
109n/a self.assertEqual(d['a'], '\u3047')
110n/a
111n/a def test_file_parse(self):
112n/a # issue1134: all encodings outside latin-1 and utf-8 fail on
113n/a # multiline strings and long lines (>512 columns)
114n/a unload(TESTFN)
115n/a filename = TESTFN + ".py"
116n/a f = open(filename, "w", encoding="cp1252")
117n/a sys.path.insert(0, os.curdir)
118n/a try:
119n/a with f:
120n/a f.write("# -*- coding: cp1252 -*-\n")
121n/a f.write("'''A short string\n")
122n/a f.write("'''\n")
123n/a f.write("'A very long string %s'\n" % ("X" * 1000))
124n/a
125n/a importlib.invalidate_caches()
126n/a __import__(TESTFN)
127n/a finally:
128n/a del sys.path[0]
129n/a unlink(filename)
130n/a unlink(filename + "c")
131n/a unlink(filename + "o")
132n/a unload(TESTFN)
133n/a rmtree('__pycache__')
134n/a
135n/a def test_error_from_string(self):
136n/a # See http://bugs.python.org/issue6289
137n/a input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
138n/a with self.assertRaises(SyntaxError) as c:
139n/a compile(input, "<string>", "exec")
140n/a expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
141n/a "ordinal not in range(128)"
142n/a self.assertTrue(c.exception.args[0].startswith(expected),
143n/a msg=c.exception.args[0])
144n/a
145n/a
146n/aclass AbstractSourceEncodingTest:
147n/a
148n/a def test_default_coding(self):
149n/a src = (b'print(ascii("\xc3\xa4"))\n')
150n/a self.check_script_output(src, br"'\xe4'")
151n/a
152n/a def test_first_coding_line(self):
153n/a src = (b'#coding:iso8859-15\n'
154n/a b'print(ascii("\xc3\xa4"))\n')
155n/a self.check_script_output(src, br"'\xc3\u20ac'")
156n/a
157n/a def test_second_coding_line(self):
158n/a src = (b'#\n'
159n/a b'#coding:iso8859-15\n'
160n/a b'print(ascii("\xc3\xa4"))\n')
161n/a self.check_script_output(src, br"'\xc3\u20ac'")
162n/a
163n/a def test_third_coding_line(self):
164n/a # Only first two lines are tested for a magic comment.
165n/a src = (b'#\n'
166n/a b'#\n'
167n/a b'#coding:iso8859-15\n'
168n/a b'print(ascii("\xc3\xa4"))\n')
169n/a self.check_script_output(src, br"'\xe4'")
170n/a
171n/a def test_double_coding_line(self):
172n/a # If the first line matches the second line is ignored.
173n/a src = (b'#coding:iso8859-15\n'
174n/a b'#coding:latin1\n'
175n/a b'print(ascii("\xc3\xa4"))\n')
176n/a self.check_script_output(src, br"'\xc3\u20ac'")
177n/a
178n/a def test_double_coding_same_line(self):
179n/a src = (b'#coding:iso8859-15 coding:latin1\n'
180n/a b'print(ascii("\xc3\xa4"))\n')
181n/a self.check_script_output(src, br"'\xc3\u20ac'")
182n/a
183n/a def test_first_non_utf8_coding_line(self):
184n/a src = (b'#coding:iso-8859-15 \xa4\n'
185n/a b'print(ascii("\xc3\xa4"))\n')
186n/a self.check_script_output(src, br"'\xc3\u20ac'")
187n/a
188n/a def test_second_non_utf8_coding_line(self):
189n/a src = (b'\n'
190n/a b'#coding:iso-8859-15 \xa4\n'
191n/a b'print(ascii("\xc3\xa4"))\n')
192n/a self.check_script_output(src, br"'\xc3\u20ac'")
193n/a
194n/a def test_utf8_bom(self):
195n/a src = (b'\xef\xbb\xbfprint(ascii("\xc3\xa4"))\n')
196n/a self.check_script_output(src, br"'\xe4'")
197n/a
198n/a def test_utf8_bom_and_utf8_coding_line(self):
199n/a src = (b'\xef\xbb\xbf#coding:utf-8\n'
200n/a b'print(ascii("\xc3\xa4"))\n')
201n/a self.check_script_output(src, br"'\xe4'")
202n/a
203n/a
204n/aclass BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
205n/a
206n/a def check_script_output(self, src, expected):
207n/a with captured_stdout() as stdout:
208n/a exec(src)
209n/a out = stdout.getvalue().encode('latin1')
210n/a self.assertEqual(out.rstrip(), expected)
211n/a
212n/a
213n/aclass FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
214n/a
215n/a def check_script_output(self, src, expected):
216n/a with tempfile.TemporaryDirectory() as tmpd:
217n/a fn = os.path.join(tmpd, 'test.py')
218n/a with open(fn, 'wb') as fp:
219n/a fp.write(src)
220n/a res = script_helper.assert_python_ok(fn)
221n/a self.assertEqual(res.out.rstrip(), expected)
222n/a
223n/a
224n/aif __name__ == "__main__":
225n/a unittest.main()