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