1 from __future__
import nested_scopes
3 from test
.test_support
import verify
, TestFailed
, check_syntax
5 print "1. simple nesting"
13 plus10
= make_adder(10)
16 verify(plus10(-2) == 8)
18 print "2. extra nesting"
21 def extra(): # check freevars passing through non-use scopes
28 plus10
= make_adder2(10)
31 verify(plus10(-2) == 8)
33 print "3. simple nesting + rebinding"
38 x
= x
+ 1 # check tracking of assignment to x in defining scope
42 plus10
= make_adder3(9)
45 verify(plus10(-2) == 8)
47 print "4. nesting with global but no free"
49 def make_adder4(): # XXX add exta level of indirection
53 return global_x
+ y
# check that plain old globals work
63 verify(adder(-2) == 8)
65 print "5. nesting through class"
69 def __call__(self
, y
):
74 plus10
= make_adder5(10)
77 verify(plus10(-2) == 8)
79 print "6. nesting plus free ref to global"
84 return global_nest_x
+ y
89 plus10
= make_adder6(10)
91 verify(inc(1) == 11) # there's only one global
92 verify(plus10(-2) == 8)
94 print "7. nearest enclosing scope"
98 x
= 42 # check that this masks binding in f()
105 verify(test_func(5) == 47)
107 print "8. mixed freevars and cellvars"
118 return identity(z
* (b
+ y
))
127 print "9. free variable in method"
130 method_and_var
= "var"
132 def method_and_var(self
):
135 return method_and_var
136 def actual_global(self
):
143 verify(t
.test() == "var")
144 verify(t
.method_and_var() == "method")
145 verify(t
.actual_global() == "global")
147 method_and_var
= "var"
149 # this class is not nested, so the rules are different
150 def method_and_var(self
):
153 return method_and_var
154 def actual_global(self
):
160 verify(t
.test() == "var")
161 verify(t
.method_and_var() == "method")
162 verify(t
.actual_global() == "global")
164 print "10. recursion"
171 return n
* fact(n
- 1)
175 raise ValueError, "x must be >= 0"
180 print "11. unoptimized namespaces"
182 check_syntax("""from __future__ import nested_scopes
183 def unoptimized_clash1(strip):
186 return strip(s) # ambiguity: free or local
190 check_syntax("""from __future__ import nested_scopes
191 def unoptimized_clash2():
194 return strip(s) # ambiguity: global or local
198 check_syntax("""from __future__ import nested_scopes
199 def unoptimized_clash2():
203 return strip(s) # ambiguity: global or local
207 # XXX could allow this for exec with const argument, but what's the point
208 check_syntax("""from __future__ import nested_scopes
216 check_syntax("""from __future__ import nested_scopes
220 del x # can't del name
223 check_syntax("""from __future__ import nested_scopes
227 return strip # global or local?
230 # and verify a few cases that should work
249 f1
= lambda x
: lambda y
: x
+ y
253 verify(plus10(5) == 15)
255 f2
= lambda x
: (lambda : lambda y
: x
+ y
)()
259 verify(plus10(5) == 15)
261 f3
= lambda x
: lambda y
: global_x
+ y
266 f8
= lambda x
, y
, z
: lambda a
, b
, c
: lambda : z
* (b
+ y
)
271 print "13. UnboundLocal"
287 except UnboundLocalError:
294 except UnboundLocalError:
299 print "14. complex definitions"
301 def makeReturner(*lst
):
306 verify(makeReturner(1,2,3)() == (1,2,3))
308 def makeReturner2(**kwargs
):
313 verify(makeReturner2(a
=11)()['a'] == 11)
315 def makeAddPair((a
, b
)):
317 return (a
+ c
, b
+ d
)
320 verify(makeAddPair((1, 2))((100, 200)) == (101,202))
322 print "15. scope of global statements"
323 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001