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

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

#countcontent
1n/afrom .. import util
2n/a
3n/amachinery = util.import_importlib('importlib.machinery')
4n/a
5n/aimport codecs
6n/aimport importlib.util
7n/aimport re
8n/aimport types
9n/a# Because sys.path gets essentially blanked, need to have unicodedata already
10n/a# imported for the parser to use.
11n/aimport unicodedata
12n/aimport unittest
13n/aimport warnings
14n/a
15n/a
16n/aCODING_RE = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
17n/a
18n/a
19n/aclass EncodingTest:
20n/a
21n/a """PEP 3120 makes UTF-8 the default encoding for source code
22n/a [default encoding].
23n/a
24n/a PEP 263 specifies how that can change on a per-file basis. Either the first
25n/a or second line can contain the encoding line [encoding first line]
26n/a encoding second line]. If the file has the BOM marker it is considered UTF-8
27n/a implicitly [BOM]. If any encoding is specified it must be UTF-8, else it is
28n/a an error [BOM and utf-8][BOM conflict].
29n/a
30n/a """
31n/a
32n/a variable = '\u00fc'
33n/a character = '\u00c9'
34n/a source_line = "{0} = '{1}'\n".format(variable, character)
35n/a module_name = '_temp'
36n/a
37n/a def run_test(self, source):
38n/a with util.create_modules(self.module_name) as mapping:
39n/a with open(mapping[self.module_name], 'wb') as file:
40n/a file.write(source)
41n/a loader = self.machinery.SourceFileLoader(self.module_name,
42n/a mapping[self.module_name])
43n/a return self.load(loader)
44n/a
45n/a def create_source(self, encoding):
46n/a encoding_line = "# coding={0}".format(encoding)
47n/a assert CODING_RE.match(encoding_line)
48n/a source_lines = [encoding_line.encode('utf-8')]
49n/a source_lines.append(self.source_line.encode(encoding))
50n/a return b'\n'.join(source_lines)
51n/a
52n/a def test_non_obvious_encoding(self):
53n/a # Make sure that an encoding that has never been a standard one for
54n/a # Python works.
55n/a encoding_line = "# coding=koi8-r"
56n/a assert CODING_RE.match(encoding_line)
57n/a source = "{0}\na=42\n".format(encoding_line).encode("koi8-r")
58n/a self.run_test(source)
59n/a
60n/a # [default encoding]
61n/a def test_default_encoding(self):
62n/a self.run_test(self.source_line.encode('utf-8'))
63n/a
64n/a # [encoding first line]
65n/a def test_encoding_on_first_line(self):
66n/a encoding = 'Latin-1'
67n/a source = self.create_source(encoding)
68n/a self.run_test(source)
69n/a
70n/a # [encoding second line]
71n/a def test_encoding_on_second_line(self):
72n/a source = b"#/usr/bin/python\n" + self.create_source('Latin-1')
73n/a self.run_test(source)
74n/a
75n/a # [BOM]
76n/a def test_bom(self):
77n/a self.run_test(codecs.BOM_UTF8 + self.source_line.encode('utf-8'))
78n/a
79n/a # [BOM and utf-8]
80n/a def test_bom_and_utf_8(self):
81n/a source = codecs.BOM_UTF8 + self.create_source('utf-8')
82n/a self.run_test(source)
83n/a
84n/a # [BOM conflict]
85n/a def test_bom_conflict(self):
86n/a source = codecs.BOM_UTF8 + self.create_source('latin-1')
87n/a with self.assertRaises(SyntaxError):
88n/a self.run_test(source)
89n/a
90n/a
91n/aclass EncodingTestPEP451(EncodingTest):
92n/a
93n/a def load(self, loader):
94n/a module = types.ModuleType(self.module_name)
95n/a module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader)
96n/a loader.exec_module(module)
97n/a return module
98n/a
99n/a
100n/a(Frozen_EncodingTestPEP451,
101n/a Source_EncodingTestPEP451
102n/a ) = util.test_both(EncodingTestPEP451, machinery=machinery)
103n/a
104n/a
105n/aclass EncodingTestPEP302(EncodingTest):
106n/a
107n/a def load(self, loader):
108n/a with warnings.catch_warnings():
109n/a warnings.simplefilter('ignore', DeprecationWarning)
110n/a return loader.load_module(self.module_name)
111n/a
112n/a
113n/a(Frozen_EncodingTestPEP302,
114n/a Source_EncodingTestPEP302
115n/a ) = util.test_both(EncodingTestPEP302, machinery=machinery)
116n/a
117n/a
118n/aclass LineEndingTest:
119n/a
120n/a r"""Source written with the three types of line endings (\n, \r\n, \r)
121n/a need to be readable [cr][crlf][lf]."""
122n/a
123n/a def run_test(self, line_ending):
124n/a module_name = '_temp'
125n/a source_lines = [b"a = 42", b"b = -13", b'']
126n/a source = line_ending.join(source_lines)
127n/a with util.create_modules(module_name) as mapping:
128n/a with open(mapping[module_name], 'wb') as file:
129n/a file.write(source)
130n/a loader = self.machinery.SourceFileLoader(module_name,
131n/a mapping[module_name])
132n/a return self.load(loader, module_name)
133n/a
134n/a # [cr]
135n/a def test_cr(self):
136n/a self.run_test(b'\r')
137n/a
138n/a # [crlf]
139n/a def test_crlf(self):
140n/a self.run_test(b'\r\n')
141n/a
142n/a # [lf]
143n/a def test_lf(self):
144n/a self.run_test(b'\n')
145n/a
146n/a
147n/aclass LineEndingTestPEP451(LineEndingTest):
148n/a
149n/a def load(self, loader, module_name):
150n/a module = types.ModuleType(module_name)
151n/a module.__spec__ = importlib.util.spec_from_loader(module_name, loader)
152n/a loader.exec_module(module)
153n/a return module
154n/a
155n/a
156n/a(Frozen_LineEndingTestPEP451,
157n/a Source_LineEndingTestPEP451
158n/a ) = util.test_both(LineEndingTestPEP451, machinery=machinery)
159n/a
160n/a
161n/aclass LineEndingTestPEP302(LineEndingTest):
162n/a
163n/a def load(self, loader, module_name):
164n/a with warnings.catch_warnings():
165n/a warnings.simplefilter('ignore', DeprecationWarning)
166n/a return loader.load_module(module_name)
167n/a
168n/a
169n/a(Frozen_LineEndingTestPEP302,
170n/a Source_LineEndingTestPEP302
171n/a ) = util.test_both(LineEndingTestPEP302, machinery=machinery)
172n/a
173n/a
174n/aif __name__ == '__main__':
175n/a unittest.main()