1 from test
.test_support
import verify
, TestFailed
, check_syntax
4 warnings
.filterwarnings("ignore", r
"import \*", SyntaxWarning, "<string>")
6 print "1. simple nesting"
14 plus10
= make_adder(10)
17 verify(plus10(-2) == 8)
19 print "2. extra nesting"
22 def extra(): # check freevars passing through non-use scopes
29 plus10
= make_adder2(10)
32 verify(plus10(-2) == 8)
34 print "3. simple nesting + rebinding"
39 x
= x
+ 1 # check tracking of assignment to x in defining scope
43 plus10
= make_adder3(9)
46 verify(plus10(-2) == 8)
48 print "4. nesting with global but no free"
50 def make_adder4(): # XXX add exta level of indirection
54 return global_x
+ y
# check that plain old globals work
64 verify(adder(-2) == 8)
66 print "5. nesting through class"
70 def __call__(self
, y
):
75 plus10
= make_adder5(10)
78 verify(plus10(-2) == 8)
80 print "6. nesting plus free ref to global"
85 return global_nest_x
+ y
90 plus10
= make_adder6(10)
92 verify(inc(1) == 11) # there's only one global
93 verify(plus10(-2) == 8)
95 print "7. nearest enclosing scope"
99 x
= 42 # check that this masks binding in f()
106 verify(test_func(5) == 47)
108 print "8. mixed freevars and cellvars"
119 return identity(z
* (b
+ y
))
128 print "9. free variable in method"
131 method_and_var
= "var"
133 def method_and_var(self
):
136 return method_and_var
137 def actual_global(self
):
144 verify(t
.test() == "var")
145 verify(t
.method_and_var() == "method")
146 verify(t
.actual_global() == "global")
148 method_and_var
= "var"
150 # this class is not nested, so the rules are different
151 def method_and_var(self
):
154 return method_and_var
155 def actual_global(self
):
161 verify(t
.test() == "var")
162 verify(t
.method_and_var() == "method")
163 verify(t
.actual_global() == "global")
165 print "10. recursion"
172 return n
* fact(n
- 1)
176 raise ValueError, "x must be >= 0"
181 print "11. unoptimized namespaces"
184 def unoptimized_clash1(strip):
187 return strip(s) # ambiguity: free or local
192 def unoptimized_clash2():
195 return strip(s) # ambiguity: global or local
200 def unoptimized_clash2():
204 return strip(s) # ambiguity: global or local
208 # XXX could allow this for exec with const argument, but what's the point
221 del x # can't del name
228 return strip # global or local?
231 # and verify a few cases that should work
252 f1
= lambda x
: lambda y
: x
+ y
256 verify(plus10(5) == 15)
258 f2
= lambda x
: (lambda : lambda y
: x
+ y
)()
262 verify(plus10(5) == 15)
264 f3
= lambda x
: lambda y
: global_x
+ y
269 f8
= lambda x
, y
, z
: lambda a
, b
, c
: lambda : z
* (b
+ y
)
274 print "13. UnboundLocal"
290 except UnboundLocalError:
302 print "14. complex definitions"
304 def makeReturner(*lst
):
309 verify(makeReturner(1,2,3)() == (1,2,3))
311 def makeReturner2(**kwargs
):
316 verify(makeReturner2(a
=11)()['a'] == 11)
318 def makeAddPair((a
, b
)):
320 return (a
+ c
, b
+ d
)
323 verify(makeAddPair((1, 2))((100, 200)) == (101,202))
325 print "15. scope of global statements"
326 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
390 print "16. check leaks"
410 verify(Foo
.count
== 0)
412 print "17. class and global"
417 def __call__(self
, y
):
422 verify(test(6)(2) == 8)
424 verify(test(3)(2) == 5)
426 print "18. verify that locals() works"
438 verify(d
.has_key('h'))
440 verify(d
== {'x': 2, 'y': 7, 'w': 6})
442 print "19. var is bound and free in class"
452 verify(inst
.a
== inst
.m())
454 print "20. interaction with trace function"
460 def adaptgetter(name
, klass
, getter
):
462 if kind
== 1: # AV happens when stepping from this line to next
464 des
= "_%s__%s" % (klass
.__name
__, name
)
465 return lambda obj
: getattr(obj
, des
)
471 adaptgetter("foo", TestClass
, (1, ""))
475 except TypeError: pass
476 else: raise TestFailed
, 'sys.settrace() did not raise TypeError'
478 print "20. eval and exec with free variables"
489 print "eval() should have failed, because code contained free vars"
496 print "exec should have failed, because code contained free vars"
498 print "21. list comprehension with local variables"
505 print "bad should not be defined"
508 [bad
for s
in 'a b' for bad
in s
.split()]
516 print "22. eval with free variables"