Debian Codegraph @

Lib/test/test_grammar.py

raw
1 # Python test set -- part 1, grammar.
2 # This just tests whether the parser accepts them all.
3
4 from test.support import check_syntax_error, skip_wasi_stack_overflow
5 from test.support import import_helper
6 import annotationlib
7 import inspect
8 import unittest
9 import sys
10 import textwrap
11 import warnings
12 # testing import *
13 from sys import *
14
15 # different import patterns to check that __annotations__ does not interfere
16 # with import machinery
17 import test.typinganndata.ann_module as ann_module
18 import typing
19 from test.typinganndata import ann_module2
20 import test
21 from test.support.numbers import (
22 VALID_UNDERSCORE_LITERALS,
23 INVALID_UNDERSCORE_LITERALS,
24 )
25
26 class TokenTests(unittest.TestCase):
27
28 from test.support import check_syntax_error
29 from test.support.warnings_helper import check_syntax_warning
30
31 def test_backslash(self):
32 # Backslash means line continuation:
33 x = 1 \
34 + 1
35 self.assertEqual(x, 2, 'backslash for line continuation')
36
37 # Backslash does not means continuation in comments :\
38 x = 0
39 self.assertEqual(x, 0, 'backslash ending comment')
40
41 def test_plain_integers(self):
42 self.assertEqual(type(000), type(0))
43 self.assertEqual(0xff, 255)
44 self.assertEqual(0o377, 255)
45 self.assertEqual(2147483647, 0o17777777777)
46 self.assertEqual(0b1001, 9)
47 # "0x" is not a valid literal
48 self.assertRaises(SyntaxError, eval, "0x")
49 from sys import maxsize
50 if maxsize == 2147483647:
51 self.assertEqual(-2147483647-1, -0o20000000000)
52 # XXX -2147483648
53 self.assertTrue(0o37777777777 > 0)
54 self.assertTrue(0xffffffff > 0)
55 self.assertTrue(0b1111111111111111111111111111111 > 0)
56 for s in ('2147483648', '0o40000000000', '0x100000000',
57 '0b10000000000000000000000000000000'):
58 try:
59 x = eval(s)
60 except OverflowError:
61 self.fail("OverflowError on huge integer literal %r" % s)
62 elif maxsize == 9223372036854775807:
63 self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
64 self.assertTrue(0o1777777777777777777777 > 0)
65 self.assertTrue(0xffffffffffffffff > 0)
66 self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
67 for s in '9223372036854775808', '0o2000000000000000000000', \
68 '0x10000000000000000', \
69 '0b100000000000000000000000000000000000000000000000000000000000000':
70 try:
71 x = eval(s)
72 except OverflowError:
73 self.fail("OverflowError on huge integer literal %r" % s)
74 else:
75 self.fail('Weird maxsize value %r' % maxsize)
76
77 def test_long_integers(self):
78 x = 0
79 x = 0xffffffffffffffff
80 x = 0Xffffffffffffffff
81 x = 0o77777777777777777
82 x = 0O77777777777777777
83 x = 123456789012345678901234567890
84 x = 0b100000000000000000000000000000000000000000000000000000000000000000000
85 x = 0B111111111111111111111111111111111111111111111111111111111111111111111
86
87 def test_floats(self):
88 x = 3.14
89 x = 314.
90 x = 0.314
91 x = 000.314
92 x = .314
93 x = 3e14
94 x = 3E14
95 x = 3e-14
96 x = 3e+14
97 x = 3.e14
98 x = .3e14
99 x = 3.1e4
100
101 def test_float_exponent_tokenization(self):
102 # See issue 21642.
103 with warnings.catch_warnings():
104 warnings.simplefilter('ignore', SyntaxWarning)
105 self.assertEqual(eval("1 if 1else 0"), 1)
106 self.assertEqual(eval("1 if 0else 0"), 0)
107 self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
108
109 def test_underscore_literals(self):
110 for lit in VALID_UNDERSCORE_LITERALS:
111 self.assertEqual(eval(lit), eval(lit.replace('_', '')))
112 for lit in INVALID_UNDERSCORE_LITERALS:
113 self.assertRaises(SyntaxError, eval, lit)
114 # Sanity check: no literal begins with an underscore
115 self.assertRaises(NameError, eval, "_0")
116
117 def test_bad_numerical_literals(self):
118 check = self.check_syntax_error
119 check("0b12", "invalid digit '2' in binary literal")
120 check("0b1_2", "invalid digit '2' in binary literal")
121 check("0b2", "invalid digit '2' in binary literal")
122 check("0b1_", "invalid binary literal")
123 check("0b", "invalid binary literal")
124 check("0o18", "invalid digit '8' in octal literal")
125 check("0o1_8", "invalid digit '8' in octal literal")
126 check("0o8", "invalid digit '8' in octal literal")
127 check("0o1_", "invalid octal literal")
128 check("0o", "invalid octal literal")
129 check("0x1_", "invalid hexadecimal literal")
130 check("0x", "invalid hexadecimal literal")
131 check("1_", "invalid decimal literal")
132 check("012",
133 "leading zeros in decimal integer literals are not permitted; "
134 "use an 0o prefix for octal integers")
135 check("1.2_", "invalid decimal literal")
136 check("1e2_", "invalid decimal literal")
137 check("1e+", "invalid decimal literal")
138
139 def test_end_of_numerical_literals(self):
140 def check(test, error=False):
141 with self.subTest(expr=test):
142 if error:
143 with warnings.catch_warnings(record=True) as w:
144 with self.assertRaisesRegex(SyntaxError,
145 r'invalid \w+ literal'):
146 compile(test, "<testcase>", "eval")
147 self.assertEqual(w, [])
148 else:
149 self.check_syntax_warning(test,
150 errtext=r'invalid \w+ literal')
151
152 for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j":
153 compile(num, "<testcase>", "eval")
154 check(f"{num}and x", error=(num == "0xf"))
155 check(f"{num}or x", error=(num == "0"))
156 check(f"{num}in x")
157 check(f"{num}not in x")
158 check(f"{num}if x else y")
159 check(f"x if {num}else y", error=(num == "0xf"))
160 check(f"[{num}for x in ()]")
161 check(f"{num}spam", error=True)
162
163 # gh-88943: Invalid non-ASCII character following a numerical literal.
164 with self.assertRaisesRegex(SyntaxError, r"invalid character '⁄' \(U\+2044\)"):
165 compile(f"{num}⁄7", "<testcase>", "eval")
166
167 with self.assertWarnsRegex(SyntaxWarning, r'invalid \w+ literal'):
168 compile(f"{num}is x", "<testcase>", "eval")
169 with warnings.catch_warnings():
170 warnings.simplefilter('error', SyntaxWarning)
171 with self.assertRaisesRegex(SyntaxError,
172 r'invalid \w+ literal'):
173 compile(f"{num}is x", "<testcase>", "eval")
174
175 check("[0x1ffor x in ()]")
176 check("[0x1for x in ()]")
177 check("[0xfor x in ()]")
178
179 def test_string_literals(self):
180 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
181 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
182 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
183 x = "doesn't \"shrink\" does it"
184 y = 'doesn\'t "shrink" does it'
185 self.assertTrue(len(x) == 24 and x == y)
186 x = "does \"shrink\" doesn't it"
187 y = 'does "shrink" doesn\'t it'
188 self.assertTrue(len(x) == 24 and x == y)
189 x = """
190 The "quick"
191 brown fox
192 jumps over
193 the 'lazy' dog.
194 """
195 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
196 self.assertEqual(x, y)
197 y = '''
198 The "quick"
199 brown fox
200 jumps over
201 the 'lazy' dog.
202 '''
203 self.assertEqual(x, y)
204 y = "\n\
205 The \"quick\"\n\
206 brown fox\n\
207 jumps over\n\
208 the 'lazy' dog.\n\
209 "
210 self.assertEqual(x, y)
211 y = '\n\
212 The \"quick\"\n\
213 brown fox\n\
214 jumps over\n\
215 the \'lazy\' dog.\n\
216 '
217 self.assertEqual(x, y)
218
219 def test_string_prefixes(self):
220 def check(s):
221 parsed = eval(s)
222 self.assertIs(type(parsed), str)
223 self.assertGreater(len(parsed), 0)
224
225 check("u'abc'")
226 check("r'abc\t'")
227 check("rf'abc\a {1 + 1}'")
228 check("fr'abc\a {1 + 1}'")
229
230 def test_bytes_prefixes(self):
231 def check(s):
232 parsed = eval(s)
233 self.assertIs(type(parsed), bytes)
234 self.assertGreater(len(parsed), 0)
235
236 check("b'abc'")
237 check("br'abc\t'")
238 check("rb'abc\a'")
239
240 def test_ellipsis(self):
241 x = ...
242 self.assertTrue(x is Ellipsis)
243 self.assertRaises(SyntaxError, eval, ".. .")
244
245 def test_eof_error(self):
246 samples = ("def foo(", "\ndef foo(", "def foo(\n")
247 for s in samples:
248 with self.assertRaises(SyntaxError) as cm:
249 compile(s, "<test>", "exec")
250 self.assertIn("was never closed", str(cm.exception))
251
252 @skip_wasi_stack_overflow()
253 def test_max_level(self):
254 # Macro defined in Parser/lexer/state.h
255 MAXLEVEL = 200
256
257 result = eval("(" * MAXLEVEL + ")" * MAXLEVEL)
258 self.assertEqual(result, ())
259
260 with self.assertRaises(SyntaxError) as cm:
261 eval("(" * (MAXLEVEL + 1) + ")" * (MAXLEVEL + 1))
262 self.assertStartsWith(str(cm.exception), 'too many nested parentheses')
263
264 var_annot_global: int # a global annotated is necessary for test_var_annot
265
266
267 class GrammarTests(unittest.TestCase):
268
269 from test.support import check_syntax_error
270 from test.support.warnings_helper import check_syntax_warning
271 from test.support.warnings_helper import check_no_warnings
272
273 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
274 # XXX can't test in a script -- this rule is only used when interactive
275
276 # file_input: (NEWLINE | stmt)* ENDMARKER
277 # Being tested as this very moment this very module
278
279 # expr_input: testlist NEWLINE
280 # XXX Hard to test -- used only in calls to input()
281
282 def test_eval_input(self):
283 # testlist ENDMARKER
284 x = eval('1, 0 or 1')
285
286 def test_var_annot_basics(self):
287 # all these should be allowed
288 var1: int = 5
289 var2: [int, str]
290 my_lst = [42]
291 def one():
292 return 1
293 int.new_attr: int
294 [list][0]: type
295 my_lst[one()-1]: int = 5
296 self.assertEqual(my_lst, [5])
297
298 def test_var_annot_syntax_errors(self):
299 # parser pass
300 check_syntax_error(self, "def f: int")
301 check_syntax_error(self, "x: int: str")
302 check_syntax_error(self, "def f():\n"
303 " nonlocal x: int\n")
304 check_syntax_error(self, "def f():\n"
305 " global x: int\n")
306 check_syntax_error(self, "x: int = y = 1")
307 check_syntax_error(self, "z = w: int = 1")
308 check_syntax_error(self, "x: int = y: int = 1")
309 # AST pass
310 check_syntax_error(self, "[x, 0]: int\n")
311 check_syntax_error(self, "f(): int\n")
312 check_syntax_error(self, "(x,): int")
313 check_syntax_error(self, "def f():\n"
314 " (x, y): int = (1, 2)\n")
315 # symtable pass
316 check_syntax_error(self, "def f():\n"
317 " x: int\n"
318 " global x\n")
319 check_syntax_error(self, "def f():\n"
320 " global x\n"
321 " x: int\n")
322 check_syntax_error(self, "def f():\n"
323 " x: int\n"
324 " nonlocal x\n")
325 check_syntax_error(self, "def f():\n"
326 " nonlocal x\n"
327 " x: int\n")
328
329 def test_var_annot_basic_semantics(self):
330 # execution order
331 with self.assertRaises(ZeroDivisionError):
332 no_name[does_not_exist]: no_name_again = 1/0
333 with self.assertRaises(NameError):
334 no_name[does_not_exist]: 1/0 = 0
335 global var_annot_global
336
337 # function semantics
338 def f():
339 st: str = "Hello"
340 a.b: int = (1, 2)
341 return st
342 self.assertEqual(f.__annotations__, {})
343 def f_OK():
344 x: 1/0
345 f_OK()
346 def fbad():
347 x: int
348 print(x)
349 with self.assertRaises(UnboundLocalError):
350 fbad()
351 def f2bad():
352 (no_such_global): int
353 print(no_such_global)
354 try:
355 f2bad()
356 except Exception as e:
357 self.assertIs(type(e), NameError)
358
359 # class semantics
360 class C:
361 __foo: int
362 s: str = "attr"
363 z = 2
364 def __init__(self, x):
365 self.x: int = x
366 self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
367 with self.assertRaises(NameError):
368 class CBad:
369 no_such_name_defined.attr: int = 0
370 with self.assertRaises(NameError):
371 class Cbad2(C):
372 x: int
373 x.y: list = []
374
375 def test_annotations_inheritance(self):
376 # Check that annotations are not inherited by derived classes
377 class A:
378 attr: int
379 class B(A):
380 pass
381 class C(A):
382 attr: str
383 class D:
384 attr2: int
385 class E(A, D):
386 pass
387 class F(C, A):
388 pass
389 self.assertEqual(A.__annotations__, {"attr": int})
390 self.assertEqual(B.__annotations__, {})
391 self.assertEqual(C.__annotations__, {"attr" : str})
392 self.assertEqual(D.__annotations__, {"attr2" : int})
393 self.assertEqual(E.__annotations__, {})
394 self.assertEqual(F.__annotations__, {})
395
396 def test_var_annot_module_semantics(self):
397 self.assertEqual(test.__annotations__, {})
398 self.assertEqual(ann_module.__annotations__,
399 {'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float})
400 self.assertEqual(ann_module.M.__annotations__,
401 {'o': type})
402 self.assertEqual(ann_module2.__annotations__, {})
403
404 def test_var_annot_in_module(self):
405 # check that functions fail the same way when executed
406 # outside of module where they were defined
407 ann_module3 = import_helper.import_fresh_module("test.typinganndata.ann_module3")
408 with self.assertRaises(NameError):
409 ann_module3.f_bad_ann()
410 with self.assertRaises(NameError):
411 ann_module3.g_bad_ann()
412 with self.assertRaises(NameError):
413 ann_module3.D_bad_ann(5)
414
415 def test_var_annot_simple_exec(self):
416 gns = {}; lns = {}
417 exec("'docstring'\n"
418 "x: int = 5\n", gns, lns)
419 self.assertNotIn('__annotate__', gns)
420
421 gns.update(lns) # __annotate__ looks at globals
422 self.assertEqual(lns["__annotate__"](annotationlib.Format.VALUE), {'x': int})
423
424 def test_var_annot_rhs(self):
425 ns = {}
426 exec('x: tuple = 1, 2', ns)
427 self.assertEqual(ns['x'], (1, 2))
428 stmt = ('def f():\n'
429 ' x: int = yield')
430 exec(stmt, ns)
431 self.assertEqual(list(ns['f']()), [None])
432
433 ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple}
434 exec('x: Tuple[int, ...] = a,*b,c', ns)
435 self.assertEqual(ns['x'], (1, 2, 3, 4, 5))
436
437 def test_funcdef(self):
438 ### [decorators] 'def' NAME parameters ['->' test] ':' suite
439 ### decorator: '@' namedexpr_test NEWLINE
440 ### decorators: decorator+
441 ### parameters: '(' [typedargslist] ')'
442 ### typedargslist: ((tfpdef ['=' test] ',')*
443 ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
444 ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
445 ### tfpdef: NAME [':' test]
446 ### varargslist: ((vfpdef ['=' test] ',')*
447 ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
448 ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
449 ### vfpdef: NAME
450 def f1(): pass
451 f1()
452 f1(*())
453 f1(*(), **{})
454 def f2(one_argument): pass
455 def f3(two, arguments): pass
456 self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
457 self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
458 def a1(one_arg,): pass
459 def a2(two, args,): pass
460 def v0(*rest): pass
461 def v1(a, *rest): pass
462 def v2(a, b, *rest): pass
463
464 f1()
465 f2(1)
466 f2(1,)
467 f3(1, 2)
468 f3(1, 2,)
469 v0()
470 v0(1)
471 v0(1,)
472 v0(1,2)
473 v0(1,2,3,4,5,6,7,8,9,0)
474 v1(1)
475 v1(1,)
476 v1(1,2)
477 v1(1,2,3)
478 v1(1,2,3,4,5,6,7,8,9,0)
479 v2(1,2)
480 v2(1,2,3)
481 v2(1,2,3,4)
482 v2(1,2,3,4,5,6,7,8,9,0)
483
484 def d01(a=1): pass
485 d01()
486 d01(1)
487 d01(*(1,))
488 d01(*[] or [2])
489 d01(*() or (), *{} and (), **() or {})
490 d01(**{'a':2})
491 d01(**{'a':2} or {})
492 def d11(a, b=1): pass
493 d11(1)
494 d11(1, 2)
495 d11(1, **{'b':2})
496 def d21(a, b, c=1): pass
497 d21(1, 2)
498 d21(1, 2, 3)
499 d21(*(1, 2, 3))
500 d21(1, *(2, 3))
501 d21(1, 2, *(3,))
502 d21(1, 2, **{'c':3})
503 def d02(a=1, b=2): pass
504 d02()
505 d02(1)
506 d02(1, 2)
507 d02(*(1, 2))
508 d02(1, *(2,))
509 d02(1, **{'b':2})
510 d02(**{'a': 1, 'b': 2})
511 def d12(a, b=1, c=2): pass
512 d12(1)
513 d12(1, 2)
514 d12(1, 2, 3)
515 def d22(a, b, c=1, d=2): pass
516 d22(1, 2)
517 d22(1, 2, 3)
518 d22(1, 2, 3, 4)
519 def d01v(a=1, *rest): pass
520 d01v()
521 d01v(1)
522 d01v(1, 2)
523 d01v(*(1, 2, 3, 4))
524 d01v(*(1,))
525 d01v(**{'a':2})
526 def d11v(a, b=1, *rest): pass
527 d11v(1)
528 d11v(1, 2)
529 d11v(1, 2, 3)
530 def d21v(a, b, c=1, *rest): pass
531 d21v(1, 2)
532 d21v(1, 2, 3)
533 d21v(1, 2, 3, 4)
534 d21v(*(1, 2, 3, 4))
535 d21v(1, 2, **{'c': 3})
536 def d02v(a=1, b=2, *rest): pass
537 d02v()
538 d02v(1)
539 d02v(1, 2)
540 d02v(1, 2, 3)
541 d02v(1, *(2, 3, 4))
542 d02v(**{'a': 1, 'b': 2})
543 def d12v(a, b=1, c=2, *rest): pass
544 d12v(1)
545 d12v(1, 2)
546 d12v(1, 2, 3)
547 d12v(1, 2, 3, 4)
548 d12v(*(1, 2, 3, 4))
549 d12v(1, 2, *(3, 4, 5))
550 d12v(1, *(2,), **{'c': 3})
551 def d22v(a, b, c=1, d=2, *rest): pass
552 d22v(1, 2)
553 d22v(1, 2, 3)
554 d22v(1, 2, 3, 4)
555 d22v(1, 2, 3, 4, 5)
556 d22v(*(1, 2, 3, 4))
557 d22v(1, 2, *(3, 4, 5))
558 d22v(1, *(2, 3), **{'d': 4})
559
560 # keyword argument type tests
561 with warnings.catch_warnings():
562 warnings.simplefilter('ignore', BytesWarning)
563 try:
564 str('x', **{b'foo':1 })
565 except TypeError:
566 pass
567 else:
568 self.fail('Bytes should not work as keyword argument names')
569 # keyword only argument tests
570 def pos0key1(*, key): return key
571 pos0key1(key=100)
572 def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
573 pos2key2(1, 2, k1=100)
574 pos2key2(1, 2, k1=100, k2=200)
575 pos2key2(1, 2, k2=100, k1=200)
576 def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
577 pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
578 pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
579
580 self.assertRaises(SyntaxError, eval, "def f(*): pass")
581 self.assertRaises(SyntaxError, eval, "def f(*,): pass")
582 self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
583
584 # keyword arguments after *arglist
585 def f(*args, **kwargs):
586 return args, kwargs
587 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
588 {'x':2, 'y':5}))
589 self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
590 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
591 self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
592 ((), {'eggs':'scrambled', 'spam':'fried'}))
593 self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
594 ((), {'eggs':'scrambled', 'spam':'fried'}))
595
596 # Check ast errors in *args and *kwargs
597 check_syntax_error(self, "f(*g(1=2))")
598 check_syntax_error(self, "f(**g(1=2))")
599
600 # argument annotation tests
601 def f(x) -> list: pass
602 self.assertEqual(f.__annotations__, {'return': list})
603 def f(x: int): pass
604 self.assertEqual(f.__annotations__, {'x': int})
605 def f(x: int, /): pass
606 self.assertEqual(f.__annotations__, {'x': int})
607 def f(x: int = 34, /): pass
608 self.assertEqual(f.__annotations__, {'x': int})
609 def f(*x: str): pass
610 self.assertEqual(f.__annotations__, {'x': str})
611 def f(**x: float): pass
612 self.assertEqual(f.__annotations__, {'x': float})
613 def f(x, y: 1+2): pass
614 self.assertEqual(f.__annotations__, {'y': 3})
615 def f(x, y: 1+2, /): pass
616 self.assertEqual(f.__annotations__, {'y': 3})
617 def f(a, b: 1, c: 2, d): pass
618 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
619 def f(a, b: 1, /, c: 2, d): pass
620 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
621 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
622 self.assertEqual(f.__annotations__,
623 {'b': 1, 'c': 2, 'e': 3, 'g': 6})
624 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
625 **k: 11) -> 12: pass
626 self.assertEqual(f.__annotations__,
627 {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
628 'k': 11, 'return': 12})
629 def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10,
630 **k: 11) -> 12: pass
631 self.assertEqual(f.__annotations__,
632 {'b': 1, 'c': 2, 'e': 3, 'f': int, 'g': 6, 'h': 7, 'j': 9,
633 'k': 11, 'return': 12})
634 # Check for issue #20625 -- annotations mangling
635 class Spam:
636 def f(self, *, __kw: 1):
637 pass
638 class Ham(Spam): pass
639 self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
640 self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
641 # Check for SF Bug #1697248 - mixing decorators and a return annotation
642 def null(x): return x
643 @null
644 def f(x) -> list: pass
645 self.assertEqual(f.__annotations__, {'return': list})
646
647 # Test expressions as decorators (PEP 614):
648 @False or null
649 def f(x): pass
650 @d := null
651 def f(x): pass
652 @lambda f: null(f)
653 def f(x): pass
654 @[..., null, ...][1]
655 def f(x): pass
656 @null(null)(null)
657 def f(x): pass
658 @[null][0].__call__.__call__
659 def f(x): pass
660
661 # test closures with a variety of opargs
662 closure = 1
663 def f(): return closure
664 def f(x=1): return closure
665 def f(*, k=1): return closure
666 def f() -> int: return closure
667
668 # Check trailing commas are permitted in funcdef argument list
669 def f(a,): pass
670 def f(*args,): pass
671 def f(**kwds,): pass
672 def f(a, *args,): pass
673 def f(a, **kwds,): pass
674 def f(*args, b,): pass
675 def f(*, b,): pass
676 def f(*args, **kwds,): pass
677 def f(a, *args, b,): pass
678 def f(a, *, b,): pass
679 def f(a, *args, **kwds,): pass
680 def f(*args, b, **kwds,): pass
681 def f(*, b, **kwds,): pass
682 def f(a, *args, b, **kwds,): pass
683 def f(a, *, b, **kwds,): pass
684
685 def test_lambdef(self):
686 ### lambdef: 'lambda' [varargslist] ':' test
687 l1 = lambda : 0
688 self.assertEqual(l1(), 0)
689 l2 = lambda : a[d] # XXX just testing the expression
690 l3 = lambda : [2 < x for x in [-1, 3, 0]]
691 self.assertEqual(l3(), [0, 1, 0])
692 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
693 self.assertEqual(l4(), 1)
694 l5 = lambda x, y, z=2: x + y + z
695 self.assertEqual(l5(1, 2), 5)
696 self.assertEqual(l5(1, 2, 3), 6)
697 check_syntax_error(self, "lambda x: x = 2")
698 check_syntax_error(self, "lambda (None,): None")
699 l6 = lambda x, y, *, k=20: x+y+k
700 self.assertEqual(l6(1,2), 1+2+20)
701 self.assertEqual(l6(1,2,k=10), 1+2+10)
702
703 # check that trailing commas are permitted
704 l10 = lambda a,: 0
705 l11 = lambda *args,: 0
706 l12 = lambda **kwds,: 0
707 l13 = lambda a, *args,: 0
708 l14 = lambda a, **kwds,: 0
709 l15 = lambda *args, b,: 0
710 l16 = lambda *, b,: 0
711 l17 = lambda *args, **kwds,: 0
712 l18 = lambda a, *args, b,: 0
713 l19 = lambda a, *, b,: 0
714 l20 = lambda a, *args, **kwds,: 0
715 l21 = lambda *args, b, **kwds,: 0
716 l22 = lambda *, b, **kwds,: 0
717 l23 = lambda a, *args, b, **kwds,: 0
718 l24 = lambda a, *, b, **kwds,: 0
719
720
721 ### stmt: simple_stmt | compound_stmt
722 # Tested below
723
724 def test_simple_stmt(self):
725 ### simple_stmt: small_stmt (';' small_stmt)* [';']
726 x = 1; pass; del x
727 def foo():
728 # verify statements that end with semi-colons
729 x = 1; pass; del x;
730 foo()
731
732 ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
733 # Tested below
734
735 def test_expr_stmt(self):
736 # (exprlist '=')* exprlist
737 1
738 1, 2, 3
739 x = 1
740 x = 1, 2, 3
741 x = y = z = 1, 2, 3
742 x, y, z = 1, 2, 3
743 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
744
745 check_syntax_error(self, "x + 1 = 1")
746 check_syntax_error(self, "a + 1 = b + 2")
747
748 # Check the heuristic for print & exec covers significant cases
749 # As well as placing some limits on false positives
750 def test_former_statements_refer_to_builtins(self):
751 keywords = "print", "exec"
752 # Cases where we want the custom error
753 cases = [
754 "{} foo",
755 "{} {{1:foo}}",
756 "if 1: {} foo",
757 "if 1: {} {{1:foo}}",
758 "if 1:\n {} foo",
759 "if 1:\n {} {{1:foo}}",
760 ]
761 for keyword in keywords:
762 custom_msg = "call to '{}'".format(keyword)
763 for case in cases:
764 source = case.format(keyword)
765 with self.subTest(source=source):
766 with self.assertRaisesRegex(SyntaxError, custom_msg):
767 exec(source)
768 source = source.replace("foo", "(foo.)")
769 with self.subTest(source=source):
770 with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
771 exec(source)
772
773 def test_del_stmt(self):
774 # 'del' exprlist
775 abc = [1,2,3]
776 x, y, z = abc
777 xyz = x, y, z
778
779 del abc
780 del x, y, (z, xyz)
781
782 x, y, z = "xyz"
783 del x
784 del y,
785 del (z)
786 del ()
787
788 a, b, c, d, e, f, g = "abcdefg"
789 del a, (b, c), (d, (e, f))
790
791 a, b, c, d, e, f, g = "abcdefg"
792 del a, [b, c], (d, [e, f])
793
794 abcd = list("abcd")
795 del abcd[1:2]
796
797 compile("del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()", "<testcase>", "exec")
798
799 def test_pass_stmt(self):
800 # 'pass'
801 pass
802
803 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
804 # Tested below
805
806 def test_break_stmt(self):
807 # 'break'
808 while 1: break
809
810 def test_continue_stmt(self):
811 # 'continue'
812 i = 1
813 while i: i = 0; continue
814
815 msg = ""
816 while not msg:
817 msg = "ok"
818 try:
819 continue
820 msg = "continue failed to continue inside try"
821 except:
822 msg = "continue inside try called except block"
823 if msg != "ok":
824 self.fail(msg)
825
826 msg = ""
827 while not msg:
828 msg = "finally block not called"
829 try:
830 continue
831 finally:
832 msg = "ok"
833 if msg != "ok":
834 self.fail(msg)
835
836 def test_break_continue_loop(self):
837 # This test warrants an explanation. It is a test specifically for SF bugs
838 # #463359 and #462937. The bug is that a 'break' statement executed or
839 # exception raised inside a try/except inside a loop, *after* a continue
840 # statement has been executed in that loop, will cause the wrong number of
841 # arguments to be popped off the stack and the instruction pointer reset to
842 # a very small number (usually 0.) Because of this, the following test
843 # *must* written as a function, and the tracking vars *must* be function
844 # arguments with default values. Otherwise, the test will loop and loop.
845
846 def test_inner(extra_burning_oil = 1, count=0):
847 big_hippo = 2
848 while big_hippo:
849 count += 1
850 try:
851 if extra_burning_oil and big_hippo == 1:
852 extra_burning_oil -= 1
853 break
854 big_hippo -= 1
855 continue
856 except:
857 raise
858 if count > 2 or big_hippo != 1:
859 self.fail("continue then break in try/except in loop broken!")
860 test_inner()
861
862 def test_return(self):
863 # 'return' [testlist_star_expr]
864 def g1(): return
865 def g2(): return 1
866 def g3():
867 z = [2, 3]
868 return 1, *z
869
870 g1()
871 x = g2()
872 y = g3()
873 self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
874 check_syntax_error(self, "class foo:return 1")
875
876 def test_control_flow_in_finally(self):
877
878 def run_case(self, src, expected):
879 with warnings.catch_warnings():
880 warnings.simplefilter('ignore', SyntaxWarning)
881 g, l = {}, { 'self': self }
882 exec(textwrap.dedent(src), g, l)
883 self.assertEqual(expected, l['result'])
884
885
886 # *********** Break in finally ***********
887
888 run_case(
889 self,
890 """
891 result = 0
892 while result < 2:
893 result += 1
894 try:
895 pass
896 finally:
897 break
898 """,
899 1)
900
901 run_case(
902 self,
903 """
904 result = 0
905 while result < 2:
906 result += 1
907 try:
908 continue
909 finally:
910 break
911 """,
912 1)
913
914 run_case(
915 self,
916 """
917 result = 0
918 while result < 2:
919 result += 1
920 try:
921 1/0
922 finally:
923 break
924 """,
925 1)
926
927 run_case(
928 self,
929 """
930 for result in [0, 1]:
931 self.assertEqual(result, 0)
932 try:
933 pass
934 finally:
935 break
936 """,
937 0)
938
939 run_case(
940 self,
941 """
942 for result in [0, 1]:
943 self.assertEqual(result, 0)
944 try:
945 continue
946 finally:
947 break
948 """,
949 0)
950
951 run_case(
952 self,
953 """
954 for result in [0, 1]:
955 self.assertEqual(result, 0)
956 try:
957 1/0
958 finally:
959 break
960 """,
961 0)
962
963
964 # *********** Continue in finally ***********
965
966 run_case(
967 self,
968 """
969 result = 0
970 while result < 2:
971 result += 1
972 try:
973 pass
974 finally:
975 continue
976 break
977 """,
978 2)
979
980
981 run_case(
982 self,
983 """
984 result = 0
985 while result < 2:
986 result += 1
987 try:
988 break
989 finally:
990 continue
991 """,
992 2)
993
994 run_case(
995 self,
996 """
997 result = 0
998 while result < 2:
999 result += 1
1000 try:
1001 1/0
1002 finally:
1003 continue
1004 break
1005 """,
1006 2)
1007
1008 run_case(
1009 self,
1010 """
1011 for result in [0, 1]:
1012 try:
1013 pass
1014 finally:
1015 continue
1016 break
1017 """,
1018 1)
1019
1020 run_case(
1021 self,
1022 """
1023 for result in [0, 1]:
1024 try:
1025 break
1026 finally:
1027 continue
1028 """,
1029 1)
1030
1031 run_case(
1032 self,
1033 """
1034 for result in [0, 1]:
1035 try:
1036 1/0
1037 finally:
1038 continue
1039 break
1040 """,
1041 1)
1042
1043
1044 # *********** Return in finally ***********
1045
1046 run_case(
1047 self,
1048 """
1049 def f():
1050 try:
1051 pass
1052 finally:
1053 return 1
1054 result = f()
1055 """,
1056 1)
1057
1058 run_case(
1059 self,
1060 """
1061 def f():
1062 try:
1063 return 2
1064 finally:
1065 return 3
1066 result = f()
1067 """,
1068 3)
1069
1070 run_case(
1071 self,
1072 """
1073 def f():
1074 try:
1075 1/0
1076 finally:
1077 return 4
1078 result = f()
1079 """,
1080 4)
1081
1082 # See issue #37830
1083 run_case(
1084 self,
1085 """
1086 def break_in_finally_after_return1(x):
1087 for count in [0, 1]:
1088 count2 = 0
1089 while count2 < 20:
1090 count2 += 10
1091 try:
1092 return count + count2
1093 finally:
1094 if x:
1095 break
1096 return 'end', count, count2
1097
1098 self.assertEqual(break_in_finally_after_return1(False), 10)
1099 self.assertEqual(break_in_finally_after_return1(True), ('end', 1, 10))
1100 result = True
1101 """,
1102 True)
1103
1104
1105 run_case(
1106 self,
1107 """
1108 def break_in_finally_after_return2(x):
1109 for count in [0, 1]:
1110 for count2 in [10, 20]:
1111 try:
1112 return count + count2
1113 finally:
1114 if x:
1115 break
1116 return 'end', count, count2
1117
1118 self.assertEqual(break_in_finally_after_return2(False), 10)
1119 self.assertEqual(break_in_finally_after_return2(True), ('end', 1, 10))
1120 result = True
1121 """,
1122 True)
1123
1124 # See issue #37830
1125 run_case(
1126 self,
1127 """
1128 def continue_in_finally_after_return1(x):
1129 count = 0
1130 while count < 100:
1131 count += 1
1132 try:
1133 return count
1134 finally:
1135 if x:
1136 continue
1137 return 'end', count
1138
1139 self.assertEqual(continue_in_finally_after_return1(False), 1)
1140 self.assertEqual(continue_in_finally_after_return1(True), ('end', 100))
1141 result = True
1142 """,
1143 True)
1144
1145 run_case(
1146 self,
1147 """
1148 def continue_in_finally_after_return2(x):
1149 for count in [0, 1]:
1150 try:
1151 return count
1152 finally:
1153 if x:
1154 continue
1155 return 'end', count
1156
1157 self.assertEqual(continue_in_finally_after_return2(False), 0)
1158 self.assertEqual(continue_in_finally_after_return2(True), ('end', 1))
1159 result = True
1160 """,
1161 True)
1162
1163 def test_yield(self):
1164 # Allowed as standalone statement
1165 def g(): yield 1
1166 def g(): yield from ()
1167 # Allowed as RHS of assignment
1168 def g(): x = yield 1
1169 def g(): x = yield from ()
1170 # Ordinary yield accepts implicit tuples
1171 def g(): yield 1, 1
1172 def g(): x = yield 1, 1
1173 # 'yield from' does not
1174 check_syntax_error(self, "def g(): yield from (), 1")
1175 check_syntax_error(self, "def g(): x = yield from (), 1")
1176 # Requires parentheses as subexpression
1177 def g(): 1, (yield 1)
1178 def g(): 1, (yield from ())
1179 check_syntax_error(self, "def g(): 1, yield 1")
1180 check_syntax_error(self, "def g(): 1, yield from ()")
1181 # Requires parentheses as call argument
1182 def g(): f((yield 1))
1183 def g(): f((yield 1), 1)
1184 def g(): f((yield from ()))
1185 def g(): f((yield from ()), 1)
1186 # Do not require parenthesis for tuple unpacking
1187 def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
1188 self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)])
1189 check_syntax_error(self, "def g(): f(yield 1)")
1190 check_syntax_error(self, "def g(): f(yield 1, 1)")
1191 check_syntax_error(self, "def g(): f(yield from ())")
1192 check_syntax_error(self, "def g(): f(yield from (), 1)")
1193 # Not allowed at top level
1194 check_syntax_error(self, "yield")
1195 check_syntax_error(self, "yield from")
1196 # Not allowed at class scope
1197 check_syntax_error(self, "class foo:yield 1")
1198 check_syntax_error(self, "class foo:yield from ()")
1199 # Check annotation refleak on SyntaxError
1200 check_syntax_error(self, "def g(a:(yield)): pass")
1201
1202 def test_yield_in_comprehensions(self):
1203 # Check yield in comprehensions
1204 def g(): [x for x in [(yield 1)]]
1205 def g(): [x for x in [(yield from ())]]
1206
1207 check = self.check_syntax_error
1208 check("def g(): [(yield x) for x in ()]",
1209 "'yield' inside list comprehension")
1210 check("def g(): [x for x in () if not (yield x)]",
1211 "'yield' inside list comprehension")
1212 check("def g(): [y for x in () for y in [(yield x)]]",
1213 "'yield' inside list comprehension")
1214 check("def g(): {(yield x) for x in ()}",
1215 "'yield' inside set comprehension")
1216 check("def g(): {(yield x): x for x in ()}",
1217 "'yield' inside dict comprehension")
1218 check("def g(): {x: (yield x) for x in ()}",
1219 "'yield' inside dict comprehension")
1220 check("def g(): ((yield x) for x in ())",
1221 "'yield' inside generator expression")
1222 check("def g(): [(yield from x) for x in ()]",
1223 "'yield' inside list comprehension")
1224 check("class C: [(yield x) for x in ()]",
1225 "'yield' inside list comprehension")
1226 check("[(yield x) for x in ()]",
1227 "'yield' inside list comprehension")
1228
1229 def test_raise(self):
1230 # 'raise' test [',' test]
1231 try: raise RuntimeError('just testing')
1232 except RuntimeError: pass
1233 try: raise KeyboardInterrupt
1234 except KeyboardInterrupt: pass
1235
1236 def test_import(self):
1237 # 'import' dotted_as_names
1238 import sys
1239 import time, sys
1240 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
1241 from time import time
1242 from time import (time)
1243 # not testable inside a function, but already done at top of the module
1244 # from sys import *
1245 from sys import path, argv
1246 from sys import (path, argv)
1247 from sys import (path, argv,)
1248
1249 def test_global(self):
1250 # 'global' NAME (',' NAME)*
1251 global a
1252 global a, b
1253 global one, two, three, four, five, six, seven, eight, nine, ten
1254
1255 def test_nonlocal(self):
1256 # 'nonlocal' NAME (',' NAME)*
1257 x = 0
1258 y = 0
1259 def f():
1260 nonlocal x
1261 nonlocal x, y
1262
1263 def test_assert(self):
1264 # assertTruestmt: 'assert' test [',' test]
1265 assert 1
1266 assert 1, 1
1267 assert lambda x:x
1268 assert 1, lambda x:x+1
1269
1270 try:
1271 assert True
1272 except AssertionError as e:
1273 self.fail("'assert True' should not have raised an AssertionError")
1274
1275 try:
1276 assert True, 'this should always pass'
1277 except AssertionError as e:
1278 self.fail("'assert True, msg' should not have "
1279 "raised an AssertionError")
1280
1281 # these tests fail if python is run with -O, so check __debug__
1282 @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
1283 def test_assert_failures(self):
1284 try:
1285 assert 0, "msg"
1286 except AssertionError as e:
1287 self.assertEqual(e.args[0], "msg")
1288 else:
1289 self.fail("AssertionError not raised by assert 0")
1290
1291 try:
1292 assert False
1293 except AssertionError as e:
1294 self.assertEqual(len(e.args), 0)
1295 else:
1296 self.fail("AssertionError not raised by 'assert False'")
1297
1298 def test_assert_syntax_warnings(self):
1299 # Ensure that we warn users if they provide a non-zero length tuple as
1300 # the assertion test.
1301 self.check_syntax_warning('assert(x, "msg")',
1302 'assertion is always true')
1303 self.check_syntax_warning('assert(False, "msg")',
1304 'assertion is always true')
1305 self.check_syntax_warning('assert(False,)',
1306 'assertion is always true')
1307
1308 with self.check_no_warnings(category=SyntaxWarning):
1309 compile('assert x, "msg"', '<testcase>', 'exec')
1310 compile('assert False, "msg"', '<testcase>', 'exec')
1311
1312 def test_assert_warning_promotes_to_syntax_error(self):
1313 # If SyntaxWarning is configured to be an error, it actually raises a
1314 # SyntaxError.
1315 # https://bugs.python.org/issue35029
1316 with warnings.catch_warnings():
1317 warnings.simplefilter('error', SyntaxWarning)
1318 try:
1319 compile('assert x, "msg" ', '<testcase>', 'exec')
1320 except SyntaxError:
1321 self.fail('SyntaxError incorrectly raised for \'assert x, "msg"\'')
1322 with self.assertRaises(SyntaxError):
1323 compile('assert(x, "msg")', '<testcase>', 'exec')
1324 with self.assertRaises(SyntaxError):
1325 compile('assert(False, "msg")', '<testcase>', 'exec')
1326 with self.assertRaises(SyntaxError):
1327 compile('assert(False,)', '<testcase>', 'exec')
1328
1329
1330 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1331 # Tested below
1332
1333 def test_if(self):
1334 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1335 if 1: pass
1336 if 1: pass
1337 else: pass
1338 if 0: pass
1339 elif 0: pass
1340 if 0: pass
1341 elif 0: pass
1342 elif 0: pass
1343 elif 0: pass
1344 else: pass
1345
1346 def test_while(self):
1347 # 'while' test ':' suite ['else' ':' suite]
1348 while 0: pass
1349 while 0: pass
1350 else: pass
1351
1352 # Issue1920: "while 0" is optimized away,
1353 # ensure that the "else" clause is still present.
1354 x = 0
1355 while 0:
1356 x = 1
1357 else:
1358 x = 2
1359 self.assertEqual(x, 2)
1360
1361 def test_for(self):
1362 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
1363 for i in 1, 2, 3: pass
1364 for i, j, k in (): pass
1365 else: pass
1366 class Squares:
1367 def __init__(self, max):
1368 self.max = max
1369 self.sofar = []
1370 def __len__(self): return len(self.sofar)
1371 def __getitem__(self, i):
1372 if not 0 <= i < self.max: raise IndexError
1373 n = len(self.sofar)
1374 while n <= i:
1375 self.sofar.append(n*n)
1376 n = n+1
1377 return self.sofar[i]
1378 n = 0
1379 for x in Squares(10): n = n+x
1380 if n != 285:
1381 self.fail('for over growing sequence')
1382
1383 result = []
1384 for x, in [(1,), (2,), (3,)]:
1385 result.append(x)
1386 self.assertEqual(result, [1, 2, 3])
1387
1388 result = []
1389 a = b = c = [1, 2, 3]
1390 for x in *a, *b, *c:
1391 result.append(x)
1392 self.assertEqual(result, 3 * a)
1393
1394 def test_try(self):
1395 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1396 ### | 'try' ':' suite 'finally' ':' suite
1397 ### except_clause: 'except' [expr ['as' NAME]]
1398 try:
1399 1/0
1400 except ZeroDivisionError:
1401 pass
1402 else:
1403 pass
1404 try: 1/0
1405 except EOFError: pass
1406 except TypeError as msg: pass
1407 except: pass
1408 else: pass
1409 try: 1/0
1410 except (EOFError, TypeError, ZeroDivisionError): pass
1411 try: 1/0
1412 except EOFError, TypeError, ZeroDivisionError: pass
1413 try: 1/0
1414 except (EOFError, TypeError, ZeroDivisionError) as msg: pass
1415 try: pass
1416 finally: pass
1417 with self.assertRaises(SyntaxError):
1418 compile("try:\n pass\nexcept Exception as a.b:\n pass", "?", "exec")
1419 compile("try:\n pass\nexcept Exception as a[b]:\n pass", "?", "exec")
1420
1421 def test_try_star(self):
1422 ### try_stmt: 'try': suite (except_star_clause : suite) + ['else' ':' suite]
1423 ### except_star_clause: 'except*' expr ['as' NAME]
1424 try:
1425 1/0
1426 except* ZeroDivisionError:
1427 pass
1428 else:
1429 pass
1430 try: 1/0
1431 except* EOFError: pass
1432 except* ZeroDivisionError as msg: pass
1433 else: pass
1434 try: 1/0
1435 except* (EOFError, TypeError, ZeroDivisionError): pass
1436 try: 1/0
1437 except* EOFError, TypeError, ZeroDivisionError: pass
1438 try: 1/0
1439 except* (EOFError, TypeError, ZeroDivisionError) as msg: pass
1440 try: pass
1441 finally: pass
1442 with self.assertRaises(SyntaxError):
1443 compile("try:\n pass\nexcept* Exception as a.b:\n pass", "?", "exec")
1444 compile("try:\n pass\nexcept* Exception as a[b]:\n pass", "?", "exec")
1445 compile("try:\n pass\nexcept*:\n pass", "?", "exec")
1446
1447 def test_suite(self):
1448 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
1449 if 1: pass
1450 if 1:
1451 pass
1452 if 1:
1453 #
1454 #
1455 #
1456 pass
1457 pass
1458 #
1459 pass
1460 #
1461
1462 def test_test(self):
1463 ### and_test ('or' and_test)*
1464 ### and_test: not_test ('and' not_test)*
1465 ### not_test: 'not' not_test | comparison
1466 if not 1: pass
1467 if 1 and 1: pass
1468 if 1 or 1: pass
1469 if not not not 1: pass
1470 if not 1 and 1 and 1: pass
1471 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
1472
1473 def test_comparison(self):
1474 ### comparison: expr (comp_op expr)*
1475 ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1476 if 1: pass
1477 x = (1 == 1)
1478 if 1 == 1: pass
1479 if 1 != 1: pass
1480 if 1 < 1: pass
1481 if 1 > 1: pass
1482 if 1 <= 1: pass
1483 if 1 >= 1: pass
1484 if x is x: pass
1485 if x is not x: pass
1486 if 1 in (): pass
1487 if 1 not in (): pass
1488 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass
1489
1490 def test_comparison_is_literal(self):
1491 def check(test, msg):
1492 self.check_syntax_warning(test, msg)
1493
1494 check('x is 1', '"is" with \'int\' literal')
1495 check('x is "thing"', '"is" with \'str\' literal')
1496 check('1 is x', '"is" with \'int\' literal')
1497 check('x is y is 1', '"is" with \'int\' literal')
1498 check('x is not 1', '"is not" with \'int\' literal')
1499 check('x is not (1, 2)', '"is not" with \'tuple\' literal')
1500 check('(1, 2) is not x', '"is not" with \'tuple\' literal')
1501
1502 check('None is 1', '"is" with \'int\' literal')
1503 check('1 is None', '"is" with \'int\' literal')
1504
1505 check('x == 3 is y', '"is" with \'int\' literal')
1506 check('x == "thing" is y', '"is" with \'str\' literal')
1507
1508 with warnings.catch_warnings():
1509 warnings.simplefilter('error', SyntaxWarning)
1510 compile('x is None', '<testcase>', 'exec')
1511 compile('x is False', '<testcase>', 'exec')
1512 compile('x is True', '<testcase>', 'exec')
1513 compile('x is ...', '<testcase>', 'exec')
1514 compile('None is x', '<testcase>', 'exec')
1515 compile('False is x', '<testcase>', 'exec')
1516 compile('True is x', '<testcase>', 'exec')
1517 compile('... is x', '<testcase>', 'exec')
1518
1519 def test_warn_missed_comma(self):
1520 def check(test):
1521 self.check_syntax_warning(test, msg)
1522
1523 msg=r'is not callable; perhaps you missed a comma\?'
1524 check('[(1, 2) (3, 4)]')
1525 check('[(x, y) (3, 4)]')
1526 check('[[1, 2] (3, 4)]')
1527 check('[{1, 2} (3, 4)]')
1528 check('[{1: 2} (3, 4)]')
1529 check('[[i for i in range(5)] (3, 4)]')
1530 check('[{i for i in range(5)} (3, 4)]')
1531 check('[(i for i in range(5)) (3, 4)]')
1532 check('[{i: i for i in range(5)} (3, 4)]')
1533 check('[f"{x}" (3, 4)]')
1534 check('[f"x={x}" (3, 4)]')
1535 check('["abc" (3, 4)]')
1536 check('[b"abc" (3, 4)]')
1537 check('[123 (3, 4)]')
1538 check('[12.3 (3, 4)]')
1539 check('[12.3j (3, 4)]')
1540 check('[None (3, 4)]')
1541 check('[True (3, 4)]')
1542 check('[... (3, 4)]')
1543 check('[t"{x}" (3, 4)]')
1544 check('[t"x={x}" (3, 4)]')
1545
1546 msg=r'is not subscriptable; perhaps you missed a comma\?'
1547 check('[{1, 2} [i, j]]')
1548 check('[{i for i in range(5)} [i, j]]')
1549 check('[(i for i in range(5)) [i, j]]')
1550 check('[(lambda x, y: x) [i, j]]')
1551 check('[123 [i, j]]')
1552 check('[12.3 [i, j]]')
1553 check('[12.3j [i, j]]')
1554 check('[None [i, j]]')
1555 check('[True [i, j]]')
1556 check('[... [i, j]]')
1557 check('[t"{x}" [i, j]]')
1558 check('[t"x={x}" [i, j]]')
1559
1560 msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?'
1561 check('[(1, 2) [i, j]]')
1562 check('[(x, y) [i, j]]')
1563 check('[[1, 2] [i, j]]')
1564 check('[[i for i in range(5)] [i, j]]')
1565 check('[f"{x}" [i, j]]')
1566 check('[f"x={x}" [i, j]]')
1567 check('["abc" [i, j]]')
1568 check('[b"abc" [i, j]]')
1569
1570 msg=r'indices must be integers or slices, not tuple;'
1571 check('[[1, 2] [3, 4]]')
1572 msg=r'indices must be integers or slices, not list;'
1573 check('[[1, 2] [[3, 4]]]')
1574 check('[[1, 2] [[i for i in range(5)]]]')
1575 msg=r'indices must be integers or slices, not set;'
1576 check('[[1, 2] [{3, 4}]]')
1577 check('[[1, 2] [{i for i in range(5)}]]')
1578 msg=r'indices must be integers or slices, not dict;'
1579 check('[[1, 2] [{3: 4}]]')
1580 check('[[1, 2] [{i: i for i in range(5)}]]')
1581 msg=r'indices must be integers or slices, not generator;'
1582 check('[[1, 2] [(i for i in range(5))]]')
1583 msg=r'indices must be integers or slices, not function;'
1584 check('[[1, 2] [(lambda x, y: x)]]')
1585 msg=r'indices must be integers or slices, not str;'
1586 check('[[1, 2] [f"{x}"]]')
1587 check('[[1, 2] [f"x={x}"]]')
1588 check('[[1, 2] ["abc"]]')
1589 msg=r'indices must be integers or slices, not string.templatelib.Template;'
1590 check('[[1, 2] [t"{x}"]]')
1591 check('[[1, 2] [t"x={x}"]]')
1592 msg=r'indices must be integers or slices, not'
1593 check('[[1, 2] [b"abc"]]')
1594 check('[[1, 2] [12.3]]')
1595 check('[[1, 2] [12.3j]]')
1596 check('[[1, 2] [None]]')
1597 check('[[1, 2] [...]]')
1598
1599 with warnings.catch_warnings():
1600 warnings.simplefilter('error', SyntaxWarning)
1601 compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')
1602 compile('[[1, 2] [i]]', '<testcase>', 'exec')
1603 compile('[[1, 2] [0]]', '<testcase>', 'exec')
1604 compile('[[1, 2] [True]]', '<testcase>', 'exec')
1605 compile('[[1, 2] [1:2]]', '<testcase>', 'exec')
1606 compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec')
1607
1608 def test_binary_mask_ops(self):
1609 x = 1 & 1
1610 x = 1 ^ 1
1611 x = 1 | 1
1612
1613 def test_shift_ops(self):
1614 x = 1 << 1
1615 x = 1 >> 1
1616 x = 1 << 1 >> 1
1617
1618 def test_additive_ops(self):
1619 x = 1
1620 x = 1 + 1
1621 x = 1 - 1 - 1
1622 x = 1 - 1 + 1 - 1 + 1
1623
1624 def test_multiplicative_ops(self):
1625 x = 1 * 1
1626 x = 1 / 1
1627 x = 1 % 1
1628 x = 1 / 1 * 1 % 1
1629
1630 def test_unary_ops(self):
1631 x = +1
1632 x = -1
1633 x = ~1
1634 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
1635 x = -1*1/1 + 1*1 - ---1*1
1636
1637 def test_selectors(self):
1638 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
1639 ### subscript: expr | [expr] ':' [expr]
1640
1641 import sys, time
1642 c = sys.path[0]
1643 x = time.time()
1644 x = sys.modules['time'].time()
1645 a = '01234'
1646 c = a[0]
1647 c = a[-1]
1648 s = a[0:5]
1649 s = a[:5]
1650 s = a[0:]
1651 s = a[:]
1652 s = a[-5:]
1653 s = a[:-1]
1654 s = a[-4:-3]
1655 # A rough test of SF bug 1333982. https://bugs.python.org/issue1333982
1656 # The testing here is fairly incomplete.
1657 # Test cases should include: commas with 1 and 2 colons
1658 d = {}
1659 d[1] = 1
1660 d[1,] = 2
1661 d[1,2] = 3
1662 d[1,2,3] = 4
1663 L = list(d)
1664 L.sort(key=lambda x: (type(x).__name__, x))
1665 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
1666
1667 def test_atoms(self):
1668 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
1669 ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
1670
1671 x = (1)
1672 x = (1 or 2 or 3)
1673 x = (1 or 2 or 3, 2, 3)
1674
1675 x = []
1676 x = [1]
1677 x = [1 or 2 or 3]
1678 x = [1 or 2 or 3, 2, 3]
1679 x = []
1680
1681 x = {}
1682 x = {'one': 1}
1683 x = {'one': 1,}
1684 x = {'one' or 'two': 1 or 2}
1685 x = {'one': 1, 'two': 2}
1686 x = {'one': 1, 'two': 2,}
1687 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
1688
1689 x = {'one'}
1690 x = {'one', 1,}
1691 x = {'one', 'two', 'three'}
1692 x = {2, 3, 4,}
1693
1694 x = x
1695 x = 'x'
1696 x = 123
1697
1698 ### exprlist: expr (',' expr)* [',']
1699 ### testlist: test (',' test)* [',']
1700 # These have been exercised enough above
1701
1702 def test_classdef(self):
1703 # 'class' NAME ['(' [testlist] ')'] ':' suite
1704 class B: pass
1705 class B2(): pass
1706 class C1(B): pass
1707 class C2(B): pass
1708 class D(C1, C2, B): pass
1709 class C:
1710 def meth1(self): pass
1711 def meth2(self, arg): pass
1712 def meth3(self, a1, a2): pass
1713
1714 # decorator: '@' namedexpr_test NEWLINE
1715 # decorators: decorator+
1716 # decorated: decorators (classdef | funcdef)
1717 def class_decorator(x): return x
1718 @class_decorator
1719 class G: pass
1720
1721 # Test expressions as decorators (PEP 614):
1722 @False or class_decorator
1723 class H: pass
1724 @d := class_decorator
1725 class I: pass
1726 @lambda c: class_decorator(c)
1727 class J: pass
1728 @[..., class_decorator, ...][1]
1729 class K: pass
1730 @class_decorator(class_decorator)(class_decorator)
1731 class L: pass
1732 @[class_decorator][0].__call__.__call__
1733 class M: pass
1734
1735 def test_dictcomps(self):
1736 # dictorsetmaker: ( (test ':' test (comp_for |
1737 # (',' test ':' test)* [','])) |
1738 # (test (comp_for | (',' test)* [','])) )
1739 nums = [1, 2, 3]
1740 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
1741
1742 def test_listcomps(self):
1743 # list comprehension tests
1744 nums = [1, 2, 3, 4, 5]
1745 strs = ["Apple", "Banana", "Coconut"]
1746 spcs = [" Apple", " Banana ", "Coco nut "]
1747
1748 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
1749 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
1750 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
1751 self.assertEqual([(i, s) for i in nums for s in strs],
1752 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
1753 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
1754 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
1755 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
1756 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
1757 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
1758 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
1759 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
1760 (5, 'Banana'), (5, 'Coconut')])
1761 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
1762 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
1763
1764 def test_in_func(l):
1765 return [0 < x < 3 for x in l if x > 2]
1766
1767 self.assertEqual(test_in_func(nums), [False, False, False])
1768
1769 def test_nested_front():
1770 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
1771 [[1, 2], [3, 4], [5, 6]])
1772
1773 test_nested_front()
1774
1775 check_syntax_error(self, "[i, s for i in nums for s in strs]")
1776 check_syntax_error(self, "[x if y]")
1777
1778 suppliers = [
1779 (1, "Boeing"),
1780 (2, "Ford"),
1781 (3, "Macdonalds")
1782 ]
1783
1784 parts = [
1785 (10, "Airliner"),
1786 (20, "Engine"),
1787 (30, "Cheeseburger")
1788 ]
1789
1790 suppart = [
1791 (1, 10), (1, 20), (2, 20), (3, 30)
1792 ]
1793
1794 x = [
1795 (sname, pname)
1796 for (sno, sname) in suppliers
1797 for (pno, pname) in parts
1798 for (sp_sno, sp_pno) in suppart
1799 if sno == sp_sno and pno == sp_pno
1800 ]
1801
1802 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
1803 ('Macdonalds', 'Cheeseburger')])
1804
1805 def test_genexps(self):
1806 # generator expression tests
1807 g = ([x for x in range(10)] for x in range(1))
1808 self.assertEqual(next(g), [x for x in range(10)])
1809 try:
1810 next(g)
1811 self.fail('should produce StopIteration exception')
1812 except StopIteration:
1813 pass
1814
1815 a = 1
1816 try:
1817 g = (a for d in a)
1818 next(g)
1819 self.fail('should produce TypeError')
1820 except TypeError:
1821 pass
1822
1823 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
1824 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
1825
1826 a = [x for x in range(10)]
1827 b = (x for x in (y for y in a))
1828 self.assertEqual(sum(b), sum([x for x in range(10)]))
1829
1830 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
1831 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
1832 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
1833 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
1834 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
1835 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)]))
1836 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
1837 check_syntax_error(self, "foo(x for x in range(10), 100)")
1838 check_syntax_error(self, "foo(100, x for x in range(10))")
1839
1840 def test_comprehension_specials(self):
1841 # test for outmost iterable precomputation
1842 x = 10; g = (i for i in range(x)); x = 5
1843 self.assertEqual(len(list(g)), 10)
1844
1845 # This should hold, since we're only precomputing outmost iterable.
1846 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
1847 x = 5; t = True;
1848 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
1849
1850 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
1851 # even though it's silly. Make sure it works (ifelse broke this.)
1852 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
1853 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
1854
1855 # verify unpacking single element tuples in listcomp/genexp.
1856 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
1857 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
1858
1859 def test_with_statement(self):
1860 class manager(object):
1861 def __enter__(self):
1862 return (1, 2)
1863 def __exit__(self, *args):
1864 pass
1865
1866 with manager():
1867 pass
1868 with manager() as x:
1869 pass
1870 with manager() as (x, y):
1871 pass
1872 with manager(), manager():
1873 pass
1874 with manager() as x, manager() as y:
1875 pass
1876 with manager() as x, manager():
1877 pass
1878
1879 with (
1880 manager()
1881 ):
1882 pass
1883
1884 with (
1885 manager() as x
1886 ):
1887 pass
1888
1889 with (
1890 manager() as (x, y),
1891 manager() as z,
1892 ):
1893 pass
1894
1895 with (
1896 manager(),
1897 manager()
1898 ):
1899 pass
1900
1901 with (
1902 manager() as x,
1903 manager() as y
1904 ):
1905 pass
1906
1907 with (
1908 manager() as x,
1909 manager()
1910 ):
1911 pass
1912
1913 with (
1914 manager() as x,
1915 manager() as y,
1916 manager() as z,
1917 ):
1918 pass
1919
1920 with (
1921 manager() as x,
1922 manager() as y,
1923 manager(),
1924 ):
1925 pass
1926
1927 def test_if_else_expr(self):
1928 # Test ifelse expressions in various cases
1929 def _checkeval(msg, ret):
1930 "helper to check that evaluation of expressions is done correctly"
1931 print(msg)
1932 return ret
1933
1934 # the next line is not allowed anymore
1935 #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
1936 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
1937 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])
1938 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
1939 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
1940 self.assertEqual((5 and 6 if 0 else 1), 1)
1941 self.assertEqual(((5 and 6) if 0 else 1), 1)
1942 self.assertEqual((5 and (6 if 1 else 1)), 6)
1943 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
1944 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
1945 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1946 self.assertEqual((not 5 if 1 else 1), False)
1947 self.assertEqual((not 5 if 0 else 1), 1)
1948 self.assertEqual((6 + 1 if 1 else 2), 7)
1949 self.assertEqual((6 - 1 if 1 else 2), 5)
1950 self.assertEqual((6 * 2 if 1 else 4), 12)
1951 self.assertEqual((6 / 2 if 1 else 3), 3)
1952 self.assertEqual((6 < 4 if 0 else 2), 2)
1953
1954 def test_paren_evaluation(self):
1955 self.assertEqual(16 // (4 // 2), 8)
1956 self.assertEqual((16 // 4) // 2, 2)
1957 self.assertEqual(16 // 4 // 2, 2)
1958 x = 2
1959 y = 3
1960 self.assertTrue(False is (x is y))
1961 self.assertFalse((False is x) is y)
1962 self.assertFalse(False is x is y)
1963
1964 def test_matrix_mul(self):
1965 # This is not intended to be a comprehensive test, rather just to be few
1966 # samples of the @ operator in test_grammar.py.
1967 class M:
1968 def __matmul__(self, o):
1969 return 4
1970 def __imatmul__(self, o):
1971 self.other = o
1972 return self
1973 m = M()
1974 self.assertEqual(m @ m, 4)
1975 m @= 42
1976 self.assertEqual(m.other, 42)
1977
1978 def test_async_await(self):
1979 async def test():
1980 def sum():
1981 pass
1982 if 1:
1983 await someobj()
1984
1985 self.assertEqual(test.__name__, 'test')
1986 self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
1987
1988 def decorator(func):
1989 setattr(func, '_marked', True)
1990 return func
1991
1992 @decorator
1993 async def test2():
1994 return 22
1995 self.assertTrue(test2._marked)
1996 self.assertEqual(test2.__name__, 'test2')
1997 self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
1998
1999 def test_async_for(self):
2000 class Done(Exception): pass
2001
2002 class AIter:
2003 def __aiter__(self):
2004 return self
2005 async def __anext__(self):
2006 raise StopAsyncIteration
2007
2008 async def foo():
2009 async for i in AIter():
2010 pass
2011 async for i, j in AIter():
2012 pass
2013 async for i in AIter():
2014 pass
2015 else:
2016 pass
2017 raise Done
2018
2019 with self.assertRaises(Done):
2020 foo().send(None)
2021
2022 def test_async_with(self):
2023 class Done(Exception): pass
2024
2025 class manager:
2026 async def __aenter__(self):
2027 return (1, 2)
2028 async def __aexit__(self, *exc):
2029 return False
2030
2031 async def foo():
2032 async with manager():
2033 pass
2034 async with manager() as x:
2035 pass
2036 async with manager() as (x, y):
2037 pass
2038 async with manager(), manager():
2039 pass
2040 async with manager() as x, manager() as y:
2041 pass
2042 async with manager() as x, manager():
2043 pass
2044 raise Done
2045
2046 with self.assertRaises(Done):
2047 foo().send(None)
2048
2049 def test_complex_lambda(self):
2050 def test1(foo, bar):
2051 return ""
2052
2053 def test2():
2054 return f"{test1(
2055 foo=lambda: '、、、、、、、、、、、、、、、、、',
2056 bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
2057 )}"
2058
2059 self.assertEqual(test2(), "")
2060
2061
2062 if __name__ == '__main__':
2063 unittest.main()