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