else: return n * fact(n - 1) if x >= 0: return fact(x) else: raise ValueError, "x must be >= 0" verify(f(6) == 720) print "11. unoptimized namespaces" check_syntax("""from __future__ import nested_scopes def unoptimized_clash1(strip): def f(s): from string import * return strip(s) # ambiguity: free or local return f """) check_syntax("""from __future__ import nested_scopes def unoptimized_clash2(): from string import * def f(s): return strip(s) # ambiguity: global or local return f """) check_syntax("""from __future__ import nested_scopes def unoptimized_clash2(): from string import *
def d32v((x,)): pass d32v((1,)) ### lambdef: 'lambda' [varargslist] ':' test print 'lambdef' l1 = lambda : 0 verify(l1() == 0) l2 = lambda : a[d] # XXX just testing the expression l3 = lambda : [2 < x for x in [-1, 3, 0L]] verify(l3() == [0, 1, 0]) l4 = lambda x = lambda y = lambda z=1 : z : y() : x() verify(l4() == 1) l5 = lambda x, y, z=2: x + y + z verify(l5(1, 2) == 5) verify(l5(1, 2, 3) == 6) check_syntax("lambda x: x = 2") ### stmt: simple_stmt | compound_stmt # Tested below ### simple_stmt: small_stmt (';' small_stmt)* [';'] print 'simple_stmt' x = 1; pass; del x def foo(): # verify statments that end with semi-colons x = 1; pass; del x; foo() ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt # Tested below
return n * fact(n - 1) if x >= 0: return fact(x) else: raise ValueError, "x must be >= 0" verify(f(6) == 720) print "11. unoptimized namespaces" check_syntax("""\ def unoptimized_clash1(strip): def f(s): from string import * return strip(s) # ambiguity: free or local return f """) check_syntax("""\ def unoptimized_clash2(): from string import * def f(s): return strip(s) # ambiguity: global or local return f """) check_syntax("""\ def unoptimized_clash2(): from string import *
d22v(1, 2, *(3, 4, 5)) d22v(1, *(2, 3), **{'d': 4}) ### lambdef: 'lambda' [varargslist] ':' test print 'lambdef' l1 = lambda : 0 verify(l1() == 0) l2 = lambda : a[d] # XXX just testing the expression l3 = lambda : [2 < x for x in [-1, 3, 0L]] verify(l3() == [0, 1, 0]) l4 = lambda x = lambda y = lambda z=1 : z : y() : x() verify(l4() == 1) l5 = lambda x, y, z=2: x + y + z verify(l5(1, 2) == 5) verify(l5(1, 2, 3) == 6) check_syntax("lambda x: x = 2") ### stmt: simple_stmt | compound_stmt # Tested below ### simple_stmt: small_stmt (';' small_stmt)* [';'] print 'simple_stmt' x = 1; pass; del x ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt # Tested below print 'expr_stmt' # (exprlist '=')* exprlist 1 1, 2, 3 x = 1