1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""This module tests SyntaxErrors. 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepHere's an example of the sort of thing that is tested. 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(x): 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... global x 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: name 'x' is local and global (<doctest test.test_syntax[0]>, line 1) 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe tests are all raise SyntaxErrors. They were created by checking 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepeach C call that raises SyntaxError. There are several modules that 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepraise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsymtable.c. 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe parser itself outlaws a lot of invalid syntax. None of these 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeperrors are tested here at the moment. We should add some tests; since 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthere are infinitely many programs with invalid syntax, we would need 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepto be judicious in selecting some. 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe compiler generates a synthetic module name for code executed by 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdoctest. Since all the code comes from the same module, a suffix like 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep[1] is appended to the module name, As a consequence, changing the 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeporder of tests in this module means renumbering all the errors after 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepit. (Maybe we should enable the ellipsis option for these tests.) 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIn ast.c, syntax errors are raised by calling ast_error(). 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepErrors from set_context(): 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> obj.None = 1 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[1]>", line 1 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> None = 1 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[2]>", line 1 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIt's a syntax error to assign to the empty tuple. Why isn't it an 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeperror to assign to the empty list? It will always raise some error at 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepruntime. 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> () = 1 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[3]>", line 1 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to () 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f() = 1 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[4]>", line 1 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to function call 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> del f() 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[5]>", line 1 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't delete function call 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> a + 1 = 2 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[6]>", line 1 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to operator 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> (x for x in x) = 1 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[7]>", line 1 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to generator expression 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> 1 = 1 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[8]>", line 1 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to literal 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> "abc" = 1 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[8]>", line 1 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to literal 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> `1` = 1 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[10]>", line 1 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to repr 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf the left-hand side of an assignment is a list or tuple, an illegal 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexpression inside that contain should still cause a syntax error. 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis test just checks a couple of cases rather than enumerating all of 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthem. 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> (a, "b", c) = (1, 2, 3) 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[11]>", line 1 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to literal 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> [a, b, c + 1] = [1, 2, 3] 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[12]>", line 1 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to operator 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> a if 1 else b = 1 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[13]>", line 1 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to conditional expression 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFrom compiler_complex_args(): 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(None=1): 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[14]>", line 1 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFrom ast_for_arguments(): 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(x, y=1, z): 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[15]>", line 1 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: non-default argument follows default argument 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(x, None): 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[16]>", line 1 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(*None): 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[17]>", line 1 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(**None): 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[18]>", line 1 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFrom ast_for_funcdef(): 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def None(x): 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... pass 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[19]>", line 1 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFrom ast_for_call(): 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(it, *varargs): 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... return list(it) 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> L = range(10) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(x for x in L) 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(x for x in L, 1) 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[23]>", line 1 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: Generator expression must be parenthesized if not sole argument 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f((x for x in L), 1) 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i100, i101, i102, i103, i104, i105, i106, i107, i108, 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i109, i110, i111, i112, i113, i114, i115, i116, i117, 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i118, i119, i120, i121, i122, i123, i124, i125, i126, 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i127, i128, i129, i130, i131, i132, i133, i134, i135, 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i136, i137, i138, i139, i140, i141, i142, i143, i144, 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i145, i146, i147, i148, i149, i150, i151, i152, i153, 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i154, i155, i156, i157, i158, i159, i160, i161, i162, 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i163, i164, i165, i166, i167, i168, i169, i170, i171, 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i172, i173, i174, i175, i176, i177, i178, i179, i180, 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i181, i182, i183, i184, i185, i186, i187, i188, i189, 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i190, i191, i192, i193, i194, i195, i196, i197, i198, 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i199, i200, i201, i202, i203, i204, i205, i206, i207, 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i208, i209, i210, i211, i212, i213, i214, i215, i216, 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i217, i218, i219, i220, i221, i222, i223, i224, i225, 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i226, i227, i228, i229, i230, i231, i232, i233, i234, 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i235, i236, i237, i238, i239, i240, i241, i242, i243, 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i244, i245, i246, i247, i248, i249, i250, i251, i252, 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i253, i254, i255) 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[25]>", line 1 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: more than 255 arguments 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe actual error cases counts positional arguments, keyword arguments, 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepand generator expression arguments separately. This test combines the 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthree. 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i100, i101, i102, i103, i104, i105, i106, i107, i108, 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i109, i110, i111, i112, i113, i114, i115, i116, i117, 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i118, i119, i120, i121, i122, i123, i124, i125, i126, 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i127, i128, i129, i130, i131, i132, i133, i134, i135, 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i136, i137, i138, i139, i140, i141, i142, i143, i144, 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i145, i146, i147, i148, i149, i150, i151, i152, i153, 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i154, i155, i156, i157, i158, i159, i160, i161, i162, 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i163, i164, i165, i166, i167, i168, i169, i170, i171, 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i172, i173, i174, i175, i176, i177, i178, i179, i180, 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i181, i182, i183, i184, i185, i186, i187, i188, i189, 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i190, i191, i192, i193, i194, i195, i196, i197, i198, 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i199, i200, i201, i202, i203, i204, i205, i206, i207, 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i208, i209, i210, i211, i212, i213, i214, i215, i216, 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i217, i218, i219, i220, i221, i222, i223, i224, i225, 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i226, i227, i228, i229, i230, i231, i232, i233, i234, 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i235, i236, i237, i238, i239, i240, i241, i242, i243, 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... i252=1, i253=1, i254=1, i255=1) 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[26]>", line 1 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: more than 255 arguments 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(lambda x: x[0] = 3) 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[27]>", line 1 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: lambda cannot contain assignment 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe grammar accepts any test (basically, any expression) in the 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepkeyword slot of a call site. Test a few different options. 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(x()=2) 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[28]>", line 1 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: keyword can't be an expression 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(a or b=1) 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[29]>", line 1 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: keyword can't be an expression 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(x.y=1) 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[30]>", line 1 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: keyword can't be an expression 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepMore set_context(): 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> (x for x in x) += 1 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[31]>", line 1 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to generator expression 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> None += 1 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[32]>", line 1 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: cannot assign to None 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f() += 1 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[33]>", line 1 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to function call 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTest continue in finally in weird combinations. 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcontinue in for loop under finally should be ok. 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def test(): 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for abc in range(10): 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... print abc 278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> test() 279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 9 280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepStart simple, a continue in a finally should not be allowed. 282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def test(): 284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for abc in range(10): 285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[36]>", line 6 292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis is essentially a continue in a finally which should not be allowed. 295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def test(): 297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for abc in range(10): 298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... except: 304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[37]>", line 6 308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def foo(): 311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[38]>", line 5 318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def foo(): 321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for a in (): 322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[39]>", line 6 329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def foo(): 332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for a in (): 333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[40]>", line 7 343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def foo(): 346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... for a in (): 347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: pass 348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... try: 350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... except: 352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... continue 353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[41]>", line 8 356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'continue' not supported inside 'finally' clause 357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThere is one test for a break that is not in a loop. The compiler 359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepuses a single data structure to keep track of try-finally and loops, 360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepso we need to be sure that a break is actually inside a loop. If it 361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepisn't, there should be a syntax error. 362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> try: 364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... print 1 365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... break 366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... print 2 367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... finally: 368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... print 3 369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[42]>", line 3 372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: 'break' outside loop 373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis should probably raise a better error than a SystemError (or none at all). 375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIn 2.5 there was a missing exception and an assert was triggered in a debug 376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbuild. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> while 1: 379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 2: 380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 3: 381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 4: 382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 5: 383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 6: 384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 8: 385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 9: 386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 10: 387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 11: 388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 12: 389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 13: 390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 14: 391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 15: 392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 16: 393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 17: 394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 18: 395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 19: 396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 20: 397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 21: 398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... while 22: 399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... break 400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SystemError: too many statically nested blocks 403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis tests assignment-context; there was a bug in Python 2.5 where compiling 405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa complex 'if' (one with 'elif') would fail to notice an invalid suite, 406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepleading to spurious errors. 407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> if 1: 409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... x() = 1 410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... elif 1: 411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[44]>", line 2 415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: can't assign to function call 416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> if 1: 418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... elif 1: 420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... x() = 1 421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[45]>", line 4 424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: can't assign to function call 425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> if 1: 427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... x() = 1 428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... elif 1: 429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... else: 431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[46]>", line 2 435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: can't assign to function call 436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> if 1: 438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... elif 1: 440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... x() = 1 441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... else: 442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[47]>", line 4 446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: can't assign to function call 447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> if 1: 449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... elif 1: 451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... pass 452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... else: 453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... x() = 1 454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[48]>", line 6 457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: can't assign to function call 458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> f(a=23, a=234) 460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[49]>", line 1 463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: keyword argument repeated 464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> del () 466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[50]>", line 1 469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't delete () 470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> {1, 2, 3} = 42 472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepTraceback (most recent call last): 473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... 474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep File "<doctest test.test_syntax[50]>", line 1 475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSyntaxError: can't assign to literal 476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepCorner-case that used to crash: 478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> def f(*xx, **__debug__): pass 480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Traceback (most recent call last): 481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxError: cannot assign to __debug__ 482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport re 486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest 487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport warnings 488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support 490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SyntaxTestCase(unittest.TestCase): 492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _check_error(self, code, errtext, 494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep filename="<testcase>", mode="exec", subclass=None): 495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Check that compiling code raises SyntaxError with errtext. 496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errtest is a regular expression that must be present in the 498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test of the exception raised. If subclass is specified it 499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep is the expected subclass of SyntaxError (e.g. IndentationError). 500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep compile(code, filename, mode) 503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except SyntaxError, err: 504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if subclass and not isinstance(err, subclass): 505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("SyntaxError is not a %s" % subclass.__name__) 506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep mo = re.search(errtext, str(err)) 507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mo is None: 508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("%s did not contain '%r'" % (err, errtext,)) 509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("compile() did not raise SyntaxError") 511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_paren_arg_with_default(self): 513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("def f((x)=23): pass", 514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "parenthesized arg with default") 515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_assign_call(self): 517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("f() = 1", "assign") 518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_assign_del(self): 520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("del f()", "delete") 521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_global_err_then_warn(self): 523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Bug tickler: The SyntaxError raised for one global statement 524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # shouldn't be clobbered by a SyntaxWarning issued for a later one. 525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep source = re.sub('(?m)^ *:', '', """\ 526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep :def error(a): 527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : global a # SyntaxError 528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep :def warning(): 529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : b = 1 530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : global b # SyntaxWarning 531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep :""") 532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep warnings.filterwarnings(action='ignore', category=SyntaxWarning) 533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error(source, "global") 534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep warnings.filters.pop(0) 535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_break_outside_loop(self): 537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("break", "outside loop") 538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_delete_deref(self): 540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep source = re.sub('(?m)^ *:', '', """\ 541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep :def foo(x): 542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : def bar(): 543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : print x 544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep : del x 545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep :""") 546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error(source, "nested scope") 547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_unexpected_indent(self): 549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("foo()\n bar()\n", "unexpected indent", 550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep subclass=IndentationError) 551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_no_indent(self): 553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("if 1:\nfoo()", "expected an indented block", 554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep subclass=IndentationError) 555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_bad_outdent(self): 557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("if 1:\n foo()\n bar()", 558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "unindent does not match .* level", 559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep subclass=IndentationError) 560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_kwargs_last(self): 562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._check_error("int(base=10, '2')", "non-keyword arg") 563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest(SyntaxTestCase) 566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep from test import test_syntax 567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep with test_support.check_py3k_warnings(("backquote not supported", 568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep SyntaxWarning)): 569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_doctest(test_syntax, verbosity=True) 570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 573