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