1 from test
.test_support
import verify
, TestFailed
, check_syntax
3 print "1. simple nesting"
11 plus10
= make_adder(10)
14 verify(plus10(-2) == 8)
16 print "2. extra nesting"
19 def extra(): # check freevars passing through non-use scopes
26 plus10
= make_adder2(10)
29 verify(plus10(-2) == 8)
31 print "3. simple nesting + rebinding"
36 x
= x
+ 1 # check tracking of assignment to x in defining scope
40 plus10
= make_adder3(9)
43 verify(plus10(-2) == 8)
45 print "4. nesting with global but no free"
47 def make_adder4(): # XXX add exta level of indirection
51 return global_x
+ y
# check that plain old globals work
61 verify(adder(-2) == 8)
63 print "5. nesting through class"
67 def __call__(self
, y
):
72 plus10
= make_adder5(10)
75 verify(plus10(-2) == 8)
77 print "6. nesting plus free ref to global"
82 return global_nest_x
+ y
87 plus10
= make_adder6(10)
89 verify(inc(1) == 11) # there's only one global
90 verify(plus10(-2) == 8)
92 print "7. nearest enclosing scope"
96 x
= 42 # check that this masks binding in f()
103 verify(test_func(5) == 47)
105 print "8. mixed freevars and cellvars"
116 return identity(z
* (b
+ y
))
125 print "9. free variable in method"
128 method_and_var
= "var"
130 def method_and_var(self
):
133 return method_and_var
134 def actual_global(self
):
141 verify(t
.test() == "var")
142 verify(t
.method_and_var() == "method")
143 verify(t
.actual_global() == "global")
145 method_and_var
= "var"
147 # this class is not nested, so the rules are different
148 def method_and_var(self
):
151 return method_and_var
152 def actual_global(self
):
158 verify(t
.test() == "var")
159 verify(t
.method_and_var() == "method")
160 verify(t
.actual_global() == "global")
162 print "10. recursion"
169 return n
* fact(n
- 1)
173 raise ValueError, "x must be >= 0"
178 print "11. unoptimized namespaces"
181 def unoptimized_clash1(strip):
184 return strip(s) # ambiguity: free or local
189 def unoptimized_clash2():
192 return strip(s) # ambiguity: global or local
197 def unoptimized_clash2():
201 return strip(s) # ambiguity: global or local
205 # XXX could allow this for exec with const argument, but what's the point
218 del x # can't del name
225 return strip # global or local?
228 # and verify a few cases that should work
247 f1
= lambda x
: lambda y
: x
+ y
251 verify(plus10(5) == 15)
253 f2
= lambda x
: (lambda : lambda y
: x
+ y
)()
257 verify(plus10(5) == 15)
259 f3
= lambda x
: lambda y
: global_x
+ y
264 f8
= lambda x
, y
, z
: lambda a
, b
, c
: lambda : z
* (b
+ y
)
269 print "13. UnboundLocal"
285 except UnboundLocalError:
297 print "14. complex definitions"
299 def makeReturner(*lst
):
304 verify(makeReturner(1,2,3)() == (1,2,3))
306 def makeReturner2(**kwargs
):
311 verify(makeReturner2(a
=11)()['a'] == 11)
313 def makeAddPair((a
, b
)):
315 return (a
+ c
, b
+ d
)
318 verify(makeAddPair((1, 2))((100, 200)) == (101,202))
320 print "15. scope of global statements"
321 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
385 print "16. check leaks"
405 verify(Foo
.count
== 0)
407 print "17. class and global"
412 def __call__(self
, y
):
417 verify(test(6)(2) == 8)
419 verify(test(3)(2) == 5)
421 print "18. verify that locals() works"
433 verify(d
.has_key('h'))
435 verify(d
== {'x': 2, 'y': 7, 'w': 6})
437 print "19. var is bound and free in class"
447 verify(inst
.a
== inst
.m())
449 print "20. interaction with trace function"
455 def adaptgetter(name
, klass
, getter
):
457 if kind
== 1: # AV happens when stepping from this line to next
459 des
= "_%s__%s" % (klass
.__name
__, name
)
460 return lambda obj
: getattr(obj
, des
)
466 adaptgetter("foo", TestClass
, (1, ""))