1 | n/a | import math |
---|
2 | n/a | import os |
---|
3 | n/a | import unittest |
---|
4 | n/a | import sys |
---|
5 | n/a | import _ast |
---|
6 | n/a | import tempfile |
---|
7 | n/a | import types |
---|
8 | n/a | from test import support |
---|
9 | n/a | from test.support import script_helper |
---|
10 | n/a | |
---|
11 | n/a | class TestSpecifics(unittest.TestCase): |
---|
12 | n/a | |
---|
13 | n/a | def compile_single(self, source): |
---|
14 | n/a | compile(source, "<single>", "single") |
---|
15 | n/a | |
---|
16 | n/a | def assertInvalidSingle(self, source): |
---|
17 | n/a | self.assertRaises(SyntaxError, self.compile_single, source) |
---|
18 | n/a | |
---|
19 | n/a | def test_no_ending_newline(self): |
---|
20 | n/a | compile("hi", "<test>", "exec") |
---|
21 | n/a | compile("hi\r", "<test>", "exec") |
---|
22 | n/a | |
---|
23 | n/a | def test_empty(self): |
---|
24 | n/a | compile("", "<test>", "exec") |
---|
25 | n/a | |
---|
26 | n/a | def test_other_newlines(self): |
---|
27 | n/a | compile("\r\n", "<test>", "exec") |
---|
28 | n/a | compile("\r", "<test>", "exec") |
---|
29 | n/a | compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec") |
---|
30 | n/a | compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec") |
---|
31 | n/a | |
---|
32 | n/a | def test_debug_assignment(self): |
---|
33 | n/a | # catch assignments to __debug__ |
---|
34 | n/a | self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single') |
---|
35 | n/a | import builtins |
---|
36 | n/a | prev = builtins.__debug__ |
---|
37 | n/a | setattr(builtins, '__debug__', 'sure') |
---|
38 | n/a | setattr(builtins, '__debug__', prev) |
---|
39 | n/a | |
---|
40 | n/a | def test_argument_handling(self): |
---|
41 | n/a | # detect duplicate positional and keyword arguments |
---|
42 | n/a | self.assertRaises(SyntaxError, eval, 'lambda a,a:0') |
---|
43 | n/a | self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0') |
---|
44 | n/a | self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0') |
---|
45 | n/a | self.assertRaises(SyntaxError, exec, 'def f(a, a): pass') |
---|
46 | n/a | self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass') |
---|
47 | n/a | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
---|
48 | n/a | |
---|
49 | n/a | def test_syntax_error(self): |
---|
50 | n/a | self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec") |
---|
51 | n/a | |
---|
52 | n/a | def test_none_keyword_arg(self): |
---|
53 | n/a | self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec") |
---|
54 | n/a | |
---|
55 | n/a | def test_duplicate_global_local(self): |
---|
56 | n/a | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
---|
57 | n/a | |
---|
58 | n/a | def test_exec_with_general_mapping_for_locals(self): |
---|
59 | n/a | |
---|
60 | n/a | class M: |
---|
61 | n/a | "Test mapping interface versus possible calls from eval()." |
---|
62 | n/a | def __getitem__(self, key): |
---|
63 | n/a | if key == 'a': |
---|
64 | n/a | return 12 |
---|
65 | n/a | raise KeyError |
---|
66 | n/a | def __setitem__(self, key, value): |
---|
67 | n/a | self.results = (key, value) |
---|
68 | n/a | def keys(self): |
---|
69 | n/a | return list('xyz') |
---|
70 | n/a | |
---|
71 | n/a | m = M() |
---|
72 | n/a | g = globals() |
---|
73 | n/a | exec('z = a', g, m) |
---|
74 | n/a | self.assertEqual(m.results, ('z', 12)) |
---|
75 | n/a | try: |
---|
76 | n/a | exec('z = b', g, m) |
---|
77 | n/a | except NameError: |
---|
78 | n/a | pass |
---|
79 | n/a | else: |
---|
80 | n/a | self.fail('Did not detect a KeyError') |
---|
81 | n/a | exec('z = dir()', g, m) |
---|
82 | n/a | self.assertEqual(m.results, ('z', list('xyz'))) |
---|
83 | n/a | exec('z = globals()', g, m) |
---|
84 | n/a | self.assertEqual(m.results, ('z', g)) |
---|
85 | n/a | exec('z = locals()', g, m) |
---|
86 | n/a | self.assertEqual(m.results, ('z', m)) |
---|
87 | n/a | self.assertRaises(TypeError, exec, 'z = b', m) |
---|
88 | n/a | |
---|
89 | n/a | class A: |
---|
90 | n/a | "Non-mapping" |
---|
91 | n/a | pass |
---|
92 | n/a | m = A() |
---|
93 | n/a | self.assertRaises(TypeError, exec, 'z = a', g, m) |
---|
94 | n/a | |
---|
95 | n/a | # Verify that dict subclasses work as well |
---|
96 | n/a | class D(dict): |
---|
97 | n/a | def __getitem__(self, key): |
---|
98 | n/a | if key == 'a': |
---|
99 | n/a | return 12 |
---|
100 | n/a | return dict.__getitem__(self, key) |
---|
101 | n/a | d = D() |
---|
102 | n/a | exec('z = a', g, d) |
---|
103 | n/a | self.assertEqual(d['z'], 12) |
---|
104 | n/a | |
---|
105 | n/a | def test_extended_arg(self): |
---|
106 | n/a | longexpr = 'x = x or ' + '-x' * 2500 |
---|
107 | n/a | g = {} |
---|
108 | n/a | code = ''' |
---|
109 | n/a | def f(x): |
---|
110 | n/a | %s |
---|
111 | n/a | %s |
---|
112 | n/a | %s |
---|
113 | n/a | %s |
---|
114 | n/a | %s |
---|
115 | n/a | %s |
---|
116 | n/a | %s |
---|
117 | n/a | %s |
---|
118 | n/a | %s |
---|
119 | n/a | %s |
---|
120 | n/a | # the expressions above have no effect, x == argument |
---|
121 | n/a | while x: |
---|
122 | n/a | x -= 1 |
---|
123 | n/a | # EXTENDED_ARG/JUMP_ABSOLUTE here |
---|
124 | n/a | return x |
---|
125 | n/a | ''' % ((longexpr,)*10) |
---|
126 | n/a | exec(code, g) |
---|
127 | n/a | self.assertEqual(g['f'](5), 0) |
---|
128 | n/a | |
---|
129 | n/a | def test_argument_order(self): |
---|
130 | n/a | self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass') |
---|
131 | n/a | |
---|
132 | n/a | def test_float_literals(self): |
---|
133 | n/a | # testing bad float literals |
---|
134 | n/a | self.assertRaises(SyntaxError, eval, "2e") |
---|
135 | n/a | self.assertRaises(SyntaxError, eval, "2.0e+") |
---|
136 | n/a | self.assertRaises(SyntaxError, eval, "1e-") |
---|
137 | n/a | self.assertRaises(SyntaxError, eval, "3-4e/21") |
---|
138 | n/a | |
---|
139 | n/a | def test_indentation(self): |
---|
140 | n/a | # testing compile() of indented block w/o trailing newline" |
---|
141 | n/a | s = """ |
---|
142 | n/a | if 1: |
---|
143 | n/a | if 2: |
---|
144 | n/a | pass""" |
---|
145 | n/a | compile(s, "<string>", "exec") |
---|
146 | n/a | |
---|
147 | n/a | # This test is probably specific to CPython and may not generalize |
---|
148 | n/a | # to other implementations. We are trying to ensure that when |
---|
149 | n/a | # the first line of code starts after 256, correct line numbers |
---|
150 | n/a | # in tracebacks are still produced. |
---|
151 | n/a | def test_leading_newlines(self): |
---|
152 | n/a | s256 = "".join(["\n"] * 256 + ["spam"]) |
---|
153 | n/a | co = compile(s256, 'fn', 'exec') |
---|
154 | n/a | self.assertEqual(co.co_firstlineno, 257) |
---|
155 | n/a | self.assertEqual(co.co_lnotab, bytes()) |
---|
156 | n/a | |
---|
157 | n/a | def test_literals_with_leading_zeroes(self): |
---|
158 | n/a | for arg in ["077787", "0xj", "0x.", "0e", "090000000000000", |
---|
159 | n/a | "080000000000000", "000000000000009", "000000000000008", |
---|
160 | n/a | "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2", |
---|
161 | n/a | "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777", |
---|
162 | n/a | "000777", "000000000000007"]: |
---|
163 | n/a | self.assertRaises(SyntaxError, eval, arg) |
---|
164 | n/a | |
---|
165 | n/a | self.assertEqual(eval("0xff"), 255) |
---|
166 | n/a | self.assertEqual(eval("0777."), 777) |
---|
167 | n/a | self.assertEqual(eval("0777.0"), 777) |
---|
168 | n/a | self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777) |
---|
169 | n/a | self.assertEqual(eval("0777e1"), 7770) |
---|
170 | n/a | self.assertEqual(eval("0e0"), 0) |
---|
171 | n/a | self.assertEqual(eval("0000e-012"), 0) |
---|
172 | n/a | self.assertEqual(eval("09.5"), 9.5) |
---|
173 | n/a | self.assertEqual(eval("0777j"), 777j) |
---|
174 | n/a | self.assertEqual(eval("000"), 0) |
---|
175 | n/a | self.assertEqual(eval("00j"), 0j) |
---|
176 | n/a | self.assertEqual(eval("00.0"), 0) |
---|
177 | n/a | self.assertEqual(eval("0e3"), 0) |
---|
178 | n/a | self.assertEqual(eval("090000000000000."), 90000000000000.) |
---|
179 | n/a | self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.) |
---|
180 | n/a | self.assertEqual(eval("090000000000000e0"), 90000000000000.) |
---|
181 | n/a | self.assertEqual(eval("090000000000000e-0"), 90000000000000.) |
---|
182 | n/a | self.assertEqual(eval("090000000000000j"), 90000000000000j) |
---|
183 | n/a | self.assertEqual(eval("000000000000008."), 8.) |
---|
184 | n/a | self.assertEqual(eval("000000000000009."), 9.) |
---|
185 | n/a | self.assertEqual(eval("0b101010"), 42) |
---|
186 | n/a | self.assertEqual(eval("-0b000000000010"), -2) |
---|
187 | n/a | self.assertEqual(eval("0o777"), 511) |
---|
188 | n/a | self.assertEqual(eval("-0o0000010"), -8) |
---|
189 | n/a | |
---|
190 | n/a | def test_unary_minus(self): |
---|
191 | n/a | # Verify treatment of unary minus on negative numbers SF bug #660455 |
---|
192 | n/a | if sys.maxsize == 2147483647: |
---|
193 | n/a | # 32-bit machine |
---|
194 | n/a | all_one_bits = '0xffffffff' |
---|
195 | n/a | self.assertEqual(eval(all_one_bits), 4294967295) |
---|
196 | n/a | self.assertEqual(eval("-" + all_one_bits), -4294967295) |
---|
197 | n/a | elif sys.maxsize == 9223372036854775807: |
---|
198 | n/a | # 64-bit machine |
---|
199 | n/a | all_one_bits = '0xffffffffffffffff' |
---|
200 | n/a | self.assertEqual(eval(all_one_bits), 18446744073709551615) |
---|
201 | n/a | self.assertEqual(eval("-" + all_one_bits), -18446744073709551615) |
---|
202 | n/a | else: |
---|
203 | n/a | self.fail("How many bits *does* this machine have???") |
---|
204 | n/a | # Verify treatment of constant folding on -(sys.maxsize+1) |
---|
205 | n/a | # i.e. -2147483648 on 32 bit platforms. Should return int. |
---|
206 | n/a | self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int) |
---|
207 | n/a | self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int) |
---|
208 | n/a | |
---|
209 | n/a | if sys.maxsize == 9223372036854775807: |
---|
210 | n/a | def test_32_63_bit_values(self): |
---|
211 | n/a | a = +4294967296 # 1 << 32 |
---|
212 | n/a | b = -4294967296 # 1 << 32 |
---|
213 | n/a | c = +281474976710656 # 1 << 48 |
---|
214 | n/a | d = -281474976710656 # 1 << 48 |
---|
215 | n/a | e = +4611686018427387904 # 1 << 62 |
---|
216 | n/a | f = -4611686018427387904 # 1 << 62 |
---|
217 | n/a | g = +9223372036854775807 # 1 << 63 - 1 |
---|
218 | n/a | h = -9223372036854775807 # 1 << 63 - 1 |
---|
219 | n/a | |
---|
220 | n/a | for variable in self.test_32_63_bit_values.__code__.co_consts: |
---|
221 | n/a | if variable is not None: |
---|
222 | n/a | self.assertIsInstance(variable, int) |
---|
223 | n/a | |
---|
224 | n/a | def test_sequence_unpacking_error(self): |
---|
225 | n/a | # Verify sequence packing/unpacking with "or". SF bug #757818 |
---|
226 | n/a | i,j = (1, -1) or (-1, 1) |
---|
227 | n/a | self.assertEqual(i, 1) |
---|
228 | n/a | self.assertEqual(j, -1) |
---|
229 | n/a | |
---|
230 | n/a | def test_none_assignment(self): |
---|
231 | n/a | stmts = [ |
---|
232 | n/a | 'None = 0', |
---|
233 | n/a | 'None += 0', |
---|
234 | n/a | '__builtins__.None = 0', |
---|
235 | n/a | 'def None(): pass', |
---|
236 | n/a | 'class None: pass', |
---|
237 | n/a | '(a, None) = 0, 0', |
---|
238 | n/a | 'for None in range(10): pass', |
---|
239 | n/a | 'def f(None): pass', |
---|
240 | n/a | 'import None', |
---|
241 | n/a | 'import x as None', |
---|
242 | n/a | 'from x import None', |
---|
243 | n/a | 'from x import y as None' |
---|
244 | n/a | ] |
---|
245 | n/a | for stmt in stmts: |
---|
246 | n/a | stmt += "\n" |
---|
247 | n/a | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single') |
---|
248 | n/a | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
---|
249 | n/a | |
---|
250 | n/a | def test_import(self): |
---|
251 | n/a | succeed = [ |
---|
252 | n/a | 'import sys', |
---|
253 | n/a | 'import os, sys', |
---|
254 | n/a | 'import os as bar', |
---|
255 | n/a | 'import os.path as bar', |
---|
256 | n/a | 'from __future__ import nested_scopes, generators', |
---|
257 | n/a | 'from __future__ import (nested_scopes,\ngenerators)', |
---|
258 | n/a | 'from __future__ import (nested_scopes,\ngenerators,)', |
---|
259 | n/a | 'from sys import stdin, stderr, stdout', |
---|
260 | n/a | 'from sys import (stdin, stderr,\nstdout)', |
---|
261 | n/a | 'from sys import (stdin, stderr,\nstdout,)', |
---|
262 | n/a | 'from sys import (stdin\n, stderr, stdout)', |
---|
263 | n/a | 'from sys import (stdin\n, stderr, stdout,)', |
---|
264 | n/a | 'from sys import stdin as si, stdout as so, stderr as se', |
---|
265 | n/a | 'from sys import (stdin as si, stdout as so, stderr as se)', |
---|
266 | n/a | 'from sys import (stdin as si, stdout as so, stderr as se,)', |
---|
267 | n/a | ] |
---|
268 | n/a | fail = [ |
---|
269 | n/a | 'import (os, sys)', |
---|
270 | n/a | 'import (os), (sys)', |
---|
271 | n/a | 'import ((os), (sys))', |
---|
272 | n/a | 'import (sys', |
---|
273 | n/a | 'import sys)', |
---|
274 | n/a | 'import (os,)', |
---|
275 | n/a | 'import os As bar', |
---|
276 | n/a | 'import os.path a bar', |
---|
277 | n/a | 'from sys import stdin As stdout', |
---|
278 | n/a | 'from sys import stdin a stdout', |
---|
279 | n/a | 'from (sys) import stdin', |
---|
280 | n/a | 'from __future__ import (nested_scopes', |
---|
281 | n/a | 'from __future__ import nested_scopes)', |
---|
282 | n/a | 'from __future__ import nested_scopes,\ngenerators', |
---|
283 | n/a | 'from sys import (stdin', |
---|
284 | n/a | 'from sys import stdin)', |
---|
285 | n/a | 'from sys import stdin, stdout,\nstderr', |
---|
286 | n/a | 'from sys import stdin si', |
---|
287 | n/a | 'from sys import stdin,' |
---|
288 | n/a | 'from sys import (*)', |
---|
289 | n/a | 'from sys import (stdin,, stdout, stderr)', |
---|
290 | n/a | 'from sys import (stdin, stdout),', |
---|
291 | n/a | ] |
---|
292 | n/a | for stmt in succeed: |
---|
293 | n/a | compile(stmt, 'tmp', 'exec') |
---|
294 | n/a | for stmt in fail: |
---|
295 | n/a | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
---|
296 | n/a | |
---|
297 | n/a | def test_for_distinct_code_objects(self): |
---|
298 | n/a | # SF bug 1048870 |
---|
299 | n/a | def f(): |
---|
300 | n/a | f1 = lambda x=1: x |
---|
301 | n/a | f2 = lambda x=2: x |
---|
302 | n/a | return f1, f2 |
---|
303 | n/a | f1, f2 = f() |
---|
304 | n/a | self.assertNotEqual(id(f1.__code__), id(f2.__code__)) |
---|
305 | n/a | |
---|
306 | n/a | def test_lambda_doc(self): |
---|
307 | n/a | l = lambda: "foo" |
---|
308 | n/a | self.assertIsNone(l.__doc__) |
---|
309 | n/a | |
---|
310 | n/a | def test_encoding(self): |
---|
311 | n/a | code = b'# -*- coding: badencoding -*-\npass\n' |
---|
312 | n/a | self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec') |
---|
313 | n/a | code = '# -*- coding: badencoding -*-\n"\xc2\xa4"\n' |
---|
314 | n/a | compile(code, 'tmp', 'exec') |
---|
315 | n/a | self.assertEqual(eval(code), '\xc2\xa4') |
---|
316 | n/a | code = '"\xc2\xa4"\n' |
---|
317 | n/a | self.assertEqual(eval(code), '\xc2\xa4') |
---|
318 | n/a | code = b'"\xc2\xa4"\n' |
---|
319 | n/a | self.assertEqual(eval(code), '\xa4') |
---|
320 | n/a | code = b'# -*- coding: latin1 -*-\n"\xc2\xa4"\n' |
---|
321 | n/a | self.assertEqual(eval(code), '\xc2\xa4') |
---|
322 | n/a | code = b'# -*- coding: utf-8 -*-\n"\xc2\xa4"\n' |
---|
323 | n/a | self.assertEqual(eval(code), '\xa4') |
---|
324 | n/a | code = b'# -*- coding: iso8859-15 -*-\n"\xc2\xa4"\n' |
---|
325 | n/a | self.assertEqual(eval(code), '\xc2\u20ac') |
---|
326 | n/a | code = '"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n' |
---|
327 | n/a | self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xc2\xa4') |
---|
328 | n/a | code = b'"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n' |
---|
329 | n/a | self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xa4') |
---|
330 | n/a | |
---|
331 | n/a | def test_subscripts(self): |
---|
332 | n/a | # SF bug 1448804 |
---|
333 | n/a | # Class to make testing subscript results easy |
---|
334 | n/a | class str_map(object): |
---|
335 | n/a | def __init__(self): |
---|
336 | n/a | self.data = {} |
---|
337 | n/a | def __getitem__(self, key): |
---|
338 | n/a | return self.data[str(key)] |
---|
339 | n/a | def __setitem__(self, key, value): |
---|
340 | n/a | self.data[str(key)] = value |
---|
341 | n/a | def __delitem__(self, key): |
---|
342 | n/a | del self.data[str(key)] |
---|
343 | n/a | def __contains__(self, key): |
---|
344 | n/a | return str(key) in self.data |
---|
345 | n/a | d = str_map() |
---|
346 | n/a | # Index |
---|
347 | n/a | d[1] = 1 |
---|
348 | n/a | self.assertEqual(d[1], 1) |
---|
349 | n/a | d[1] += 1 |
---|
350 | n/a | self.assertEqual(d[1], 2) |
---|
351 | n/a | del d[1] |
---|
352 | n/a | self.assertNotIn(1, d) |
---|
353 | n/a | # Tuple of indices |
---|
354 | n/a | d[1, 1] = 1 |
---|
355 | n/a | self.assertEqual(d[1, 1], 1) |
---|
356 | n/a | d[1, 1] += 1 |
---|
357 | n/a | self.assertEqual(d[1, 1], 2) |
---|
358 | n/a | del d[1, 1] |
---|
359 | n/a | self.assertNotIn((1, 1), d) |
---|
360 | n/a | # Simple slice |
---|
361 | n/a | d[1:2] = 1 |
---|
362 | n/a | self.assertEqual(d[1:2], 1) |
---|
363 | n/a | d[1:2] += 1 |
---|
364 | n/a | self.assertEqual(d[1:2], 2) |
---|
365 | n/a | del d[1:2] |
---|
366 | n/a | self.assertNotIn(slice(1, 2), d) |
---|
367 | n/a | # Tuple of simple slices |
---|
368 | n/a | d[1:2, 1:2] = 1 |
---|
369 | n/a | self.assertEqual(d[1:2, 1:2], 1) |
---|
370 | n/a | d[1:2, 1:2] += 1 |
---|
371 | n/a | self.assertEqual(d[1:2, 1:2], 2) |
---|
372 | n/a | del d[1:2, 1:2] |
---|
373 | n/a | self.assertNotIn((slice(1, 2), slice(1, 2)), d) |
---|
374 | n/a | # Extended slice |
---|
375 | n/a | d[1:2:3] = 1 |
---|
376 | n/a | self.assertEqual(d[1:2:3], 1) |
---|
377 | n/a | d[1:2:3] += 1 |
---|
378 | n/a | self.assertEqual(d[1:2:3], 2) |
---|
379 | n/a | del d[1:2:3] |
---|
380 | n/a | self.assertNotIn(slice(1, 2, 3), d) |
---|
381 | n/a | # Tuple of extended slices |
---|
382 | n/a | d[1:2:3, 1:2:3] = 1 |
---|
383 | n/a | self.assertEqual(d[1:2:3, 1:2:3], 1) |
---|
384 | n/a | d[1:2:3, 1:2:3] += 1 |
---|
385 | n/a | self.assertEqual(d[1:2:3, 1:2:3], 2) |
---|
386 | n/a | del d[1:2:3, 1:2:3] |
---|
387 | n/a | self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d) |
---|
388 | n/a | # Ellipsis |
---|
389 | n/a | d[...] = 1 |
---|
390 | n/a | self.assertEqual(d[...], 1) |
---|
391 | n/a | d[...] += 1 |
---|
392 | n/a | self.assertEqual(d[...], 2) |
---|
393 | n/a | del d[...] |
---|
394 | n/a | self.assertNotIn(Ellipsis, d) |
---|
395 | n/a | # Tuple of Ellipses |
---|
396 | n/a | d[..., ...] = 1 |
---|
397 | n/a | self.assertEqual(d[..., ...], 1) |
---|
398 | n/a | d[..., ...] += 1 |
---|
399 | n/a | self.assertEqual(d[..., ...], 2) |
---|
400 | n/a | del d[..., ...] |
---|
401 | n/a | self.assertNotIn((Ellipsis, Ellipsis), d) |
---|
402 | n/a | |
---|
403 | n/a | def test_annotation_limit(self): |
---|
404 | n/a | # more than 255 annotations, should compile ok |
---|
405 | n/a | s = "def f(%s): pass" |
---|
406 | n/a | s %= ', '.join('a%d:%d' % (i,i) for i in range(300)) |
---|
407 | n/a | compile(s, '?', 'exec') |
---|
408 | n/a | |
---|
409 | n/a | def test_mangling(self): |
---|
410 | n/a | class A: |
---|
411 | n/a | def f(): |
---|
412 | n/a | __mangled = 1 |
---|
413 | n/a | __not_mangled__ = 2 |
---|
414 | n/a | import __mangled_mod |
---|
415 | n/a | import __package__.module |
---|
416 | n/a | |
---|
417 | n/a | self.assertIn("_A__mangled", A.f.__code__.co_varnames) |
---|
418 | n/a | self.assertIn("__not_mangled__", A.f.__code__.co_varnames) |
---|
419 | n/a | self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames) |
---|
420 | n/a | self.assertIn("__package__", A.f.__code__.co_varnames) |
---|
421 | n/a | |
---|
422 | n/a | def test_compile_ast(self): |
---|
423 | n/a | fname = __file__ |
---|
424 | n/a | if fname.lower().endswith('pyc'): |
---|
425 | n/a | fname = fname[:-1] |
---|
426 | n/a | with open(fname, 'r') as f: |
---|
427 | n/a | fcontents = f.read() |
---|
428 | n/a | sample_code = [ |
---|
429 | n/a | ['<assign>', 'x = 5'], |
---|
430 | n/a | ['<ifblock>', """if True:\n pass\n"""], |
---|
431 | n/a | ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], |
---|
432 | n/a | ['<deffunc>', """def foo():\n pass\nfoo()\n"""], |
---|
433 | n/a | [fname, fcontents], |
---|
434 | n/a | ] |
---|
435 | n/a | |
---|
436 | n/a | for fname, code in sample_code: |
---|
437 | n/a | co1 = compile(code, '%s1' % fname, 'exec') |
---|
438 | n/a | ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) |
---|
439 | n/a | self.assertTrue(type(ast) == _ast.Module) |
---|
440 | n/a | co2 = compile(ast, '%s3' % fname, 'exec') |
---|
441 | n/a | self.assertEqual(co1, co2) |
---|
442 | n/a | # the code object's filename comes from the second compilation step |
---|
443 | n/a | self.assertEqual(co2.co_filename, '%s3' % fname) |
---|
444 | n/a | |
---|
445 | n/a | # raise exception when node type doesn't match with compile mode |
---|
446 | n/a | co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) |
---|
447 | n/a | self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') |
---|
448 | n/a | |
---|
449 | n/a | # raise exception when node type is no start node |
---|
450 | n/a | self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') |
---|
451 | n/a | |
---|
452 | n/a | # raise exception when node has invalid children |
---|
453 | n/a | ast = _ast.Module() |
---|
454 | n/a | ast.body = [_ast.BoolOp()] |
---|
455 | n/a | self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') |
---|
456 | n/a | |
---|
457 | n/a | def test_dict_evaluation_order(self): |
---|
458 | n/a | i = 0 |
---|
459 | n/a | |
---|
460 | n/a | def f(): |
---|
461 | n/a | nonlocal i |
---|
462 | n/a | i += 1 |
---|
463 | n/a | return i |
---|
464 | n/a | |
---|
465 | n/a | d = {f(): f(), f(): f()} |
---|
466 | n/a | self.assertEqual(d, {1: 2, 3: 4}) |
---|
467 | n/a | |
---|
468 | n/a | def test_compile_filename(self): |
---|
469 | n/a | for filename in 'file.py', b'file.py': |
---|
470 | n/a | code = compile('pass', filename, 'exec') |
---|
471 | n/a | self.assertEqual(code.co_filename, 'file.py') |
---|
472 | n/a | for filename in bytearray(b'file.py'), memoryview(b'file.py'): |
---|
473 | n/a | with self.assertWarns(DeprecationWarning): |
---|
474 | n/a | code = compile('pass', filename, 'exec') |
---|
475 | n/a | self.assertEqual(code.co_filename, 'file.py') |
---|
476 | n/a | self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec') |
---|
477 | n/a | |
---|
478 | n/a | @support.cpython_only |
---|
479 | n/a | def test_same_filename_used(self): |
---|
480 | n/a | s = """def f(): pass\ndef g(): pass""" |
---|
481 | n/a | c = compile(s, "myfile", "exec") |
---|
482 | n/a | for obj in c.co_consts: |
---|
483 | n/a | if isinstance(obj, types.CodeType): |
---|
484 | n/a | self.assertIs(obj.co_filename, c.co_filename) |
---|
485 | n/a | |
---|
486 | n/a | def test_single_statement(self): |
---|
487 | n/a | self.compile_single("1 + 2") |
---|
488 | n/a | self.compile_single("\n1 + 2") |
---|
489 | n/a | self.compile_single("1 + 2\n") |
---|
490 | n/a | self.compile_single("1 + 2\n\n") |
---|
491 | n/a | self.compile_single("1 + 2\t\t\n") |
---|
492 | n/a | self.compile_single("1 + 2\t\t\n ") |
---|
493 | n/a | self.compile_single("1 + 2 # one plus two") |
---|
494 | n/a | self.compile_single("1; 2") |
---|
495 | n/a | self.compile_single("import sys; sys") |
---|
496 | n/a | self.compile_single("def f():\n pass") |
---|
497 | n/a | self.compile_single("while False:\n pass") |
---|
498 | n/a | self.compile_single("if x:\n f(x)") |
---|
499 | n/a | self.compile_single("if x:\n f(x)\nelse:\n g(x)") |
---|
500 | n/a | self.compile_single("class T:\n pass") |
---|
501 | n/a | |
---|
502 | n/a | def test_bad_single_statement(self): |
---|
503 | n/a | self.assertInvalidSingle('1\n2') |
---|
504 | n/a | self.assertInvalidSingle('def f(): pass') |
---|
505 | n/a | self.assertInvalidSingle('a = 13\nb = 187') |
---|
506 | n/a | self.assertInvalidSingle('del x\ndel y') |
---|
507 | n/a | self.assertInvalidSingle('f()\ng()') |
---|
508 | n/a | self.assertInvalidSingle('f()\n# blah\nblah()') |
---|
509 | n/a | self.assertInvalidSingle('f()\nxy # blah\nblah()') |
---|
510 | n/a | self.assertInvalidSingle('x = 5 # comment\nx = 6\n') |
---|
511 | n/a | |
---|
512 | n/a | def test_particularly_evil_undecodable(self): |
---|
513 | n/a | # Issue 24022 |
---|
514 | n/a | src = b'0000\x00\n00000000000\n\x00\n\x9e\n' |
---|
515 | n/a | with tempfile.TemporaryDirectory() as tmpd: |
---|
516 | n/a | fn = os.path.join(tmpd, "bad.py") |
---|
517 | n/a | with open(fn, "wb") as fp: |
---|
518 | n/a | fp.write(src) |
---|
519 | n/a | res = script_helper.run_python_until_end(fn)[0] |
---|
520 | n/a | self.assertIn(b"Non-UTF-8", res.err) |
---|
521 | n/a | |
---|
522 | n/a | def test_yet_more_evil_still_undecodable(self): |
---|
523 | n/a | # Issue #25388 |
---|
524 | n/a | src = b"#\x00\n#\xfd\n" |
---|
525 | n/a | with tempfile.TemporaryDirectory() as tmpd: |
---|
526 | n/a | fn = os.path.join(tmpd, "bad.py") |
---|
527 | n/a | with open(fn, "wb") as fp: |
---|
528 | n/a | fp.write(src) |
---|
529 | n/a | res = script_helper.run_python_until_end(fn)[0] |
---|
530 | n/a | self.assertIn(b"Non-UTF-8", res.err) |
---|
531 | n/a | |
---|
532 | n/a | @support.cpython_only |
---|
533 | n/a | def test_compiler_recursion_limit(self): |
---|
534 | n/a | # Expected limit is sys.getrecursionlimit() * the scaling factor |
---|
535 | n/a | # in symtable.c (currently 3) |
---|
536 | n/a | # We expect to fail *at* that limit, because we use up some of |
---|
537 | n/a | # the stack depth limit in the test suite code |
---|
538 | n/a | # So we check the expected limit and 75% of that |
---|
539 | n/a | # XXX (ncoghlan): duplicating the scaling factor here is a little |
---|
540 | n/a | # ugly. Perhaps it should be exposed somewhere... |
---|
541 | n/a | fail_depth = sys.getrecursionlimit() * 3 |
---|
542 | n/a | success_depth = int(fail_depth * 0.75) |
---|
543 | n/a | |
---|
544 | n/a | def check_limit(prefix, repeated): |
---|
545 | n/a | expect_ok = prefix + repeated * success_depth |
---|
546 | n/a | self.compile_single(expect_ok) |
---|
547 | n/a | broken = prefix + repeated * fail_depth |
---|
548 | n/a | details = "Compiling ({!r} + {!r} * {})".format( |
---|
549 | n/a | prefix, repeated, fail_depth) |
---|
550 | n/a | with self.assertRaises(RecursionError, msg=details): |
---|
551 | n/a | self.compile_single(broken) |
---|
552 | n/a | |
---|
553 | n/a | check_limit("a", "()") |
---|
554 | n/a | check_limit("a", ".b") |
---|
555 | n/a | check_limit("a", "[0]") |
---|
556 | n/a | check_limit("a", "*a") |
---|
557 | n/a | |
---|
558 | n/a | def test_null_terminated(self): |
---|
559 | n/a | # The source code is null-terminated internally, but bytes-like |
---|
560 | n/a | # objects are accepted, which could be not terminated. |
---|
561 | n/a | with self.assertRaisesRegex(ValueError, "cannot contain null"): |
---|
562 | n/a | compile("123\x00", "<dummy>", "eval") |
---|
563 | n/a | with self.assertRaisesRegex(ValueError, "cannot contain null"): |
---|
564 | n/a | compile(memoryview(b"123\x00"), "<dummy>", "eval") |
---|
565 | n/a | code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval") |
---|
566 | n/a | self.assertEqual(eval(code), 23) |
---|
567 | n/a | code = compile(memoryview(b"1234")[1:-1], "<dummy>", "eval") |
---|
568 | n/a | self.assertEqual(eval(code), 23) |
---|
569 | n/a | code = compile(memoryview(b"$23$")[1:-1], "<dummy>", "eval") |
---|
570 | n/a | self.assertEqual(eval(code), 23) |
---|
571 | n/a | |
---|
572 | n/a | # Also test when eval() and exec() do the compilation step |
---|
573 | n/a | self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23) |
---|
574 | n/a | namespace = dict() |
---|
575 | n/a | exec(memoryview(b"ax = 123")[1:-1], namespace) |
---|
576 | n/a | self.assertEqual(namespace['x'], 12) |
---|
577 | n/a | |
---|
578 | n/a | def check_constant(self, func, expected): |
---|
579 | n/a | for const in func.__code__.co_consts: |
---|
580 | n/a | if repr(const) == repr(expected): |
---|
581 | n/a | break |
---|
582 | n/a | else: |
---|
583 | n/a | self.fail("unable to find constant %r in %r" |
---|
584 | n/a | % (expected, func.__code__.co_consts)) |
---|
585 | n/a | |
---|
586 | n/a | # Merging equal constants is not a strict requirement for the Python |
---|
587 | n/a | # semantics, it's a more an implementation detail. |
---|
588 | n/a | @support.cpython_only |
---|
589 | n/a | def test_merge_constants(self): |
---|
590 | n/a | # Issue #25843: compile() must merge constants which are equal |
---|
591 | n/a | # and have the same type. |
---|
592 | n/a | |
---|
593 | n/a | def check_same_constant(const): |
---|
594 | n/a | ns = {} |
---|
595 | n/a | code = "f1, f2 = lambda: %r, lambda: %r" % (const, const) |
---|
596 | n/a | exec(code, ns) |
---|
597 | n/a | f1 = ns['f1'] |
---|
598 | n/a | f2 = ns['f2'] |
---|
599 | n/a | self.assertIs(f1.__code__, f2.__code__) |
---|
600 | n/a | self.check_constant(f1, const) |
---|
601 | n/a | self.assertEqual(repr(f1()), repr(const)) |
---|
602 | n/a | |
---|
603 | n/a | check_same_constant(None) |
---|
604 | n/a | check_same_constant(0) |
---|
605 | n/a | check_same_constant(0.0) |
---|
606 | n/a | check_same_constant(b'abc') |
---|
607 | n/a | check_same_constant('abc') |
---|
608 | n/a | |
---|
609 | n/a | # Note: "lambda: ..." emits "LOAD_CONST Ellipsis", |
---|
610 | n/a | # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis" |
---|
611 | n/a | f1, f2 = lambda: ..., lambda: ... |
---|
612 | n/a | self.assertIs(f1.__code__, f2.__code__) |
---|
613 | n/a | self.check_constant(f1, Ellipsis) |
---|
614 | n/a | self.assertEqual(repr(f1()), repr(Ellipsis)) |
---|
615 | n/a | |
---|
616 | n/a | # {0} is converted to a constant frozenset({0}) by the peephole |
---|
617 | n/a | # optimizer |
---|
618 | n/a | f1, f2 = lambda x: x in {0}, lambda x: x in {0} |
---|
619 | n/a | self.assertIs(f1.__code__, f2.__code__) |
---|
620 | n/a | self.check_constant(f1, frozenset({0})) |
---|
621 | n/a | self.assertTrue(f1(0)) |
---|
622 | n/a | |
---|
623 | n/a | def test_dont_merge_constants(self): |
---|
624 | n/a | # Issue #25843: compile() must not merge constants which are equal |
---|
625 | n/a | # but have a different type. |
---|
626 | n/a | |
---|
627 | n/a | def check_different_constants(const1, const2): |
---|
628 | n/a | ns = {} |
---|
629 | n/a | exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns) |
---|
630 | n/a | f1 = ns['f1'] |
---|
631 | n/a | f2 = ns['f2'] |
---|
632 | n/a | self.assertIsNot(f1.__code__, f2.__code__) |
---|
633 | n/a | self.assertNotEqual(f1.__code__, f2.__code__) |
---|
634 | n/a | self.check_constant(f1, const1) |
---|
635 | n/a | self.check_constant(f2, const2) |
---|
636 | n/a | self.assertEqual(repr(f1()), repr(const1)) |
---|
637 | n/a | self.assertEqual(repr(f2()), repr(const2)) |
---|
638 | n/a | |
---|
639 | n/a | check_different_constants(0, 0.0) |
---|
640 | n/a | check_different_constants(+0.0, -0.0) |
---|
641 | n/a | check_different_constants((0,), (0.0,)) |
---|
642 | n/a | check_different_constants('a', b'a') |
---|
643 | n/a | check_different_constants(('a',), (b'a',)) |
---|
644 | n/a | |
---|
645 | n/a | # check_different_constants() cannot be used because repr(-0j) is |
---|
646 | n/a | # '(-0-0j)', but when '(-0-0j)' is evaluated to 0j: we loose the sign. |
---|
647 | n/a | f1, f2 = lambda: +0.0j, lambda: -0.0j |
---|
648 | n/a | self.assertIsNot(f1.__code__, f2.__code__) |
---|
649 | n/a | self.check_constant(f1, +0.0j) |
---|
650 | n/a | self.check_constant(f2, -0.0j) |
---|
651 | n/a | self.assertEqual(repr(f1()), repr(+0.0j)) |
---|
652 | n/a | self.assertEqual(repr(f2()), repr(-0.0j)) |
---|
653 | n/a | |
---|
654 | n/a | # {0} is converted to a constant frozenset({0}) by the peephole |
---|
655 | n/a | # optimizer |
---|
656 | n/a | f1, f2 = lambda x: x in {0}, lambda x: x in {0.0} |
---|
657 | n/a | self.assertIsNot(f1.__code__, f2.__code__) |
---|
658 | n/a | self.check_constant(f1, frozenset({0})) |
---|
659 | n/a | self.check_constant(f2, frozenset({0.0})) |
---|
660 | n/a | self.assertTrue(f1(0)) |
---|
661 | n/a | self.assertTrue(f2(0.0)) |
---|
662 | n/a | |
---|
663 | n/a | def test_path_like_objects(self): |
---|
664 | n/a | # An implicit test for PyUnicode_FSDecoder(). |
---|
665 | n/a | class PathLike: |
---|
666 | n/a | def __init__(self, path): |
---|
667 | n/a | self._path = path |
---|
668 | n/a | def __fspath__(self): |
---|
669 | n/a | return self._path |
---|
670 | n/a | |
---|
671 | n/a | compile("42", PathLike("test_compile_pathlike"), "single") |
---|
672 | n/a | |
---|
673 | n/a | |
---|
674 | n/a | class TestStackSize(unittest.TestCase): |
---|
675 | n/a | # These tests check that the computed stack size for a code object |
---|
676 | n/a | # stays within reasonable bounds (see issue #21523 for an example |
---|
677 | n/a | # dysfunction). |
---|
678 | n/a | N = 100 |
---|
679 | n/a | |
---|
680 | n/a | def check_stack_size(self, code): |
---|
681 | n/a | # To assert that the alleged stack size is not O(N), we |
---|
682 | n/a | # check that it is smaller than log(N). |
---|
683 | n/a | if isinstance(code, str): |
---|
684 | n/a | code = compile(code, "<foo>", "single") |
---|
685 | n/a | max_size = math.ceil(math.log(len(code.co_code))) |
---|
686 | n/a | self.assertLessEqual(code.co_stacksize, max_size) |
---|
687 | n/a | |
---|
688 | n/a | def test_and(self): |
---|
689 | n/a | self.check_stack_size("x and " * self.N + "x") |
---|
690 | n/a | |
---|
691 | n/a | def test_or(self): |
---|
692 | n/a | self.check_stack_size("x or " * self.N + "x") |
---|
693 | n/a | |
---|
694 | n/a | def test_and_or(self): |
---|
695 | n/a | self.check_stack_size("x and x or " * self.N + "x") |
---|
696 | n/a | |
---|
697 | n/a | def test_chained_comparison(self): |
---|
698 | n/a | self.check_stack_size("x < " * self.N + "x") |
---|
699 | n/a | |
---|
700 | n/a | def test_if_else(self): |
---|
701 | n/a | self.check_stack_size("x if x else " * self.N + "x") |
---|
702 | n/a | |
---|
703 | n/a | def test_binop(self): |
---|
704 | n/a | self.check_stack_size("x + " * self.N + "x") |
---|
705 | n/a | |
---|
706 | n/a | def test_func_and(self): |
---|
707 | n/a | code = "def f(x):\n" |
---|
708 | n/a | code += " x and x\n" * self.N |
---|
709 | n/a | self.check_stack_size(code) |
---|
710 | n/a | |
---|
711 | n/a | |
---|
712 | n/a | if __name__ == "__main__": |
---|
713 | n/a | unittest.main() |
---|