from test
.test_support
import verify
, TestFailed
, check_syntax
warnings
.filterwarnings("ignore", r
"import \*", SyntaxWarning, "<string>")
print "1. simple nesting"
def extra(): # check freevars passing through non-use scopes
print "3. simple nesting + rebinding"
x
= x
+ 1 # check tracking of assignment to x in defining scope
print "4. nesting with global but no free"
def make_adder4(): # XXX add exta level of indirection
return global_x
+ y
# check that plain old globals work
print "5. nesting through class"
print "6. nesting plus free ref to global"
verify(inc(1) == 11) # there's only one global
print "7. nearest enclosing scope"
x
= 42 # check that this masks binding in f()
verify(test_func(5) == 47)
print "8. mixed freevars and cellvars"
return identity(z
* (b
+ y
))
print "9. free variable in method"
def method_and_var(self
):
verify(t
.test() == "var")
verify(t
.method_and_var() == "method")
verify(t
.actual_global() == "global")
# this class is not nested, so the rules are different
def method_and_var(self
):
verify(t
.test() == "var")
verify(t
.method_and_var() == "method")
verify(t
.actual_global() == "global")
raise ValueError, "x must be >= 0"
print "11. unoptimized namespaces"
def unoptimized_clash1(strip):
return strip(s) # ambiguity: free or local
def unoptimized_clash2():
return strip(s) # ambiguity: global or local
def unoptimized_clash2():
return strip(s) # ambiguity: global or local
# XXX could allow this for exec with const argument, but what's the point
return strip # global or local?
# and verify a few cases that should work
f1
= lambda x
: lambda y
: x
+ y
f2
= lambda x
: (lambda : lambda y
: x
+ y
)()
f3
= lambda x
: lambda y
: global_x
+ y
f8
= lambda x
, y
, z
: lambda a
, b
, c
: lambda : z
* (b
+ y
)
except UnboundLocalError:
print "14. complex definitions"
verify(makeReturner(1,2,3)() == (1,2,3))
def makeReturner2(**kwargs
):
verify(makeReturner2(a
=11)()['a'] == 11)
verify(makeAddPair((1, 2))((100, 200)) == (101,202))
print "15. scope of global statements"
# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
print "17. class and global"
print "18. verify that locals() works"
verify(d
== {'x': 2, 'y': 7, 'w': 6})
print "19. var is bound and free in class"
verify(inst
.a
== inst
.m())
print "20. interaction with trace function"
def adaptgetter(name
, klass
, getter
):
if kind
== 1: # AV happens when stepping from this line to next
des
= "_%s__%s" % (klass
.__name
__, name
)
return lambda obj
: getattr(obj
, des
)
adaptgetter("foo", TestClass
, (1, ""))
else: raise TestFailed
, 'sys.settrace() did not raise TypeError'
print "20. eval and exec with free variables"
print "eval() should have failed, because code contained free vars"
print "exec should have failed, because code contained free vars"
print "21. list comprehension with local variables"
print "bad should not be defined"
[bad
for s
in 'a b' for bad
in s
.split()]
print "22. eval with free variables"