1 | n/a | # Python test set -- part 1, grammar. |
---|
2 | n/a | # This just tests whether the parser accepts them all. |
---|
3 | n/a | |
---|
4 | n/a | from test.support import check_syntax_error |
---|
5 | n/a | import inspect |
---|
6 | n/a | import unittest |
---|
7 | n/a | import sys |
---|
8 | n/a | # testing import * |
---|
9 | n/a | from sys import * |
---|
10 | n/a | |
---|
11 | n/a | # different import patterns to check that __annotations__ does not interfere |
---|
12 | n/a | # with import machinery |
---|
13 | n/a | import test.ann_module as ann_module |
---|
14 | n/a | import typing |
---|
15 | n/a | from collections import ChainMap |
---|
16 | n/a | from test import ann_module2 |
---|
17 | n/a | import test |
---|
18 | n/a | |
---|
19 | n/a | # These are shared with test_tokenize and other test modules. |
---|
20 | n/a | # |
---|
21 | n/a | # Note: since several test cases filter out floats by looking for "e" and ".", |
---|
22 | n/a | # don't add hexadecimal literals that contain "e" or "E". |
---|
23 | n/a | VALID_UNDERSCORE_LITERALS = [ |
---|
24 | n/a | '0_0_0', |
---|
25 | n/a | '4_2', |
---|
26 | n/a | '1_0000_0000', |
---|
27 | n/a | '0b1001_0100', |
---|
28 | n/a | '0xffff_ffff', |
---|
29 | n/a | '0o5_7_7', |
---|
30 | n/a | '1_00_00.5', |
---|
31 | n/a | '1_00_00.5e5', |
---|
32 | n/a | '1_00_00e5_1', |
---|
33 | n/a | '1e1_0', |
---|
34 | n/a | '.1_4', |
---|
35 | n/a | '.1_4e1', |
---|
36 | n/a | '0b_0', |
---|
37 | n/a | '0x_f', |
---|
38 | n/a | '0o_5', |
---|
39 | n/a | '1_00_00j', |
---|
40 | n/a | '1_00_00.5j', |
---|
41 | n/a | '1_00_00e5_1j', |
---|
42 | n/a | '.1_4j', |
---|
43 | n/a | '(1_2.5+3_3j)', |
---|
44 | n/a | '(.5_6j)', |
---|
45 | n/a | ] |
---|
46 | n/a | INVALID_UNDERSCORE_LITERALS = [ |
---|
47 | n/a | # Trailing underscores: |
---|
48 | n/a | '0_', |
---|
49 | n/a | '42_', |
---|
50 | n/a | '1.4j_', |
---|
51 | n/a | '0x_', |
---|
52 | n/a | '0b1_', |
---|
53 | n/a | '0xf_', |
---|
54 | n/a | '0o5_', |
---|
55 | n/a | '0 if 1_Else 1', |
---|
56 | n/a | # Underscores in the base selector: |
---|
57 | n/a | '0_b0', |
---|
58 | n/a | '0_xf', |
---|
59 | n/a | '0_o5', |
---|
60 | n/a | # Old-style octal, still disallowed: |
---|
61 | n/a | '0_7', |
---|
62 | n/a | '09_99', |
---|
63 | n/a | # Multiple consecutive underscores: |
---|
64 | n/a | '4_______2', |
---|
65 | n/a | '0.1__4', |
---|
66 | n/a | '0.1__4j', |
---|
67 | n/a | '0b1001__0100', |
---|
68 | n/a | '0xffff__ffff', |
---|
69 | n/a | '0x___', |
---|
70 | n/a | '0o5__77', |
---|
71 | n/a | '1e1__0', |
---|
72 | n/a | '1e1__0j', |
---|
73 | n/a | # Underscore right before a dot: |
---|
74 | n/a | '1_.4', |
---|
75 | n/a | '1_.4j', |
---|
76 | n/a | # Underscore right after a dot: |
---|
77 | n/a | '1._4', |
---|
78 | n/a | '1._4j', |
---|
79 | n/a | '._5', |
---|
80 | n/a | '._5j', |
---|
81 | n/a | # Underscore right after a sign: |
---|
82 | n/a | '1.0e+_1', |
---|
83 | n/a | '1.0e+_1j', |
---|
84 | n/a | # Underscore right before j: |
---|
85 | n/a | '1.4_j', |
---|
86 | n/a | '1.4e5_j', |
---|
87 | n/a | # Underscore right before e: |
---|
88 | n/a | '1_e1', |
---|
89 | n/a | '1.4_e1', |
---|
90 | n/a | '1.4_e1j', |
---|
91 | n/a | # Underscore right after e: |
---|
92 | n/a | '1e_1', |
---|
93 | n/a | '1.4e_1', |
---|
94 | n/a | '1.4e_1j', |
---|
95 | n/a | # Complex cases with parens: |
---|
96 | n/a | '(1+1.5_j_)', |
---|
97 | n/a | '(1+1.5_j)', |
---|
98 | n/a | ] |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | class TokenTests(unittest.TestCase): |
---|
102 | n/a | |
---|
103 | n/a | def test_backslash(self): |
---|
104 | n/a | # Backslash means line continuation: |
---|
105 | n/a | x = 1 \ |
---|
106 | n/a | + 1 |
---|
107 | n/a | self.assertEqual(x, 2, 'backslash for line continuation') |
---|
108 | n/a | |
---|
109 | n/a | # Backslash does not means continuation in comments :\ |
---|
110 | n/a | x = 0 |
---|
111 | n/a | self.assertEqual(x, 0, 'backslash ending comment') |
---|
112 | n/a | |
---|
113 | n/a | def test_plain_integers(self): |
---|
114 | n/a | self.assertEqual(type(000), type(0)) |
---|
115 | n/a | self.assertEqual(0xff, 255) |
---|
116 | n/a | self.assertEqual(0o377, 255) |
---|
117 | n/a | self.assertEqual(2147483647, 0o17777777777) |
---|
118 | n/a | self.assertEqual(0b1001, 9) |
---|
119 | n/a | # "0x" is not a valid literal |
---|
120 | n/a | self.assertRaises(SyntaxError, eval, "0x") |
---|
121 | n/a | from sys import maxsize |
---|
122 | n/a | if maxsize == 2147483647: |
---|
123 | n/a | self.assertEqual(-2147483647-1, -0o20000000000) |
---|
124 | n/a | # XXX -2147483648 |
---|
125 | n/a | self.assertTrue(0o37777777777 > 0) |
---|
126 | n/a | self.assertTrue(0xffffffff > 0) |
---|
127 | n/a | self.assertTrue(0b1111111111111111111111111111111 > 0) |
---|
128 | n/a | for s in ('2147483648', '0o40000000000', '0x100000000', |
---|
129 | n/a | '0b10000000000000000000000000000000'): |
---|
130 | n/a | try: |
---|
131 | n/a | x = eval(s) |
---|
132 | n/a | except OverflowError: |
---|
133 | n/a | self.fail("OverflowError on huge integer literal %r" % s) |
---|
134 | n/a | elif maxsize == 9223372036854775807: |
---|
135 | n/a | self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
---|
136 | n/a | self.assertTrue(0o1777777777777777777777 > 0) |
---|
137 | n/a | self.assertTrue(0xffffffffffffffff > 0) |
---|
138 | n/a | self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
---|
139 | n/a | for s in '9223372036854775808', '0o2000000000000000000000', \ |
---|
140 | n/a | '0x10000000000000000', \ |
---|
141 | n/a | '0b100000000000000000000000000000000000000000000000000000000000000': |
---|
142 | n/a | try: |
---|
143 | n/a | x = eval(s) |
---|
144 | n/a | except OverflowError: |
---|
145 | n/a | self.fail("OverflowError on huge integer literal %r" % s) |
---|
146 | n/a | else: |
---|
147 | n/a | self.fail('Weird maxsize value %r' % maxsize) |
---|
148 | n/a | |
---|
149 | n/a | def test_long_integers(self): |
---|
150 | n/a | x = 0 |
---|
151 | n/a | x = 0xffffffffffffffff |
---|
152 | n/a | x = 0Xffffffffffffffff |
---|
153 | n/a | x = 0o77777777777777777 |
---|
154 | n/a | x = 0O77777777777777777 |
---|
155 | n/a | x = 123456789012345678901234567890 |
---|
156 | n/a | x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |
---|
157 | n/a | x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |
---|
158 | n/a | |
---|
159 | n/a | def test_floats(self): |
---|
160 | n/a | x = 3.14 |
---|
161 | n/a | x = 314. |
---|
162 | n/a | x = 0.314 |
---|
163 | n/a | # XXX x = 000.314 |
---|
164 | n/a | x = .314 |
---|
165 | n/a | x = 3e14 |
---|
166 | n/a | x = 3E14 |
---|
167 | n/a | x = 3e-14 |
---|
168 | n/a | x = 3e+14 |
---|
169 | n/a | x = 3.e14 |
---|
170 | n/a | x = .3e14 |
---|
171 | n/a | x = 3.1e4 |
---|
172 | n/a | |
---|
173 | n/a | def test_float_exponent_tokenization(self): |
---|
174 | n/a | # See issue 21642. |
---|
175 | n/a | self.assertEqual(1 if 1else 0, 1) |
---|
176 | n/a | self.assertEqual(1 if 0else 0, 0) |
---|
177 | n/a | self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
---|
178 | n/a | |
---|
179 | n/a | def test_underscore_literals(self): |
---|
180 | n/a | for lit in VALID_UNDERSCORE_LITERALS: |
---|
181 | n/a | self.assertEqual(eval(lit), eval(lit.replace('_', ''))) |
---|
182 | n/a | for lit in INVALID_UNDERSCORE_LITERALS: |
---|
183 | n/a | self.assertRaises(SyntaxError, eval, lit) |
---|
184 | n/a | # Sanity check: no literal begins with an underscore |
---|
185 | n/a | self.assertRaises(NameError, eval, "_0") |
---|
186 | n/a | |
---|
187 | n/a | def test_string_literals(self): |
---|
188 | n/a | x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
---|
189 | n/a | x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
---|
190 | n/a | x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) |
---|
191 | n/a | x = "doesn't \"shrink\" does it" |
---|
192 | n/a | y = 'doesn\'t "shrink" does it' |
---|
193 | n/a | self.assertTrue(len(x) == 24 and x == y) |
---|
194 | n/a | x = "does \"shrink\" doesn't it" |
---|
195 | n/a | y = 'does "shrink" doesn\'t it' |
---|
196 | n/a | self.assertTrue(len(x) == 24 and x == y) |
---|
197 | n/a | x = """ |
---|
198 | n/a | The "quick" |
---|
199 | n/a | brown fox |
---|
200 | n/a | jumps over |
---|
201 | n/a | the 'lazy' dog. |
---|
202 | n/a | """ |
---|
203 | n/a | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
---|
204 | n/a | self.assertEqual(x, y) |
---|
205 | n/a | y = ''' |
---|
206 | n/a | The "quick" |
---|
207 | n/a | brown fox |
---|
208 | n/a | jumps over |
---|
209 | n/a | the 'lazy' dog. |
---|
210 | n/a | ''' |
---|
211 | n/a | self.assertEqual(x, y) |
---|
212 | n/a | y = "\n\ |
---|
213 | n/a | The \"quick\"\n\ |
---|
214 | n/a | brown fox\n\ |
---|
215 | n/a | jumps over\n\ |
---|
216 | n/a | the 'lazy' dog.\n\ |
---|
217 | n/a | " |
---|
218 | n/a | self.assertEqual(x, y) |
---|
219 | n/a | y = '\n\ |
---|
220 | n/a | The \"quick\"\n\ |
---|
221 | n/a | brown fox\n\ |
---|
222 | n/a | jumps over\n\ |
---|
223 | n/a | the \'lazy\' dog.\n\ |
---|
224 | n/a | ' |
---|
225 | n/a | self.assertEqual(x, y) |
---|
226 | n/a | |
---|
227 | n/a | def test_ellipsis(self): |
---|
228 | n/a | x = ... |
---|
229 | n/a | self.assertTrue(x is Ellipsis) |
---|
230 | n/a | self.assertRaises(SyntaxError, eval, ".. .") |
---|
231 | n/a | |
---|
232 | n/a | def test_eof_error(self): |
---|
233 | n/a | samples = ("def foo(", "\ndef foo(", "def foo(\n") |
---|
234 | n/a | for s in samples: |
---|
235 | n/a | with self.assertRaises(SyntaxError) as cm: |
---|
236 | n/a | compile(s, "<test>", "exec") |
---|
237 | n/a | self.assertIn("unexpected EOF", str(cm.exception)) |
---|
238 | n/a | |
---|
239 | n/a | var_annot_global: int # a global annotated is necessary for test_var_annot |
---|
240 | n/a | |
---|
241 | n/a | # custom namespace for testing __annotations__ |
---|
242 | n/a | |
---|
243 | n/a | class CNS: |
---|
244 | n/a | def __init__(self): |
---|
245 | n/a | self._dct = {} |
---|
246 | n/a | def __setitem__(self, item, value): |
---|
247 | n/a | self._dct[item.lower()] = value |
---|
248 | n/a | def __getitem__(self, item): |
---|
249 | n/a | return self._dct[item] |
---|
250 | n/a | |
---|
251 | n/a | |
---|
252 | n/a | class GrammarTests(unittest.TestCase): |
---|
253 | n/a | |
---|
254 | n/a | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
---|
255 | n/a | # XXX can't test in a script -- this rule is only used when interactive |
---|
256 | n/a | |
---|
257 | n/a | # file_input: (NEWLINE | stmt)* ENDMARKER |
---|
258 | n/a | # Being tested as this very moment this very module |
---|
259 | n/a | |
---|
260 | n/a | # expr_input: testlist NEWLINE |
---|
261 | n/a | # XXX Hard to test -- used only in calls to input() |
---|
262 | n/a | |
---|
263 | n/a | def test_eval_input(self): |
---|
264 | n/a | # testlist ENDMARKER |
---|
265 | n/a | x = eval('1, 0 or 1') |
---|
266 | n/a | |
---|
267 | n/a | def test_var_annot_basics(self): |
---|
268 | n/a | # all these should be allowed |
---|
269 | n/a | var1: int = 5 |
---|
270 | n/a | var2: [int, str] |
---|
271 | n/a | my_lst = [42] |
---|
272 | n/a | def one(): |
---|
273 | n/a | return 1 |
---|
274 | n/a | int.new_attr: int |
---|
275 | n/a | [list][0]: type |
---|
276 | n/a | my_lst[one()-1]: int = 5 |
---|
277 | n/a | self.assertEqual(my_lst, [5]) |
---|
278 | n/a | |
---|
279 | n/a | def test_var_annot_syntax_errors(self): |
---|
280 | n/a | # parser pass |
---|
281 | n/a | check_syntax_error(self, "def f: int") |
---|
282 | n/a | check_syntax_error(self, "x: int: str") |
---|
283 | n/a | check_syntax_error(self, "def f():\n" |
---|
284 | n/a | " nonlocal x: int\n") |
---|
285 | n/a | # AST pass |
---|
286 | n/a | check_syntax_error(self, "[x, 0]: int\n") |
---|
287 | n/a | check_syntax_error(self, "f(): int\n") |
---|
288 | n/a | check_syntax_error(self, "(x,): int") |
---|
289 | n/a | check_syntax_error(self, "def f():\n" |
---|
290 | n/a | " (x, y): int = (1, 2)\n") |
---|
291 | n/a | # symtable pass |
---|
292 | n/a | check_syntax_error(self, "def f():\n" |
---|
293 | n/a | " x: int\n" |
---|
294 | n/a | " global x\n") |
---|
295 | n/a | check_syntax_error(self, "def f():\n" |
---|
296 | n/a | " global x\n" |
---|
297 | n/a | " x: int\n") |
---|
298 | n/a | |
---|
299 | n/a | def test_var_annot_basic_semantics(self): |
---|
300 | n/a | # execution order |
---|
301 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
302 | n/a | no_name[does_not_exist]: no_name_again = 1/0 |
---|
303 | n/a | with self.assertRaises(NameError): |
---|
304 | n/a | no_name[does_not_exist]: 1/0 = 0 |
---|
305 | n/a | global var_annot_global |
---|
306 | n/a | |
---|
307 | n/a | # function semantics |
---|
308 | n/a | def f(): |
---|
309 | n/a | st: str = "Hello" |
---|
310 | n/a | a.b: int = (1, 2) |
---|
311 | n/a | return st |
---|
312 | n/a | self.assertEqual(f.__annotations__, {}) |
---|
313 | n/a | def f_OK(): |
---|
314 | n/a | x: 1/0 |
---|
315 | n/a | f_OK() |
---|
316 | n/a | def fbad(): |
---|
317 | n/a | x: int |
---|
318 | n/a | print(x) |
---|
319 | n/a | with self.assertRaises(UnboundLocalError): |
---|
320 | n/a | fbad() |
---|
321 | n/a | def f2bad(): |
---|
322 | n/a | (no_such_global): int |
---|
323 | n/a | print(no_such_global) |
---|
324 | n/a | try: |
---|
325 | n/a | f2bad() |
---|
326 | n/a | except Exception as e: |
---|
327 | n/a | self.assertIs(type(e), NameError) |
---|
328 | n/a | |
---|
329 | n/a | # class semantics |
---|
330 | n/a | class C: |
---|
331 | n/a | __foo: int |
---|
332 | n/a | s: str = "attr" |
---|
333 | n/a | z = 2 |
---|
334 | n/a | def __init__(self, x): |
---|
335 | n/a | self.x: int = x |
---|
336 | n/a | self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str}) |
---|
337 | n/a | with self.assertRaises(NameError): |
---|
338 | n/a | class CBad: |
---|
339 | n/a | no_such_name_defined.attr: int = 0 |
---|
340 | n/a | with self.assertRaises(NameError): |
---|
341 | n/a | class Cbad2(C): |
---|
342 | n/a | x: int |
---|
343 | n/a | x.y: list = [] |
---|
344 | n/a | |
---|
345 | n/a | def test_var_annot_metaclass_semantics(self): |
---|
346 | n/a | class CMeta(type): |
---|
347 | n/a | @classmethod |
---|
348 | n/a | def __prepare__(metacls, name, bases, **kwds): |
---|
349 | n/a | return {'__annotations__': CNS()} |
---|
350 | n/a | class CC(metaclass=CMeta): |
---|
351 | n/a | XX: 'ANNOT' |
---|
352 | n/a | self.assertEqual(CC.__annotations__['xx'], 'ANNOT') |
---|
353 | n/a | |
---|
354 | n/a | def test_var_annot_module_semantics(self): |
---|
355 | n/a | with self.assertRaises(AttributeError): |
---|
356 | n/a | print(test.__annotations__) |
---|
357 | n/a | self.assertEqual(ann_module.__annotations__, |
---|
358 | n/a | {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]}) |
---|
359 | n/a | self.assertEqual(ann_module.M.__annotations__, |
---|
360 | n/a | {'123': 123, 'o': type}) |
---|
361 | n/a | self.assertEqual(ann_module2.__annotations__, {}) |
---|
362 | n/a | |
---|
363 | n/a | def test_var_annot_in_module(self): |
---|
364 | n/a | # check that functions fail the same way when executed |
---|
365 | n/a | # outside of module where they were defined |
---|
366 | n/a | from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann |
---|
367 | n/a | with self.assertRaises(NameError): |
---|
368 | n/a | f_bad_ann() |
---|
369 | n/a | with self.assertRaises(NameError): |
---|
370 | n/a | g_bad_ann() |
---|
371 | n/a | with self.assertRaises(NameError): |
---|
372 | n/a | D_bad_ann(5) |
---|
373 | n/a | |
---|
374 | n/a | def test_var_annot_simple_exec(self): |
---|
375 | n/a | gns = {}; lns= {} |
---|
376 | n/a | exec("'docstring'\n" |
---|
377 | n/a | "__annotations__[1] = 2\n" |
---|
378 | n/a | "x: int = 5\n", gns, lns) |
---|
379 | n/a | self.assertEqual(lns["__annotations__"], {1: 2, 'x': int}) |
---|
380 | n/a | with self.assertRaises(KeyError): |
---|
381 | n/a | gns['__annotations__'] |
---|
382 | n/a | |
---|
383 | n/a | def test_var_annot_custom_maps(self): |
---|
384 | n/a | # tests with custom locals() and __annotations__ |
---|
385 | n/a | ns = {'__annotations__': CNS()} |
---|
386 | n/a | exec('X: int; Z: str = "Z"; (w): complex = 1j', ns) |
---|
387 | n/a | self.assertEqual(ns['__annotations__']['x'], int) |
---|
388 | n/a | self.assertEqual(ns['__annotations__']['z'], str) |
---|
389 | n/a | with self.assertRaises(KeyError): |
---|
390 | n/a | ns['__annotations__']['w'] |
---|
391 | n/a | nonloc_ns = {} |
---|
392 | n/a | class CNS2: |
---|
393 | n/a | def __init__(self): |
---|
394 | n/a | self._dct = {} |
---|
395 | n/a | def __setitem__(self, item, value): |
---|
396 | n/a | nonlocal nonloc_ns |
---|
397 | n/a | self._dct[item] = value |
---|
398 | n/a | nonloc_ns[item] = value |
---|
399 | n/a | def __getitem__(self, item): |
---|
400 | n/a | return self._dct[item] |
---|
401 | n/a | exec('x: int = 1', {}, CNS2()) |
---|
402 | n/a | self.assertEqual(nonloc_ns['__annotations__']['x'], int) |
---|
403 | n/a | |
---|
404 | n/a | def test_var_annot_refleak(self): |
---|
405 | n/a | # complex case: custom locals plus custom __annotations__ |
---|
406 | n/a | # this was causing refleak |
---|
407 | n/a | cns = CNS() |
---|
408 | n/a | nonloc_ns = {'__annotations__': cns} |
---|
409 | n/a | class CNS2: |
---|
410 | n/a | def __init__(self): |
---|
411 | n/a | self._dct = {'__annotations__': cns} |
---|
412 | n/a | def __setitem__(self, item, value): |
---|
413 | n/a | nonlocal nonloc_ns |
---|
414 | n/a | self._dct[item] = value |
---|
415 | n/a | nonloc_ns[item] = value |
---|
416 | n/a | def __getitem__(self, item): |
---|
417 | n/a | return self._dct[item] |
---|
418 | n/a | exec('X: str', {}, CNS2()) |
---|
419 | n/a | self.assertEqual(nonloc_ns['__annotations__']['x'], str) |
---|
420 | n/a | |
---|
421 | n/a | def test_funcdef(self): |
---|
422 | n/a | ### [decorators] 'def' NAME parameters ['->' test] ':' suite |
---|
423 | n/a | ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
---|
424 | n/a | ### decorators: decorator+ |
---|
425 | n/a | ### parameters: '(' [typedargslist] ')' |
---|
426 | n/a | ### typedargslist: ((tfpdef ['=' test] ',')* |
---|
427 | n/a | ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
---|
428 | n/a | ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
---|
429 | n/a | ### tfpdef: NAME [':' test] |
---|
430 | n/a | ### varargslist: ((vfpdef ['=' test] ',')* |
---|
431 | n/a | ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
---|
432 | n/a | ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
---|
433 | n/a | ### vfpdef: NAME |
---|
434 | n/a | def f1(): pass |
---|
435 | n/a | f1() |
---|
436 | n/a | f1(*()) |
---|
437 | n/a | f1(*(), **{}) |
---|
438 | n/a | def f2(one_argument): pass |
---|
439 | n/a | def f3(two, arguments): pass |
---|
440 | n/a | self.assertEqual(f2.__code__.co_varnames, ('one_argument',)) |
---|
441 | n/a | self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments')) |
---|
442 | n/a | def a1(one_arg,): pass |
---|
443 | n/a | def a2(two, args,): pass |
---|
444 | n/a | def v0(*rest): pass |
---|
445 | n/a | def v1(a, *rest): pass |
---|
446 | n/a | def v2(a, b, *rest): pass |
---|
447 | n/a | |
---|
448 | n/a | f1() |
---|
449 | n/a | f2(1) |
---|
450 | n/a | f2(1,) |
---|
451 | n/a | f3(1, 2) |
---|
452 | n/a | f3(1, 2,) |
---|
453 | n/a | v0() |
---|
454 | n/a | v0(1) |
---|
455 | n/a | v0(1,) |
---|
456 | n/a | v0(1,2) |
---|
457 | n/a | v0(1,2,3,4,5,6,7,8,9,0) |
---|
458 | n/a | v1(1) |
---|
459 | n/a | v1(1,) |
---|
460 | n/a | v1(1,2) |
---|
461 | n/a | v1(1,2,3) |
---|
462 | n/a | v1(1,2,3,4,5,6,7,8,9,0) |
---|
463 | n/a | v2(1,2) |
---|
464 | n/a | v2(1,2,3) |
---|
465 | n/a | v2(1,2,3,4) |
---|
466 | n/a | v2(1,2,3,4,5,6,7,8,9,0) |
---|
467 | n/a | |
---|
468 | n/a | def d01(a=1): pass |
---|
469 | n/a | d01() |
---|
470 | n/a | d01(1) |
---|
471 | n/a | d01(*(1,)) |
---|
472 | n/a | d01(*[] or [2]) |
---|
473 | n/a | d01(*() or (), *{} and (), **() or {}) |
---|
474 | n/a | d01(**{'a':2}) |
---|
475 | n/a | d01(**{'a':2} or {}) |
---|
476 | n/a | def d11(a, b=1): pass |
---|
477 | n/a | d11(1) |
---|
478 | n/a | d11(1, 2) |
---|
479 | n/a | d11(1, **{'b':2}) |
---|
480 | n/a | def d21(a, b, c=1): pass |
---|
481 | n/a | d21(1, 2) |
---|
482 | n/a | d21(1, 2, 3) |
---|
483 | n/a | d21(*(1, 2, 3)) |
---|
484 | n/a | d21(1, *(2, 3)) |
---|
485 | n/a | d21(1, 2, *(3,)) |
---|
486 | n/a | d21(1, 2, **{'c':3}) |
---|
487 | n/a | def d02(a=1, b=2): pass |
---|
488 | n/a | d02() |
---|
489 | n/a | d02(1) |
---|
490 | n/a | d02(1, 2) |
---|
491 | n/a | d02(*(1, 2)) |
---|
492 | n/a | d02(1, *(2,)) |
---|
493 | n/a | d02(1, **{'b':2}) |
---|
494 | n/a | d02(**{'a': 1, 'b': 2}) |
---|
495 | n/a | def d12(a, b=1, c=2): pass |
---|
496 | n/a | d12(1) |
---|
497 | n/a | d12(1, 2) |
---|
498 | n/a | d12(1, 2, 3) |
---|
499 | n/a | def d22(a, b, c=1, d=2): pass |
---|
500 | n/a | d22(1, 2) |
---|
501 | n/a | d22(1, 2, 3) |
---|
502 | n/a | d22(1, 2, 3, 4) |
---|
503 | n/a | def d01v(a=1, *rest): pass |
---|
504 | n/a | d01v() |
---|
505 | n/a | d01v(1) |
---|
506 | n/a | d01v(1, 2) |
---|
507 | n/a | d01v(*(1, 2, 3, 4)) |
---|
508 | n/a | d01v(*(1,)) |
---|
509 | n/a | d01v(**{'a':2}) |
---|
510 | n/a | def d11v(a, b=1, *rest): pass |
---|
511 | n/a | d11v(1) |
---|
512 | n/a | d11v(1, 2) |
---|
513 | n/a | d11v(1, 2, 3) |
---|
514 | n/a | def d21v(a, b, c=1, *rest): pass |
---|
515 | n/a | d21v(1, 2) |
---|
516 | n/a | d21v(1, 2, 3) |
---|
517 | n/a | d21v(1, 2, 3, 4) |
---|
518 | n/a | d21v(*(1, 2, 3, 4)) |
---|
519 | n/a | d21v(1, 2, **{'c': 3}) |
---|
520 | n/a | def d02v(a=1, b=2, *rest): pass |
---|
521 | n/a | d02v() |
---|
522 | n/a | d02v(1) |
---|
523 | n/a | d02v(1, 2) |
---|
524 | n/a | d02v(1, 2, 3) |
---|
525 | n/a | d02v(1, *(2, 3, 4)) |
---|
526 | n/a | d02v(**{'a': 1, 'b': 2}) |
---|
527 | n/a | def d12v(a, b=1, c=2, *rest): pass |
---|
528 | n/a | d12v(1) |
---|
529 | n/a | d12v(1, 2) |
---|
530 | n/a | d12v(1, 2, 3) |
---|
531 | n/a | d12v(1, 2, 3, 4) |
---|
532 | n/a | d12v(*(1, 2, 3, 4)) |
---|
533 | n/a | d12v(1, 2, *(3, 4, 5)) |
---|
534 | n/a | d12v(1, *(2,), **{'c': 3}) |
---|
535 | n/a | def d22v(a, b, c=1, d=2, *rest): pass |
---|
536 | n/a | d22v(1, 2) |
---|
537 | n/a | d22v(1, 2, 3) |
---|
538 | n/a | d22v(1, 2, 3, 4) |
---|
539 | n/a | d22v(1, 2, 3, 4, 5) |
---|
540 | n/a | d22v(*(1, 2, 3, 4)) |
---|
541 | n/a | d22v(1, 2, *(3, 4, 5)) |
---|
542 | n/a | d22v(1, *(2, 3), **{'d': 4}) |
---|
543 | n/a | |
---|
544 | n/a | # keyword argument type tests |
---|
545 | n/a | try: |
---|
546 | n/a | str('x', **{b'foo':1 }) |
---|
547 | n/a | except TypeError: |
---|
548 | n/a | pass |
---|
549 | n/a | else: |
---|
550 | n/a | self.fail('Bytes should not work as keyword argument names') |
---|
551 | n/a | # keyword only argument tests |
---|
552 | n/a | def pos0key1(*, key): return key |
---|
553 | n/a | pos0key1(key=100) |
---|
554 | n/a | def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |
---|
555 | n/a | pos2key2(1, 2, k1=100) |
---|
556 | n/a | pos2key2(1, 2, k1=100, k2=200) |
---|
557 | n/a | pos2key2(1, 2, k2=100, k1=200) |
---|
558 | n/a | def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |
---|
559 | n/a | pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |
---|
560 | n/a | pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |
---|
561 | n/a | |
---|
562 | n/a | self.assertRaises(SyntaxError, eval, "def f(*): pass") |
---|
563 | n/a | self.assertRaises(SyntaxError, eval, "def f(*,): pass") |
---|
564 | n/a | self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass") |
---|
565 | n/a | |
---|
566 | n/a | # keyword arguments after *arglist |
---|
567 | n/a | def f(*args, **kwargs): |
---|
568 | n/a | return args, kwargs |
---|
569 | n/a | self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |
---|
570 | n/a | {'x':2, 'y':5})) |
---|
571 | n/a | self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {})) |
---|
572 | n/a | self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
---|
573 | n/a | self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}), |
---|
574 | n/a | ((), {'eggs':'scrambled', 'spam':'fried'})) |
---|
575 | n/a | self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}), |
---|
576 | n/a | ((), {'eggs':'scrambled', 'spam':'fried'})) |
---|
577 | n/a | |
---|
578 | n/a | # argument annotation tests |
---|
579 | n/a | def f(x) -> list: pass |
---|
580 | n/a | self.assertEqual(f.__annotations__, {'return': list}) |
---|
581 | n/a | def f(x: int): pass |
---|
582 | n/a | self.assertEqual(f.__annotations__, {'x': int}) |
---|
583 | n/a | def f(*x: str): pass |
---|
584 | n/a | self.assertEqual(f.__annotations__, {'x': str}) |
---|
585 | n/a | def f(**x: float): pass |
---|
586 | n/a | self.assertEqual(f.__annotations__, {'x': float}) |
---|
587 | n/a | def f(x, y: 1+2): pass |
---|
588 | n/a | self.assertEqual(f.__annotations__, {'y': 3}) |
---|
589 | n/a | def f(a, b: 1, c: 2, d): pass |
---|
590 | n/a | self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
---|
591 | n/a | def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass |
---|
592 | n/a | self.assertEqual(f.__annotations__, |
---|
593 | n/a | {'b': 1, 'c': 2, 'e': 3, 'g': 6}) |
---|
594 | n/a | def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10, |
---|
595 | n/a | **k: 11) -> 12: pass |
---|
596 | n/a | self.assertEqual(f.__annotations__, |
---|
597 | n/a | {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
---|
598 | n/a | 'k': 11, 'return': 12}) |
---|
599 | n/a | # Check for issue #20625 -- annotations mangling |
---|
600 | n/a | class Spam: |
---|
601 | n/a | def f(self, *, __kw: 1): |
---|
602 | n/a | pass |
---|
603 | n/a | class Ham(Spam): pass |
---|
604 | n/a | self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1}) |
---|
605 | n/a | self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1}) |
---|
606 | n/a | # Check for SF Bug #1697248 - mixing decorators and a return annotation |
---|
607 | n/a | def null(x): return x |
---|
608 | n/a | @null |
---|
609 | n/a | def f(x) -> list: pass |
---|
610 | n/a | self.assertEqual(f.__annotations__, {'return': list}) |
---|
611 | n/a | |
---|
612 | n/a | # test closures with a variety of opargs |
---|
613 | n/a | closure = 1 |
---|
614 | n/a | def f(): return closure |
---|
615 | n/a | def f(x=1): return closure |
---|
616 | n/a | def f(*, k=1): return closure |
---|
617 | n/a | def f() -> int: return closure |
---|
618 | n/a | |
---|
619 | n/a | # Check ast errors in *args and *kwargs |
---|
620 | n/a | check_syntax_error(self, "f(*g(1=2))") |
---|
621 | n/a | check_syntax_error(self, "f(**g(1=2))") |
---|
622 | n/a | |
---|
623 | n/a | # Check trailing commas are permitted in funcdef argument list |
---|
624 | n/a | def f(a,): pass |
---|
625 | n/a | def f(*args,): pass |
---|
626 | n/a | def f(**kwds,): pass |
---|
627 | n/a | def f(a, *args,): pass |
---|
628 | n/a | def f(a, **kwds,): pass |
---|
629 | n/a | def f(*args, b,): pass |
---|
630 | n/a | def f(*, b,): pass |
---|
631 | n/a | def f(*args, **kwds,): pass |
---|
632 | n/a | def f(a, *args, b,): pass |
---|
633 | n/a | def f(a, *, b,): pass |
---|
634 | n/a | def f(a, *args, **kwds,): pass |
---|
635 | n/a | def f(*args, b, **kwds,): pass |
---|
636 | n/a | def f(*, b, **kwds,): pass |
---|
637 | n/a | def f(a, *args, b, **kwds,): pass |
---|
638 | n/a | def f(a, *, b, **kwds,): pass |
---|
639 | n/a | |
---|
640 | n/a | def test_lambdef(self): |
---|
641 | n/a | ### lambdef: 'lambda' [varargslist] ':' test |
---|
642 | n/a | l1 = lambda : 0 |
---|
643 | n/a | self.assertEqual(l1(), 0) |
---|
644 | n/a | l2 = lambda : a[d] # XXX just testing the expression |
---|
645 | n/a | l3 = lambda : [2 < x for x in [-1, 3, 0]] |
---|
646 | n/a | self.assertEqual(l3(), [0, 1, 0]) |
---|
647 | n/a | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
---|
648 | n/a | self.assertEqual(l4(), 1) |
---|
649 | n/a | l5 = lambda x, y, z=2: x + y + z |
---|
650 | n/a | self.assertEqual(l5(1, 2), 5) |
---|
651 | n/a | self.assertEqual(l5(1, 2, 3), 6) |
---|
652 | n/a | check_syntax_error(self, "lambda x: x = 2") |
---|
653 | n/a | check_syntax_error(self, "lambda (None,): None") |
---|
654 | n/a | l6 = lambda x, y, *, k=20: x+y+k |
---|
655 | n/a | self.assertEqual(l6(1,2), 1+2+20) |
---|
656 | n/a | self.assertEqual(l6(1,2,k=10), 1+2+10) |
---|
657 | n/a | |
---|
658 | n/a | # check that trailing commas are permitted |
---|
659 | n/a | l10 = lambda a,: 0 |
---|
660 | n/a | l11 = lambda *args,: 0 |
---|
661 | n/a | l12 = lambda **kwds,: 0 |
---|
662 | n/a | l13 = lambda a, *args,: 0 |
---|
663 | n/a | l14 = lambda a, **kwds,: 0 |
---|
664 | n/a | l15 = lambda *args, b,: 0 |
---|
665 | n/a | l16 = lambda *, b,: 0 |
---|
666 | n/a | l17 = lambda *args, **kwds,: 0 |
---|
667 | n/a | l18 = lambda a, *args, b,: 0 |
---|
668 | n/a | l19 = lambda a, *, b,: 0 |
---|
669 | n/a | l20 = lambda a, *args, **kwds,: 0 |
---|
670 | n/a | l21 = lambda *args, b, **kwds,: 0 |
---|
671 | n/a | l22 = lambda *, b, **kwds,: 0 |
---|
672 | n/a | l23 = lambda a, *args, b, **kwds,: 0 |
---|
673 | n/a | l24 = lambda a, *, b, **kwds,: 0 |
---|
674 | n/a | |
---|
675 | n/a | |
---|
676 | n/a | ### stmt: simple_stmt | compound_stmt |
---|
677 | n/a | # Tested below |
---|
678 | n/a | |
---|
679 | n/a | def test_simple_stmt(self): |
---|
680 | n/a | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
---|
681 | n/a | x = 1; pass; del x |
---|
682 | n/a | def foo(): |
---|
683 | n/a | # verify statements that end with semi-colons |
---|
684 | n/a | x = 1; pass; del x; |
---|
685 | n/a | foo() |
---|
686 | n/a | |
---|
687 | n/a | ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt |
---|
688 | n/a | # Tested below |
---|
689 | n/a | |
---|
690 | n/a | def test_expr_stmt(self): |
---|
691 | n/a | # (exprlist '=')* exprlist |
---|
692 | n/a | 1 |
---|
693 | n/a | 1, 2, 3 |
---|
694 | n/a | x = 1 |
---|
695 | n/a | x = 1, 2, 3 |
---|
696 | n/a | x = y = z = 1, 2, 3 |
---|
697 | n/a | x, y, z = 1, 2, 3 |
---|
698 | n/a | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
---|
699 | n/a | |
---|
700 | n/a | check_syntax_error(self, "x + 1 = 1") |
---|
701 | n/a | check_syntax_error(self, "a + 1 = b + 2") |
---|
702 | n/a | |
---|
703 | n/a | # Check the heuristic for print & exec covers significant cases |
---|
704 | n/a | # As well as placing some limits on false positives |
---|
705 | n/a | def test_former_statements_refer_to_builtins(self): |
---|
706 | n/a | keywords = "print", "exec" |
---|
707 | n/a | # Cases where we want the custom error |
---|
708 | n/a | cases = [ |
---|
709 | n/a | "{} foo", |
---|
710 | n/a | "{} {{1:foo}}", |
---|
711 | n/a | "if 1: {} foo", |
---|
712 | n/a | "if 1: {} {{1:foo}}", |
---|
713 | n/a | "if 1:\n {} foo", |
---|
714 | n/a | "if 1:\n {} {{1:foo}}", |
---|
715 | n/a | ] |
---|
716 | n/a | for keyword in keywords: |
---|
717 | n/a | custom_msg = "call to '{}'".format(keyword) |
---|
718 | n/a | for case in cases: |
---|
719 | n/a | source = case.format(keyword) |
---|
720 | n/a | with self.subTest(source=source): |
---|
721 | n/a | with self.assertRaisesRegex(SyntaxError, custom_msg): |
---|
722 | n/a | exec(source) |
---|
723 | n/a | source = source.replace("foo", "(foo.)") |
---|
724 | n/a | with self.subTest(source=source): |
---|
725 | n/a | with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
---|
726 | n/a | exec(source) |
---|
727 | n/a | |
---|
728 | n/a | def test_del_stmt(self): |
---|
729 | n/a | # 'del' exprlist |
---|
730 | n/a | abc = [1,2,3] |
---|
731 | n/a | x, y, z = abc |
---|
732 | n/a | xyz = x, y, z |
---|
733 | n/a | |
---|
734 | n/a | del abc |
---|
735 | n/a | del x, y, (z, xyz) |
---|
736 | n/a | |
---|
737 | n/a | def test_pass_stmt(self): |
---|
738 | n/a | # 'pass' |
---|
739 | n/a | pass |
---|
740 | n/a | |
---|
741 | n/a | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
---|
742 | n/a | # Tested below |
---|
743 | n/a | |
---|
744 | n/a | def test_break_stmt(self): |
---|
745 | n/a | # 'break' |
---|
746 | n/a | while 1: break |
---|
747 | n/a | |
---|
748 | n/a | def test_continue_stmt(self): |
---|
749 | n/a | # 'continue' |
---|
750 | n/a | i = 1 |
---|
751 | n/a | while i: i = 0; continue |
---|
752 | n/a | |
---|
753 | n/a | msg = "" |
---|
754 | n/a | while not msg: |
---|
755 | n/a | msg = "ok" |
---|
756 | n/a | try: |
---|
757 | n/a | continue |
---|
758 | n/a | msg = "continue failed to continue inside try" |
---|
759 | n/a | except: |
---|
760 | n/a | msg = "continue inside try called except block" |
---|
761 | n/a | if msg != "ok": |
---|
762 | n/a | self.fail(msg) |
---|
763 | n/a | |
---|
764 | n/a | msg = "" |
---|
765 | n/a | while not msg: |
---|
766 | n/a | msg = "finally block not called" |
---|
767 | n/a | try: |
---|
768 | n/a | continue |
---|
769 | n/a | finally: |
---|
770 | n/a | msg = "ok" |
---|
771 | n/a | if msg != "ok": |
---|
772 | n/a | self.fail(msg) |
---|
773 | n/a | |
---|
774 | n/a | def test_break_continue_loop(self): |
---|
775 | n/a | # This test warrants an explanation. It is a test specifically for SF bugs |
---|
776 | n/a | # #463359 and #462937. The bug is that a 'break' statement executed or |
---|
777 | n/a | # exception raised inside a try/except inside a loop, *after* a continue |
---|
778 | n/a | # statement has been executed in that loop, will cause the wrong number of |
---|
779 | n/a | # arguments to be popped off the stack and the instruction pointer reset to |
---|
780 | n/a | # a very small number (usually 0.) Because of this, the following test |
---|
781 | n/a | # *must* written as a function, and the tracking vars *must* be function |
---|
782 | n/a | # arguments with default values. Otherwise, the test will loop and loop. |
---|
783 | n/a | |
---|
784 | n/a | def test_inner(extra_burning_oil = 1, count=0): |
---|
785 | n/a | big_hippo = 2 |
---|
786 | n/a | while big_hippo: |
---|
787 | n/a | count += 1 |
---|
788 | n/a | try: |
---|
789 | n/a | if extra_burning_oil and big_hippo == 1: |
---|
790 | n/a | extra_burning_oil -= 1 |
---|
791 | n/a | break |
---|
792 | n/a | big_hippo -= 1 |
---|
793 | n/a | continue |
---|
794 | n/a | except: |
---|
795 | n/a | raise |
---|
796 | n/a | if count > 2 or big_hippo != 1: |
---|
797 | n/a | self.fail("continue then break in try/except in loop broken!") |
---|
798 | n/a | test_inner() |
---|
799 | n/a | |
---|
800 | n/a | def test_return(self): |
---|
801 | n/a | # 'return' [testlist] |
---|
802 | n/a | def g1(): return |
---|
803 | n/a | def g2(): return 1 |
---|
804 | n/a | g1() |
---|
805 | n/a | x = g2() |
---|
806 | n/a | check_syntax_error(self, "class foo:return 1") |
---|
807 | n/a | |
---|
808 | n/a | def test_yield(self): |
---|
809 | n/a | # Allowed as standalone statement |
---|
810 | n/a | def g(): yield 1 |
---|
811 | n/a | def g(): yield from () |
---|
812 | n/a | # Allowed as RHS of assignment |
---|
813 | n/a | def g(): x = yield 1 |
---|
814 | n/a | def g(): x = yield from () |
---|
815 | n/a | # Ordinary yield accepts implicit tuples |
---|
816 | n/a | def g(): yield 1, 1 |
---|
817 | n/a | def g(): x = yield 1, 1 |
---|
818 | n/a | # 'yield from' does not |
---|
819 | n/a | check_syntax_error(self, "def g(): yield from (), 1") |
---|
820 | n/a | check_syntax_error(self, "def g(): x = yield from (), 1") |
---|
821 | n/a | # Requires parentheses as subexpression |
---|
822 | n/a | def g(): 1, (yield 1) |
---|
823 | n/a | def g(): 1, (yield from ()) |
---|
824 | n/a | check_syntax_error(self, "def g(): 1, yield 1") |
---|
825 | n/a | check_syntax_error(self, "def g(): 1, yield from ()") |
---|
826 | n/a | # Requires parentheses as call argument |
---|
827 | n/a | def g(): f((yield 1)) |
---|
828 | n/a | def g(): f((yield 1), 1) |
---|
829 | n/a | def g(): f((yield from ())) |
---|
830 | n/a | def g(): f((yield from ()), 1) |
---|
831 | n/a | check_syntax_error(self, "def g(): f(yield 1)") |
---|
832 | n/a | check_syntax_error(self, "def g(): f(yield 1, 1)") |
---|
833 | n/a | check_syntax_error(self, "def g(): f(yield from ())") |
---|
834 | n/a | check_syntax_error(self, "def g(): f(yield from (), 1)") |
---|
835 | n/a | # Not allowed at top level |
---|
836 | n/a | check_syntax_error(self, "yield") |
---|
837 | n/a | check_syntax_error(self, "yield from") |
---|
838 | n/a | # Not allowed at class scope |
---|
839 | n/a | check_syntax_error(self, "class foo:yield 1") |
---|
840 | n/a | check_syntax_error(self, "class foo:yield from ()") |
---|
841 | n/a | # Check annotation refleak on SyntaxError |
---|
842 | n/a | check_syntax_error(self, "def g(a:(yield)): pass") |
---|
843 | n/a | |
---|
844 | n/a | def test_raise(self): |
---|
845 | n/a | # 'raise' test [',' test] |
---|
846 | n/a | try: raise RuntimeError('just testing') |
---|
847 | n/a | except RuntimeError: pass |
---|
848 | n/a | try: raise KeyboardInterrupt |
---|
849 | n/a | except KeyboardInterrupt: pass |
---|
850 | n/a | |
---|
851 | n/a | def test_import(self): |
---|
852 | n/a | # 'import' dotted_as_names |
---|
853 | n/a | import sys |
---|
854 | n/a | import time, sys |
---|
855 | n/a | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
---|
856 | n/a | from time import time |
---|
857 | n/a | from time import (time) |
---|
858 | n/a | # not testable inside a function, but already done at top of the module |
---|
859 | n/a | # from sys import * |
---|
860 | n/a | from sys import path, argv |
---|
861 | n/a | from sys import (path, argv) |
---|
862 | n/a | from sys import (path, argv,) |
---|
863 | n/a | |
---|
864 | n/a | def test_global(self): |
---|
865 | n/a | # 'global' NAME (',' NAME)* |
---|
866 | n/a | global a |
---|
867 | n/a | global a, b |
---|
868 | n/a | global one, two, three, four, five, six, seven, eight, nine, ten |
---|
869 | n/a | |
---|
870 | n/a | def test_nonlocal(self): |
---|
871 | n/a | # 'nonlocal' NAME (',' NAME)* |
---|
872 | n/a | x = 0 |
---|
873 | n/a | y = 0 |
---|
874 | n/a | def f(): |
---|
875 | n/a | nonlocal x |
---|
876 | n/a | nonlocal x, y |
---|
877 | n/a | |
---|
878 | n/a | def test_assert(self): |
---|
879 | n/a | # assertTruestmt: 'assert' test [',' test] |
---|
880 | n/a | assert 1 |
---|
881 | n/a | assert 1, 1 |
---|
882 | n/a | assert lambda x:x |
---|
883 | n/a | assert 1, lambda x:x+1 |
---|
884 | n/a | |
---|
885 | n/a | try: |
---|
886 | n/a | assert True |
---|
887 | n/a | except AssertionError as e: |
---|
888 | n/a | self.fail("'assert True' should not have raised an AssertionError") |
---|
889 | n/a | |
---|
890 | n/a | try: |
---|
891 | n/a | assert True, 'this should always pass' |
---|
892 | n/a | except AssertionError as e: |
---|
893 | n/a | self.fail("'assert True, msg' should not have " |
---|
894 | n/a | "raised an AssertionError") |
---|
895 | n/a | |
---|
896 | n/a | # these tests fail if python is run with -O, so check __debug__ |
---|
897 | n/a | @unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
---|
898 | n/a | def testAssert2(self): |
---|
899 | n/a | try: |
---|
900 | n/a | assert 0, "msg" |
---|
901 | n/a | except AssertionError as e: |
---|
902 | n/a | self.assertEqual(e.args[0], "msg") |
---|
903 | n/a | else: |
---|
904 | n/a | self.fail("AssertionError not raised by assert 0") |
---|
905 | n/a | |
---|
906 | n/a | try: |
---|
907 | n/a | assert False |
---|
908 | n/a | except AssertionError as e: |
---|
909 | n/a | self.assertEqual(len(e.args), 0) |
---|
910 | n/a | else: |
---|
911 | n/a | self.fail("AssertionError not raised by 'assert False'") |
---|
912 | n/a | |
---|
913 | n/a | |
---|
914 | n/a | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
---|
915 | n/a | # Tested below |
---|
916 | n/a | |
---|
917 | n/a | def test_if(self): |
---|
918 | n/a | # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
---|
919 | n/a | if 1: pass |
---|
920 | n/a | if 1: pass |
---|
921 | n/a | else: pass |
---|
922 | n/a | if 0: pass |
---|
923 | n/a | elif 0: pass |
---|
924 | n/a | if 0: pass |
---|
925 | n/a | elif 0: pass |
---|
926 | n/a | elif 0: pass |
---|
927 | n/a | elif 0: pass |
---|
928 | n/a | else: pass |
---|
929 | n/a | |
---|
930 | n/a | def test_while(self): |
---|
931 | n/a | # 'while' test ':' suite ['else' ':' suite] |
---|
932 | n/a | while 0: pass |
---|
933 | n/a | while 0: pass |
---|
934 | n/a | else: pass |
---|
935 | n/a | |
---|
936 | n/a | # Issue1920: "while 0" is optimized away, |
---|
937 | n/a | # ensure that the "else" clause is still present. |
---|
938 | n/a | x = 0 |
---|
939 | n/a | while 0: |
---|
940 | n/a | x = 1 |
---|
941 | n/a | else: |
---|
942 | n/a | x = 2 |
---|
943 | n/a | self.assertEqual(x, 2) |
---|
944 | n/a | |
---|
945 | n/a | def test_for(self): |
---|
946 | n/a | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
---|
947 | n/a | for i in 1, 2, 3: pass |
---|
948 | n/a | for i, j, k in (): pass |
---|
949 | n/a | else: pass |
---|
950 | n/a | class Squares: |
---|
951 | n/a | def __init__(self, max): |
---|
952 | n/a | self.max = max |
---|
953 | n/a | self.sofar = [] |
---|
954 | n/a | def __len__(self): return len(self.sofar) |
---|
955 | n/a | def __getitem__(self, i): |
---|
956 | n/a | if not 0 <= i < self.max: raise IndexError |
---|
957 | n/a | n = len(self.sofar) |
---|
958 | n/a | while n <= i: |
---|
959 | n/a | self.sofar.append(n*n) |
---|
960 | n/a | n = n+1 |
---|
961 | n/a | return self.sofar[i] |
---|
962 | n/a | n = 0 |
---|
963 | n/a | for x in Squares(10): n = n+x |
---|
964 | n/a | if n != 285: |
---|
965 | n/a | self.fail('for over growing sequence') |
---|
966 | n/a | |
---|
967 | n/a | result = [] |
---|
968 | n/a | for x, in [(1,), (2,), (3,)]: |
---|
969 | n/a | result.append(x) |
---|
970 | n/a | self.assertEqual(result, [1, 2, 3]) |
---|
971 | n/a | |
---|
972 | n/a | def test_try(self): |
---|
973 | n/a | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
---|
974 | n/a | ### | 'try' ':' suite 'finally' ':' suite |
---|
975 | n/a | ### except_clause: 'except' [expr ['as' expr]] |
---|
976 | n/a | try: |
---|
977 | n/a | 1/0 |
---|
978 | n/a | except ZeroDivisionError: |
---|
979 | n/a | pass |
---|
980 | n/a | else: |
---|
981 | n/a | pass |
---|
982 | n/a | try: 1/0 |
---|
983 | n/a | except EOFError: pass |
---|
984 | n/a | except TypeError as msg: pass |
---|
985 | n/a | except RuntimeError as msg: pass |
---|
986 | n/a | except: pass |
---|
987 | n/a | else: pass |
---|
988 | n/a | try: 1/0 |
---|
989 | n/a | except (EOFError, TypeError, ZeroDivisionError): pass |
---|
990 | n/a | try: 1/0 |
---|
991 | n/a | except (EOFError, TypeError, ZeroDivisionError) as msg: pass |
---|
992 | n/a | try: pass |
---|
993 | n/a | finally: pass |
---|
994 | n/a | |
---|
995 | n/a | def test_suite(self): |
---|
996 | n/a | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
---|
997 | n/a | if 1: pass |
---|
998 | n/a | if 1: |
---|
999 | n/a | pass |
---|
1000 | n/a | if 1: |
---|
1001 | n/a | # |
---|
1002 | n/a | # |
---|
1003 | n/a | # |
---|
1004 | n/a | pass |
---|
1005 | n/a | pass |
---|
1006 | n/a | # |
---|
1007 | n/a | pass |
---|
1008 | n/a | # |
---|
1009 | n/a | |
---|
1010 | n/a | def test_test(self): |
---|
1011 | n/a | ### and_test ('or' and_test)* |
---|
1012 | n/a | ### and_test: not_test ('and' not_test)* |
---|
1013 | n/a | ### not_test: 'not' not_test | comparison |
---|
1014 | n/a | if not 1: pass |
---|
1015 | n/a | if 1 and 1: pass |
---|
1016 | n/a | if 1 or 1: pass |
---|
1017 | n/a | if not not not 1: pass |
---|
1018 | n/a | if not 1 and 1 and 1: pass |
---|
1019 | n/a | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
---|
1020 | n/a | |
---|
1021 | n/a | def test_comparison(self): |
---|
1022 | n/a | ### comparison: expr (comp_op expr)* |
---|
1023 | n/a | ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
---|
1024 | n/a | if 1: pass |
---|
1025 | n/a | x = (1 == 1) |
---|
1026 | n/a | if 1 == 1: pass |
---|
1027 | n/a | if 1 != 1: pass |
---|
1028 | n/a | if 1 < 1: pass |
---|
1029 | n/a | if 1 > 1: pass |
---|
1030 | n/a | if 1 <= 1: pass |
---|
1031 | n/a | if 1 >= 1: pass |
---|
1032 | n/a | if 1 is 1: pass |
---|
1033 | n/a | if 1 is not 1: pass |
---|
1034 | n/a | if 1 in (): pass |
---|
1035 | n/a | if 1 not in (): pass |
---|
1036 | n/a | if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass |
---|
1037 | n/a | |
---|
1038 | n/a | def test_binary_mask_ops(self): |
---|
1039 | n/a | x = 1 & 1 |
---|
1040 | n/a | x = 1 ^ 1 |
---|
1041 | n/a | x = 1 | 1 |
---|
1042 | n/a | |
---|
1043 | n/a | def test_shift_ops(self): |
---|
1044 | n/a | x = 1 << 1 |
---|
1045 | n/a | x = 1 >> 1 |
---|
1046 | n/a | x = 1 << 1 >> 1 |
---|
1047 | n/a | |
---|
1048 | n/a | def test_additive_ops(self): |
---|
1049 | n/a | x = 1 |
---|
1050 | n/a | x = 1 + 1 |
---|
1051 | n/a | x = 1 - 1 - 1 |
---|
1052 | n/a | x = 1 - 1 + 1 - 1 + 1 |
---|
1053 | n/a | |
---|
1054 | n/a | def test_multiplicative_ops(self): |
---|
1055 | n/a | x = 1 * 1 |
---|
1056 | n/a | x = 1 / 1 |
---|
1057 | n/a | x = 1 % 1 |
---|
1058 | n/a | x = 1 / 1 * 1 % 1 |
---|
1059 | n/a | |
---|
1060 | n/a | def test_unary_ops(self): |
---|
1061 | n/a | x = +1 |
---|
1062 | n/a | x = -1 |
---|
1063 | n/a | x = ~1 |
---|
1064 | n/a | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
---|
1065 | n/a | x = -1*1/1 + 1*1 - ---1*1 |
---|
1066 | n/a | |
---|
1067 | n/a | def test_selectors(self): |
---|
1068 | n/a | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
---|
1069 | n/a | ### subscript: expr | [expr] ':' [expr] |
---|
1070 | n/a | |
---|
1071 | n/a | import sys, time |
---|
1072 | n/a | c = sys.path[0] |
---|
1073 | n/a | x = time.time() |
---|
1074 | n/a | x = sys.modules['time'].time() |
---|
1075 | n/a | a = '01234' |
---|
1076 | n/a | c = a[0] |
---|
1077 | n/a | c = a[-1] |
---|
1078 | n/a | s = a[0:5] |
---|
1079 | n/a | s = a[:5] |
---|
1080 | n/a | s = a[0:] |
---|
1081 | n/a | s = a[:] |
---|
1082 | n/a | s = a[-5:] |
---|
1083 | n/a | s = a[:-1] |
---|
1084 | n/a | s = a[-4:-3] |
---|
1085 | n/a | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
---|
1086 | n/a | # The testing here is fairly incomplete. |
---|
1087 | n/a | # Test cases should include: commas with 1 and 2 colons |
---|
1088 | n/a | d = {} |
---|
1089 | n/a | d[1] = 1 |
---|
1090 | n/a | d[1,] = 2 |
---|
1091 | n/a | d[1,2] = 3 |
---|
1092 | n/a | d[1,2,3] = 4 |
---|
1093 | n/a | L = list(d) |
---|
1094 | n/a | L.sort(key=lambda x: x if isinstance(x, tuple) else ()) |
---|
1095 | n/a | self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
---|
1096 | n/a | |
---|
1097 | n/a | def test_atoms(self): |
---|
1098 | n/a | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING |
---|
1099 | n/a | ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
---|
1100 | n/a | |
---|
1101 | n/a | x = (1) |
---|
1102 | n/a | x = (1 or 2 or 3) |
---|
1103 | n/a | x = (1 or 2 or 3, 2, 3) |
---|
1104 | n/a | |
---|
1105 | n/a | x = [] |
---|
1106 | n/a | x = [1] |
---|
1107 | n/a | x = [1 or 2 or 3] |
---|
1108 | n/a | x = [1 or 2 or 3, 2, 3] |
---|
1109 | n/a | x = [] |
---|
1110 | n/a | |
---|
1111 | n/a | x = {} |
---|
1112 | n/a | x = {'one': 1} |
---|
1113 | n/a | x = {'one': 1,} |
---|
1114 | n/a | x = {'one' or 'two': 1 or 2} |
---|
1115 | n/a | x = {'one': 1, 'two': 2} |
---|
1116 | n/a | x = {'one': 1, 'two': 2,} |
---|
1117 | n/a | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
---|
1118 | n/a | |
---|
1119 | n/a | x = {'one'} |
---|
1120 | n/a | x = {'one', 1,} |
---|
1121 | n/a | x = {'one', 'two', 'three'} |
---|
1122 | n/a | x = {2, 3, 4,} |
---|
1123 | n/a | |
---|
1124 | n/a | x = x |
---|
1125 | n/a | x = 'x' |
---|
1126 | n/a | x = 123 |
---|
1127 | n/a | |
---|
1128 | n/a | ### exprlist: expr (',' expr)* [','] |
---|
1129 | n/a | ### testlist: test (',' test)* [','] |
---|
1130 | n/a | # These have been exercised enough above |
---|
1131 | n/a | |
---|
1132 | n/a | def test_classdef(self): |
---|
1133 | n/a | # 'class' NAME ['(' [testlist] ')'] ':' suite |
---|
1134 | n/a | class B: pass |
---|
1135 | n/a | class B2(): pass |
---|
1136 | n/a | class C1(B): pass |
---|
1137 | n/a | class C2(B): pass |
---|
1138 | n/a | class D(C1, C2, B): pass |
---|
1139 | n/a | class C: |
---|
1140 | n/a | def meth1(self): pass |
---|
1141 | n/a | def meth2(self, arg): pass |
---|
1142 | n/a | def meth3(self, a1, a2): pass |
---|
1143 | n/a | |
---|
1144 | n/a | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
---|
1145 | n/a | # decorators: decorator+ |
---|
1146 | n/a | # decorated: decorators (classdef | funcdef) |
---|
1147 | n/a | def class_decorator(x): return x |
---|
1148 | n/a | @class_decorator |
---|
1149 | n/a | class G: pass |
---|
1150 | n/a | |
---|
1151 | n/a | def test_dictcomps(self): |
---|
1152 | n/a | # dictorsetmaker: ( (test ':' test (comp_for | |
---|
1153 | n/a | # (',' test ':' test)* [','])) | |
---|
1154 | n/a | # (test (comp_for | (',' test)* [','])) ) |
---|
1155 | n/a | nums = [1, 2, 3] |
---|
1156 | n/a | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
---|
1157 | n/a | |
---|
1158 | n/a | def test_listcomps(self): |
---|
1159 | n/a | # list comprehension tests |
---|
1160 | n/a | nums = [1, 2, 3, 4, 5] |
---|
1161 | n/a | strs = ["Apple", "Banana", "Coconut"] |
---|
1162 | n/a | spcs = [" Apple", " Banana ", "Coco nut "] |
---|
1163 | n/a | |
---|
1164 | n/a | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
---|
1165 | n/a | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
---|
1166 | n/a | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
---|
1167 | n/a | self.assertEqual([(i, s) for i in nums for s in strs], |
---|
1168 | n/a | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
---|
1169 | n/a | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
---|
1170 | n/a | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
---|
1171 | n/a | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
---|
1172 | n/a | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
---|
1173 | n/a | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
---|
1174 | n/a | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
---|
1175 | n/a | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
---|
1176 | n/a | (5, 'Banana'), (5, 'Coconut')]) |
---|
1177 | n/a | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
---|
1178 | n/a | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
---|
1179 | n/a | |
---|
1180 | n/a | def test_in_func(l): |
---|
1181 | n/a | return [0 < x < 3 for x in l if x > 2] |
---|
1182 | n/a | |
---|
1183 | n/a | self.assertEqual(test_in_func(nums), [False, False, False]) |
---|
1184 | n/a | |
---|
1185 | n/a | def test_nested_front(): |
---|
1186 | n/a | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
---|
1187 | n/a | [[1, 2], [3, 4], [5, 6]]) |
---|
1188 | n/a | |
---|
1189 | n/a | test_nested_front() |
---|
1190 | n/a | |
---|
1191 | n/a | check_syntax_error(self, "[i, s for i in nums for s in strs]") |
---|
1192 | n/a | check_syntax_error(self, "[x if y]") |
---|
1193 | n/a | |
---|
1194 | n/a | suppliers = [ |
---|
1195 | n/a | (1, "Boeing"), |
---|
1196 | n/a | (2, "Ford"), |
---|
1197 | n/a | (3, "Macdonalds") |
---|
1198 | n/a | ] |
---|
1199 | n/a | |
---|
1200 | n/a | parts = [ |
---|
1201 | n/a | (10, "Airliner"), |
---|
1202 | n/a | (20, "Engine"), |
---|
1203 | n/a | (30, "Cheeseburger") |
---|
1204 | n/a | ] |
---|
1205 | n/a | |
---|
1206 | n/a | suppart = [ |
---|
1207 | n/a | (1, 10), (1, 20), (2, 20), (3, 30) |
---|
1208 | n/a | ] |
---|
1209 | n/a | |
---|
1210 | n/a | x = [ |
---|
1211 | n/a | (sname, pname) |
---|
1212 | n/a | for (sno, sname) in suppliers |
---|
1213 | n/a | for (pno, pname) in parts |
---|
1214 | n/a | for (sp_sno, sp_pno) in suppart |
---|
1215 | n/a | if sno == sp_sno and pno == sp_pno |
---|
1216 | n/a | ] |
---|
1217 | n/a | |
---|
1218 | n/a | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
---|
1219 | n/a | ('Macdonalds', 'Cheeseburger')]) |
---|
1220 | n/a | |
---|
1221 | n/a | def test_genexps(self): |
---|
1222 | n/a | # generator expression tests |
---|
1223 | n/a | g = ([x for x in range(10)] for x in range(1)) |
---|
1224 | n/a | self.assertEqual(next(g), [x for x in range(10)]) |
---|
1225 | n/a | try: |
---|
1226 | n/a | next(g) |
---|
1227 | n/a | self.fail('should produce StopIteration exception') |
---|
1228 | n/a | except StopIteration: |
---|
1229 | n/a | pass |
---|
1230 | n/a | |
---|
1231 | n/a | a = 1 |
---|
1232 | n/a | try: |
---|
1233 | n/a | g = (a for d in a) |
---|
1234 | n/a | next(g) |
---|
1235 | n/a | self.fail('should produce TypeError') |
---|
1236 | n/a | except TypeError: |
---|
1237 | n/a | pass |
---|
1238 | n/a | |
---|
1239 | n/a | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
---|
1240 | n/a | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
---|
1241 | n/a | |
---|
1242 | n/a | a = [x for x in range(10)] |
---|
1243 | n/a | b = (x for x in (y for y in a)) |
---|
1244 | n/a | self.assertEqual(sum(b), sum([x for x in range(10)])) |
---|
1245 | n/a | |
---|
1246 | n/a | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
---|
1247 | n/a | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
---|
1248 | n/a | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
---|
1249 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
---|
1250 | n/a | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
---|
1251 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)])) |
---|
1252 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
---|
1253 | n/a | check_syntax_error(self, "foo(x for x in range(10), 100)") |
---|
1254 | n/a | check_syntax_error(self, "foo(100, x for x in range(10))") |
---|
1255 | n/a | |
---|
1256 | n/a | def test_comprehension_specials(self): |
---|
1257 | n/a | # test for outmost iterable precomputation |
---|
1258 | n/a | x = 10; g = (i for i in range(x)); x = 5 |
---|
1259 | n/a | self.assertEqual(len(list(g)), 10) |
---|
1260 | n/a | |
---|
1261 | n/a | # This should hold, since we're only precomputing outmost iterable. |
---|
1262 | n/a | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
---|
1263 | n/a | x = 5; t = True; |
---|
1264 | n/a | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
---|
1265 | n/a | |
---|
1266 | n/a | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
---|
1267 | n/a | # even though it's silly. Make sure it works (ifelse broke this.) |
---|
1268 | n/a | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
---|
1269 | n/a | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
---|
1270 | n/a | |
---|
1271 | n/a | # verify unpacking single element tuples in listcomp/genexp. |
---|
1272 | n/a | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
---|
1273 | n/a | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
---|
1274 | n/a | |
---|
1275 | n/a | def test_with_statement(self): |
---|
1276 | n/a | class manager(object): |
---|
1277 | n/a | def __enter__(self): |
---|
1278 | n/a | return (1, 2) |
---|
1279 | n/a | def __exit__(self, *args): |
---|
1280 | n/a | pass |
---|
1281 | n/a | |
---|
1282 | n/a | with manager(): |
---|
1283 | n/a | pass |
---|
1284 | n/a | with manager() as x: |
---|
1285 | n/a | pass |
---|
1286 | n/a | with manager() as (x, y): |
---|
1287 | n/a | pass |
---|
1288 | n/a | with manager(), manager(): |
---|
1289 | n/a | pass |
---|
1290 | n/a | with manager() as x, manager() as y: |
---|
1291 | n/a | pass |
---|
1292 | n/a | with manager() as x, manager(): |
---|
1293 | n/a | pass |
---|
1294 | n/a | |
---|
1295 | n/a | def test_if_else_expr(self): |
---|
1296 | n/a | # Test ifelse expressions in various cases |
---|
1297 | n/a | def _checkeval(msg, ret): |
---|
1298 | n/a | "helper to check that evaluation of expressions is done correctly" |
---|
1299 | n/a | print(msg) |
---|
1300 | n/a | return ret |
---|
1301 | n/a | |
---|
1302 | n/a | # the next line is not allowed anymore |
---|
1303 | n/a | #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |
---|
1304 | n/a | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
---|
1305 | n/a | self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) |
---|
1306 | n/a | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
---|
1307 | n/a | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
---|
1308 | n/a | self.assertEqual((5 and 6 if 0 else 1), 1) |
---|
1309 | n/a | self.assertEqual(((5 and 6) if 0 else 1), 1) |
---|
1310 | n/a | self.assertEqual((5 and (6 if 1 else 1)), 6) |
---|
1311 | n/a | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
---|
1312 | n/a | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
---|
1313 | n/a | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
---|
1314 | n/a | self.assertEqual((not 5 if 1 else 1), False) |
---|
1315 | n/a | self.assertEqual((not 5 if 0 else 1), 1) |
---|
1316 | n/a | self.assertEqual((6 + 1 if 1 else 2), 7) |
---|
1317 | n/a | self.assertEqual((6 - 1 if 1 else 2), 5) |
---|
1318 | n/a | self.assertEqual((6 * 2 if 1 else 4), 12) |
---|
1319 | n/a | self.assertEqual((6 / 2 if 1 else 3), 3) |
---|
1320 | n/a | self.assertEqual((6 < 4 if 0 else 2), 2) |
---|
1321 | n/a | |
---|
1322 | n/a | def test_paren_evaluation(self): |
---|
1323 | n/a | self.assertEqual(16 // (4 // 2), 8) |
---|
1324 | n/a | self.assertEqual((16 // 4) // 2, 2) |
---|
1325 | n/a | self.assertEqual(16 // 4 // 2, 2) |
---|
1326 | n/a | self.assertTrue(False is (2 is 3)) |
---|
1327 | n/a | self.assertFalse((False is 2) is 3) |
---|
1328 | n/a | self.assertFalse(False is 2 is 3) |
---|
1329 | n/a | |
---|
1330 | n/a | def test_matrix_mul(self): |
---|
1331 | n/a | # This is not intended to be a comprehensive test, rather just to be few |
---|
1332 | n/a | # samples of the @ operator in test_grammar.py. |
---|
1333 | n/a | class M: |
---|
1334 | n/a | def __matmul__(self, o): |
---|
1335 | n/a | return 4 |
---|
1336 | n/a | def __imatmul__(self, o): |
---|
1337 | n/a | self.other = o |
---|
1338 | n/a | return self |
---|
1339 | n/a | m = M() |
---|
1340 | n/a | self.assertEqual(m @ m, 4) |
---|
1341 | n/a | m @= 42 |
---|
1342 | n/a | self.assertEqual(m.other, 42) |
---|
1343 | n/a | |
---|
1344 | n/a | def test_async_await(self): |
---|
1345 | n/a | async def test(): |
---|
1346 | n/a | def sum(): |
---|
1347 | n/a | pass |
---|
1348 | n/a | if 1: |
---|
1349 | n/a | await someobj() |
---|
1350 | n/a | |
---|
1351 | n/a | self.assertEqual(test.__name__, 'test') |
---|
1352 | n/a | self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE)) |
---|
1353 | n/a | |
---|
1354 | n/a | def decorator(func): |
---|
1355 | n/a | setattr(func, '_marked', True) |
---|
1356 | n/a | return func |
---|
1357 | n/a | |
---|
1358 | n/a | @decorator |
---|
1359 | n/a | async def test2(): |
---|
1360 | n/a | return 22 |
---|
1361 | n/a | self.assertTrue(test2._marked) |
---|
1362 | n/a | self.assertEqual(test2.__name__, 'test2') |
---|
1363 | n/a | self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE)) |
---|
1364 | n/a | |
---|
1365 | n/a | def test_async_for(self): |
---|
1366 | n/a | class Done(Exception): pass |
---|
1367 | n/a | |
---|
1368 | n/a | class AIter: |
---|
1369 | n/a | def __aiter__(self): |
---|
1370 | n/a | return self |
---|
1371 | n/a | async def __anext__(self): |
---|
1372 | n/a | raise StopAsyncIteration |
---|
1373 | n/a | |
---|
1374 | n/a | async def foo(): |
---|
1375 | n/a | async for i in AIter(): |
---|
1376 | n/a | pass |
---|
1377 | n/a | async for i, j in AIter(): |
---|
1378 | n/a | pass |
---|
1379 | n/a | async for i in AIter(): |
---|
1380 | n/a | pass |
---|
1381 | n/a | else: |
---|
1382 | n/a | pass |
---|
1383 | n/a | raise Done |
---|
1384 | n/a | |
---|
1385 | n/a | with self.assertRaises(Done): |
---|
1386 | n/a | foo().send(None) |
---|
1387 | n/a | |
---|
1388 | n/a | def test_async_with(self): |
---|
1389 | n/a | class Done(Exception): pass |
---|
1390 | n/a | |
---|
1391 | n/a | class manager: |
---|
1392 | n/a | async def __aenter__(self): |
---|
1393 | n/a | return (1, 2) |
---|
1394 | n/a | async def __aexit__(self, *exc): |
---|
1395 | n/a | return False |
---|
1396 | n/a | |
---|
1397 | n/a | async def foo(): |
---|
1398 | n/a | async with manager(): |
---|
1399 | n/a | pass |
---|
1400 | n/a | async with manager() as x: |
---|
1401 | n/a | pass |
---|
1402 | n/a | async with manager() as (x, y): |
---|
1403 | n/a | pass |
---|
1404 | n/a | async with manager(), manager(): |
---|
1405 | n/a | pass |
---|
1406 | n/a | async with manager() as x, manager() as y: |
---|
1407 | n/a | pass |
---|
1408 | n/a | async with manager() as x, manager(): |
---|
1409 | n/a | pass |
---|
1410 | n/a | raise Done |
---|
1411 | n/a | |
---|
1412 | n/a | with self.assertRaises(Done): |
---|
1413 | n/a | foo().send(None) |
---|
1414 | n/a | |
---|
1415 | n/a | |
---|
1416 | n/a | if __name__ == '__main__': |
---|
1417 | n/a | unittest.main() |
---|