ยปCore Development>Code coverage>Lib/importlib/test/source/test_source_encoding.py

Python code coverage for Lib/importlib/test/source/test_source_encoding.py

#countcontent
1n/afrom . import util as source_util
2n/a
3n/afrom importlib import _bootstrap
4n/aimport codecs
5n/aimport re
6n/aimport sys
7n/a# Because sys.path gets essentially blanked, need to have unicodedata already
8n/a# imported for the parser to use.
9n/aimport unicodedata
10n/aimport unittest
11n/a
12n/a
13n/aCODING_RE = re.compile(r'coding[:=]\s*([-\w.]+)')
14n/a
15n/a
16n/aclass EncodingTest(unittest.TestCase):
17n/a
18n/a """PEP 3120 makes UTF-8 the default encoding for source code
19n/a [default encoding].
20n/a
21n/a PEP 263 specifies how that can change on a per-file basis. Either the first
22n/a or second line can contain the encoding line [encoding first line]
23n/a encoding second line]. If the file has the BOM marker it is considered UTF-8
24n/a implicitly [BOM]. If any encoding is specified it must be UTF-8, else it is
25n/a an error [BOM and utf-8][BOM conflict].
26n/a
27n/a """
28n/a
29n/a variable = '\u00fc'
30n/a character = '\u00c9'
31n/a source_line = "{0} = '{1}'\n".format(variable, character)
32n/a module_name = '_temp'
33n/a
34n/a def run_test(self, source):
35n/a with source_util.create_modules(self.module_name) as mapping:
36n/a with open(mapping[self.module_name], 'wb') as file:
37n/a file.write(source)
38n/a loader = _bootstrap.SourceFileLoader(self.module_name,
39n/a mapping[self.module_name])
40n/a return loader.load_module(self.module_name)
41n/a
42n/a def create_source(self, encoding):
43n/a encoding_line = "# coding={0}".format(encoding)
44n/a assert CODING_RE.search(encoding_line)
45n/a source_lines = [encoding_line.encode('utf-8')]
46n/a source_lines.append(self.source_line.encode(encoding))
47n/a return b'\n'.join(source_lines)
48n/a
49n/a def test_non_obvious_encoding(self):
50n/a # Make sure that an encoding that has never been a standard one for
51n/a # Python works.
52n/a encoding_line = "# coding=koi8-r"
53n/a assert CODING_RE.search(encoding_line)
54n/a source = "{0}\na=42\n".format(encoding_line).encode("koi8-r")
55n/a self.run_test(source)
56n/a
57n/a # [default encoding]
58n/a def test_default_encoding(self):
59n/a self.run_test(self.source_line.encode('utf-8'))
60n/a
61n/a # [encoding first line]
62n/a def test_encoding_on_first_line(self):
63n/a encoding = 'Latin-1'
64n/a source = self.create_source(encoding)
65n/a self.run_test(source)
66n/a
67n/a # [encoding second line]
68n/a def test_encoding_on_second_line(self):
69n/a source = b"#/usr/bin/python\n" + self.create_source('Latin-1')
70n/a self.run_test(source)
71n/a
72n/a # [BOM]
73n/a def test_bom(self):
74n/a self.run_test(codecs.BOM_UTF8 + self.source_line.encode('utf-8'))
75n/a
76n/a # [BOM and utf-8]
77n/a def test_bom_and_utf_8(self):
78n/a source = codecs.BOM_UTF8 + self.create_source('utf-8')
79n/a self.run_test(source)
80n/a
81n/a # [BOM conflict]
82n/a def test_bom_conflict(self):
83n/a source = codecs.BOM_UTF8 + self.create_source('latin-1')
84n/a with self.assertRaises(SyntaxError):
85n/a self.run_test(source)
86n/a
87n/a
88n/aclass LineEndingTest(unittest.TestCase):
89n/a
90n/a r"""Source written with the three types of line endings (\n, \r\n, \r)
91n/a need to be readable [cr][crlf][lf]."""
92n/a
93n/a def run_test(self, line_ending):
94n/a module_name = '_temp'
95n/a source_lines = [b"a = 42", b"b = -13", b'']
96n/a source = line_ending.join(source_lines)
97n/a with source_util.create_modules(module_name) as mapping:
98n/a with open(mapping[module_name], 'wb') as file:
99n/a file.write(source)
100n/a loader = _bootstrap.SourceFileLoader(module_name,
101n/a mapping[module_name])
102n/a return loader.load_module(module_name)
103n/a
104n/a # [cr]
105n/a def test_cr(self):
106n/a self.run_test(b'\r')
107n/a
108n/a # [crlf]
109n/a def test_crlf(self):
110n/a self.run_test(b'\r\n')
111n/a
112n/a # [lf]
113n/a def test_lf(self):
114n/a self.run_test(b'\n')
115n/a
116n/a
117n/adef test_main():
118n/a from test.support import run_unittest
119n/a run_unittest(EncodingTest, LineEndingTest)
120n/a
121n/a
122n/aif __name__ == '__main__':
123n/a test_main()