blob: f39bbc2037203d041c39fad457f769213c6d814a [file] [log] [blame]
Tim Petersd48004f2001-09-25 19:29:35 +00001from test_support import verify, TestFailed, check_syntax
Jeremy Hylton4588c782001-01-25 20:11:23 +00002
Jeremy Hyltoncd738362001-08-07 16:38:19 +00003import warnings
Guido van Rossum796e1e02001-12-15 18:04:10 +00004warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<string>")
Jeremy Hyltoncd738362001-08-07 16:38:19 +00005
Jeremy Hylton4588c782001-01-25 20:11:23 +00006print "1. simple nesting"
7
8def make_adder(x):
9 def adder(y):
10 return x + y
11 return adder
12
13inc = make_adder(1)
14plus10 = make_adder(10)
15
16verify(inc(1) == 2)
17verify(plus10(-2) == 8)
18
19print "2. extra nesting"
20
21def make_adder2(x):
22 def extra(): # check freevars passing through non-use scopes
23 def adder(y):
24 return x + y
25 return adder
26 return extra()
27
28inc = make_adder2(1)
29plus10 = make_adder2(10)
30
31verify(inc(1) == 2)
32verify(plus10(-2) == 8)
33
34print "3. simple nesting + rebinding"
35
36def make_adder3(x):
37 def adder(y):
38 return x + y
39 x = x + 1 # check tracking of assignment to x in defining scope
40 return adder
41
42inc = make_adder3(0)
43plus10 = make_adder3(9)
44
45verify(inc(1) == 2)
46verify(plus10(-2) == 8)
47
48print "4. nesting with global but no free"
49
50def make_adder4(): # XXX add exta level of indirection
51 def nest():
52 def nest():
53 def adder(y):
54 return global_x + y # check that plain old globals work
55 return adder
56 return nest()
57 return nest()
58
59global_x = 1
60adder = make_adder4()
61verify(adder(1) == 2)
62
63global_x = 10
64verify(adder(-2) == 8)
65
66print "5. nesting through class"
67
68def make_adder5(x):
69 class Adder:
70 def __call__(self, y):
71 return x + y
72 return Adder()
73
74inc = make_adder5(1)
75plus10 = make_adder5(10)
76
77verify(inc(1) == 2)
78verify(plus10(-2) == 8)
79
80print "6. nesting plus free ref to global"
81
82def make_adder6(x):
83 global global_nest_x
84 def adder(y):
85 return global_nest_x + y
86 global_nest_x = x
87 return adder
88
89inc = make_adder6(1)
90plus10 = make_adder6(10)
91
Jeremy Hylton251ef962001-01-30 01:26:53 +000092verify(inc(1) == 11) # there's only one global
Jeremy Hylton4588c782001-01-25 20:11:23 +000093verify(plus10(-2) == 8)
94
95print "7. nearest enclosing scope"
96
97def f(x):
98 def g(y):
99 x = 42 # check that this masks binding in f()
100 def h(z):
101 return x + z
102 return h
103 return g(2)
104
105test_func = f(10)
106verify(test_func(5) == 47)
107
108print "8. mixed freevars and cellvars"
109
110def identity(x):
Tim Peters10fb3862001-02-09 20:17:14 +0000111 return x
Jeremy Hylton4588c782001-01-25 20:11:23 +0000112
113def f(x, y, z):
114 def g(a, b, c):
115 a = a + x # 3
116 def h():
Tim Peters10fb3862001-02-09 20:17:14 +0000117 # z * (4 + 9)
118 # 3 * 13
Jeremy Hylton4588c782001-01-25 20:11:23 +0000119 return identity(z * (b + y))
120 y = c + z # 9
121 return h
122 return g
123
124g = f(1, 2, 3)
125h = g(2, 4, 6)
Tim Peters10fb3862001-02-09 20:17:14 +0000126verify(h() == 39)
Jeremy Hylton4588c782001-01-25 20:11:23 +0000127
128print "9. free variable in method"
129
130def test():
131 method_and_var = "var"
132 class Test:
133 def method_and_var(self):
134 return "method"
135 def test(self):
136 return method_and_var
137 def actual_global(self):
138 return str("global")
139 def str(self):
140 return str(self)
141 return Test()
142
143t = test()
144verify(t.test() == "var")
145verify(t.method_and_var() == "method")
146verify(t.actual_global() == "global")
147
148method_and_var = "var"
149class Test:
150 # this class is not nested, so the rules are different
151 def method_and_var(self):
152 return "method"
153 def test(self):
154 return method_and_var
155 def actual_global(self):
156 return str("global")
157 def str(self):
158 return str(self)
159
Jeremy Hyltonde602482001-02-05 17:35:20 +0000160t = Test()
Jeremy Hylton4588c782001-01-25 20:11:23 +0000161verify(t.test() == "var")
162verify(t.method_and_var() == "method")
163verify(t.actual_global() == "global")
164
165print "10. recursion"
166
167def f(x):
168 def fact(n):
169 if n == 0:
170 return 1
171 else:
172 return n * fact(n - 1)
173 if x >= 0:
174 return fact(x)
175 else:
176 raise ValueError, "x must be >= 0"
177
178verify(f(6) == 720)
179
180
181print "11. unoptimized namespaces"
182
Tim Petersc4889c42001-07-12 22:36:02 +0000183check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000184def unoptimized_clash1(strip):
Jeremy Hylton4588c782001-01-25 20:11:23 +0000185 def f(s):
186 from string import *
187 return strip(s) # ambiguity: free or local
188 return f
Jeremy Hylton97a01672001-02-09 22:56:46 +0000189""")
Jeremy Hylton4588c782001-01-25 20:11:23 +0000190
Tim Petersc4889c42001-07-12 22:36:02 +0000191check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000192def unoptimized_clash2():
Jeremy Hylton4588c782001-01-25 20:11:23 +0000193 from string import *
194 def f(s):
195 return strip(s) # ambiguity: global or local
196 return f
Jeremy Hylton97a01672001-02-09 22:56:46 +0000197""")
Jeremy Hylton4588c782001-01-25 20:11:23 +0000198
Tim Petersc4889c42001-07-12 22:36:02 +0000199check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000200def unoptimized_clash2():
Jeremy Hylton97a01672001-02-09 22:56:46 +0000201 from string import *
202 def g():
203 def f(s):
204 return strip(s) # ambiguity: global or local
205 return f
206""")
207
Tim Peters0e6d2132001-02-15 23:56:39 +0000208# XXX could allow this for exec with const argument, but what's the point
Tim Petersc4889c42001-07-12 22:36:02 +0000209check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000210def error(y):
Jeremy Hylton4588c782001-01-25 20:11:23 +0000211 exec "a = 1"
212 def f(x):
213 return x + y
214 return f
Jeremy Hylton97a01672001-02-09 22:56:46 +0000215""")
Jeremy Hylton4588c782001-01-25 20:11:23 +0000216
Tim Petersc4889c42001-07-12 22:36:02 +0000217check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000218def f(x):
Jeremy Hylton4588c782001-01-25 20:11:23 +0000219 def g():
220 return x
Jeremy Hylton97a01672001-02-09 22:56:46 +0000221 del x # can't del name
222""")
223
Tim Petersc4889c42001-07-12 22:36:02 +0000224check_syntax("""\
Jeremy Hylton5941d192001-02-27 20:23:58 +0000225def f():
Jeremy Hylton97a01672001-02-09 22:56:46 +0000226 def g():
227 from string import *
228 return strip # global or local?
Tim Peters0e6d2132001-02-15 23:56:39 +0000229""")
Jeremy Hylton97a01672001-02-09 22:56:46 +0000230
231# and verify a few cases that should work
232
Jeremy Hyltoncd738362001-08-07 16:38:19 +0000233exec """
Jeremy Hylton97a01672001-02-09 22:56:46 +0000234def noproblem1():
235 from string import *
236 f = lambda x:x
237
238def noproblem2():
239 from string import *
240 def f(x):
241 return x + 1
242
243def noproblem3():
244 from string import *
245 def f(x):
246 global y
247 y = x
Jeremy Hyltoncd738362001-08-07 16:38:19 +0000248"""
Jeremy Hylton4588c782001-01-25 20:11:23 +0000249
250print "12. lambdas"
251
252f1 = lambda x: lambda y: x + y
253inc = f1(1)
254plus10 = f1(10)
255verify(inc(1) == 2)
256verify(plus10(5) == 15)
257
258f2 = lambda x: (lambda : lambda y: x + y)()
259inc = f2(1)
260plus10 = f2(10)
261verify(inc(1) == 2)
262verify(plus10(5) == 15)
263
264f3 = lambda x: lambda y: global_x + y
265global_x = 1
266inc = f3(None)
267verify(inc(2) == 3)
268
269f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y)
270g = f8(1, 2, 3)
271h = g(2, 4, 6)
272verify(h() == 18)
Jeremy Hyltonde602482001-02-05 17:35:20 +0000273
274print "13. UnboundLocal"
275
276def errorInOuter():
277 print y
278 def inner():
279 return y
280 y = 1
281
282def errorInInner():
283 def inner():
284 return y
285 inner()
286 y = 1
287
288try:
289 errorInOuter()
290except UnboundLocalError:
291 pass
292else:
293 raise TestFailed
294
295try:
296 errorInInner()
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000297except NameError:
Jeremy Hyltonde602482001-02-05 17:35:20 +0000298 pass
299else:
300 raise TestFailed
Jeremy Hylton97a01672001-02-09 22:56:46 +0000301
302print "14. complex definitions"
303
304def makeReturner(*lst):
305 def returner():
306 return lst
307 return returner
Tim Peters0e6d2132001-02-15 23:56:39 +0000308
Jeremy Hylton97a01672001-02-09 22:56:46 +0000309verify(makeReturner(1,2,3)() == (1,2,3))
Tim Peters0e6d2132001-02-15 23:56:39 +0000310
Jeremy Hylton97a01672001-02-09 22:56:46 +0000311def makeReturner2(**kwargs):
312 def returner():
313 return kwargs
314 return returner
315
316verify(makeReturner2(a=11)()['a'] == 11)
317
318def makeAddPair((a, b)):
319 def addPair((c, d)):
320 return (a + c, b + d)
321 return addPair
322
323verify(makeAddPair((1, 2))((100, 200)) == (101,202))
Guido van Rossum9aa643c2001-03-01 20:35:45 +0000324
325print "15. scope of global statements"
326# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
327
328# I
329x = 7
330def f():
331 x = 1
332 def g():
333 global x
334 def i():
335 def h():
336 return x
337 return h()
338 return i()
339 return g()
340verify(f() == 7)
341verify(x == 7)
342
343# II
344x = 7
345def f():
346 x = 1
347 def g():
348 x = 2
349 def i():
350 def h():
351 return x
352 return h()
353 return i()
354 return g()
355verify(f() == 2)
356verify(x == 7)
357
358# III
359x = 7
360def f():
361 x = 1
362 def g():
363 global x
364 x = 2
365 def i():
366 def h():
367 return x
368 return h()
369 return i()
370 return g()
371verify(f() == 2)
372verify(x == 2)
373
374# IV
375x = 7
376def f():
377 x = 3
378 def g():
379 global x
380 x = 2
381 def i():
382 def h():
383 return x
384 return h()
385 return i()
386 return g()
387verify(f() == 2)
388verify(x == 2)
Jeremy Hylton5b44a672001-03-13 02:01:12 +0000389
390print "16. check leaks"
391
392class Foo:
393 count = 0
Tim Peters30edd232001-03-16 08:29:48 +0000394
Jeremy Hylton5b44a672001-03-13 02:01:12 +0000395 def __init__(self):
396 Foo.count += 1
397
398 def __del__(self):
399 Foo.count -= 1
400
401def f1():
402 x = Foo()
403 def f2():
404 return x
405 f2()
Tim Peters30edd232001-03-16 08:29:48 +0000406
Jeremy Hylton5b44a672001-03-13 02:01:12 +0000407for i in range(100):
408 f1()
409
410verify(Foo.count == 0)
Jeremy Hylton5c7a2512001-03-21 16:44:39 +0000411
412print "17. class and global"
413
414def test(x):
415 class Foo:
416 global x
417 def __call__(self, y):
418 return x + y
419 return Foo()
420
421x = 0
422verify(test(6)(2) == 8)
423x = -1
424verify(test(3)(2) == 5)
425
426print "18. verify that locals() works"
427
428def f(x):
429 def g(y):
430 def h(z):
431 return y + z
432 w = x + y
433 y += 3
434 return locals()
435 return g
436
437d = f(2)(4)
438verify(d.has_key('h'))
439del d['h']
440verify(d == {'x': 2, 'y': 7, 'w': 6})
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000441
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +0000442print "19. var is bound and free in class"
443
444def f(x):
445 class C:
446 def m(self):
447 return x
448 a = x
449 return C
450
451inst = f(3)()
452verify(inst.a == inst.m())
Jeremy Hylton4c889012001-05-08 04:08:59 +0000453
454print "20. interaction with trace function"
455
456import sys
457def tracer(a,b,c):
458 return tracer
459
460def adaptgetter(name, klass, getter):
461 kind, des = getter
462 if kind == 1: # AV happens when stepping from this line to next
463 if des == "":
Jeremy Hyltonf36cfef2001-12-13 20:00:26 +0000464 des = "_%s__%s" % (klass.__name__, name)
Jeremy Hylton4c889012001-05-08 04:08:59 +0000465 return lambda obj: getattr(obj, des)
466
467class TestClass:
468 pass
469
470sys.settrace(tracer)
471adaptgetter("foo", TestClass, (1, ""))
472sys.settrace(None)
Jeremy Hylton5121e7d2001-07-30 21:55:29 +0000473
Neal Norwitz290d31e2002-03-03 15:12:58 +0000474try: sys.settrace()
475except TypeError: pass
476else: raise TestFailed, 'sys.settrace() did not raise TypeError'
477
Jeremy Hyltonccae83772001-12-13 19:45:04 +0000478print "20. eval and exec with free variables"
Jeremy Hylton5121e7d2001-07-30 21:55:29 +0000479
480def f(x):
481 return lambda: x + 1
482
483g = f(3)
484try:
485 eval(g.func_code)
486except TypeError:
487 pass
488else:
489 print "eval() should have failed, because code contained free vars"
Jeremy Hyltoncd738362001-08-07 16:38:19 +0000490
Jeremy Hyltonccae83772001-12-13 19:45:04 +0000491try:
492 exec g.func_code
493except TypeError:
494 pass
495else:
496 print "exec should have failed, because code contained free vars"
497
Jeremy Hyltoncf672f12001-10-18 16:23:11 +0000498print "21. list comprehension with local variables"
499
500try:
501 print bad
502except NameError:
503 pass
504else:
505 print "bad should not be defined"
506
507def x():
508 [bad for s in 'a b' for bad in s.split()]
509
510x()
511try:
512 print bad
513except NameError:
514 pass
Jeremy Hylton954aed82002-04-20 04:51:39 +0000515
516print "22. eval with free variables"
517
518def f(x):
519 def g():
520 x
521 eval("x + 1")
522 return g
523
524f(4)()