make file closing more robust
[python/dscho.git] / Lib / test / test_scope.py
blobfb1e26f1f512b10bc8f35b338d873cd793310d1c
1 import unittest
2 from test.support import check_syntax_error, run_unittest
4 import warnings
5 warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<test string>")
6 warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<string>")
8 class ScopeTests(unittest.TestCase):
10 def testSimpleNesting(self):
12 def make_adder(x):
13 def adder(y):
14 return x + y
15 return adder
17 inc = make_adder(1)
18 plus10 = make_adder(10)
20 self.assertEqual(inc(1), 2)
21 self.assertEqual(plus10(-2), 8)
23 def testExtraNesting(self):
25 def make_adder2(x):
26 def extra(): # check freevars passing through non-use scopes
27 def adder(y):
28 return x + y
29 return adder
30 return extra()
32 inc = make_adder2(1)
33 plus10 = make_adder2(10)
35 self.assertEqual(inc(1), 2)
36 self.assertEqual(plus10(-2), 8)
38 def testSimpleAndRebinding(self):
40 def make_adder3(x):
41 def adder(y):
42 return x + y
43 x = x + 1 # check tracking of assignment to x in defining scope
44 return adder
46 inc = make_adder3(0)
47 plus10 = make_adder3(9)
49 self.assertEqual(inc(1), 2)
50 self.assertEqual(plus10(-2), 8)
52 def testNestingGlobalNoFree(self):
54 def make_adder4(): # XXX add exta level of indirection
55 def nest():
56 def nest():
57 def adder(y):
58 return global_x + y # check that plain old globals work
59 return adder
60 return nest()
61 return nest()
63 global_x = 1
64 adder = make_adder4()
65 self.assertEqual(adder(1), 2)
67 global_x = 10
68 self.assertEqual(adder(-2), 8)
70 def testNestingThroughClass(self):
72 def make_adder5(x):
73 class Adder:
74 def __call__(self, y):
75 return x + y
76 return Adder()
78 inc = make_adder5(1)
79 plus10 = make_adder5(10)
81 self.assertEqual(inc(1), 2)
82 self.assertEqual(plus10(-2), 8)
84 def testNestingPlusFreeRefToGlobal(self):
86 def make_adder6(x):
87 global global_nest_x
88 def adder(y):
89 return global_nest_x + y
90 global_nest_x = x
91 return adder
93 inc = make_adder6(1)
94 plus10 = make_adder6(10)
96 self.assertEqual(inc(1), 11) # there's only one global
97 self.assertEqual(plus10(-2), 8)
99 def testNearestEnclosingScope(self):
101 def f(x):
102 def g(y):
103 x = 42 # check that this masks binding in f()
104 def h(z):
105 return x + z
106 return h
107 return g(2)
109 test_func = f(10)
110 self.assertEqual(test_func(5), 47)
112 def testMixedFreevarsAndCellvars(self):
114 def identity(x):
115 return x
117 def f(x, y, z):
118 def g(a, b, c):
119 a = a + x # 3
120 def h():
121 # z * (4 + 9)
122 # 3 * 13
123 return identity(z * (b + y))
124 y = c + z # 9
125 return h
126 return g
128 g = f(1, 2, 3)
129 h = g(2, 4, 6)
130 self.assertEqual(h(), 39)
132 def testFreeVarInMethod(self):
134 def test():
135 method_and_var = "var"
136 class Test:
137 def method_and_var(self):
138 return "method"
139 def test(self):
140 return method_and_var
141 def actual_global(self):
142 return str("global")
143 def str(self):
144 return str(self)
145 return Test()
147 t = test()
148 self.assertEqual(t.test(), "var")
149 self.assertEqual(t.method_and_var(), "method")
150 self.assertEqual(t.actual_global(), "global")
152 method_and_var = "var"
153 class Test:
154 # this class is not nested, so the rules are different
155 def method_and_var(self):
156 return "method"
157 def test(self):
158 return method_and_var
159 def actual_global(self):
160 return str("global")
161 def str(self):
162 return str(self)
164 t = Test()
165 self.assertEqual(t.test(), "var")
166 self.assertEqual(t.method_and_var(), "method")
167 self.assertEqual(t.actual_global(), "global")
169 def testCellIsKwonlyArg(self):
170 # Issue 1409: Initialisation of a cell value,
171 # when it comes from a keyword-only parameter
172 def foo(*, a=17):
173 def bar():
174 return a + 5
175 return bar() + 3
177 self.assertEqual(foo(a=42), 50)
178 self.assertEqual(foo(), 25)
180 def testRecursion(self):
182 def f(x):
183 def fact(n):
184 if n == 0:
185 return 1
186 else:
187 return n * fact(n - 1)
188 if x >= 0:
189 return fact(x)
190 else:
191 raise ValueError("x must be >= 0")
193 self.assertEqual(f(6), 720)
196 def testUnoptimizedNamespaces(self):
198 check_syntax_error(self, """\
199 def unoptimized_clash1(strip):
200 def f(s):
201 from sys import *
202 return getrefcount(s) # ambiguity: free or local
203 return f
204 """)
206 check_syntax_error(self, """\
207 def unoptimized_clash2():
208 from sys import *
209 def f(s):
210 return getrefcount(s) # ambiguity: global or local
211 return f
212 """)
214 check_syntax_error(self, """\
215 def unoptimized_clash2():
216 from sys import *
217 def g():
218 def f(s):
219 return getrefcount(s) # ambiguity: global or local
220 return f
221 """)
223 check_syntax_error(self, """\
224 def f(x):
225 def g():
226 return x
227 del x # can't del name
228 """)
230 check_syntax_error(self, """\
231 def f():
232 def g():
233 from sys import *
234 return getrefcount # global or local?
235 """)
237 def testLambdas(self):
239 f1 = lambda x: lambda y: x + y
240 inc = f1(1)
241 plus10 = f1(10)
242 self.assertEqual(inc(1), 2)
243 self.assertEqual(plus10(5), 15)
245 f2 = lambda x: (lambda : lambda y: x + y)()
246 inc = f2(1)
247 plus10 = f2(10)
248 self.assertEqual(inc(1), 2)
249 self.assertEqual(plus10(5), 15)
251 f3 = lambda x: lambda y: global_x + y
252 global_x = 1
253 inc = f3(None)
254 self.assertEqual(inc(2), 3)
256 f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y)
257 g = f8(1, 2, 3)
258 h = g(2, 4, 6)
259 self.assertEqual(h(), 18)
261 def testUnboundLocal(self):
263 def errorInOuter():
264 print(y)
265 def inner():
266 return y
267 y = 1
269 def errorInInner():
270 def inner():
271 return y
272 inner()
273 y = 1
275 try:
276 errorInOuter()
277 except UnboundLocalError:
278 pass
279 else:
280 self.fail()
282 try:
283 errorInInner()
284 except NameError:
285 pass
286 else:
287 self.fail()
289 # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
290 exec("""
291 global_x = 1
292 def f():
293 global_x += 1
294 try:
296 except UnboundLocalError:
297 pass
298 else:
299 fail('scope of global_x not correctly determined')
300 """, {'fail': self.fail})
302 def testComplexDefinitions(self):
304 def makeReturner(*lst):
305 def returner():
306 return lst
307 return returner
309 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
311 def makeReturner2(**kwargs):
312 def returner():
313 return kwargs
314 return returner
316 self.assertEqual(makeReturner2(a=11)()['a'], 11)
318 def testScopeOfGlobalStmt(self):
319 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
321 exec("""\
323 x = 7
324 def f():
325 x = 1
326 def g():
327 global x
328 def i():
329 def h():
330 return x
331 return h()
332 return i()
333 return g()
334 self.assertEqual(f(), 7)
335 self.assertEqual(x, 7)
337 # II
338 x = 7
339 def f():
340 x = 1
341 def g():
342 x = 2
343 def i():
344 def h():
345 return x
346 return h()
347 return i()
348 return g()
349 self.assertEqual(f(), 2)
350 self.assertEqual(x, 7)
352 # III
353 x = 7
354 def f():
355 x = 1
356 def g():
357 global x
358 x = 2
359 def i():
360 def h():
361 return x
362 return h()
363 return i()
364 return g()
365 self.assertEqual(f(), 2)
366 self.assertEqual(x, 2)
368 # IV
369 x = 7
370 def f():
371 x = 3
372 def g():
373 global x
374 x = 2
375 def i():
376 def h():
377 return x
378 return h()
379 return i()
380 return g()
381 self.assertEqual(f(), 2)
382 self.assertEqual(x, 2)
384 # XXX what about global statements in class blocks?
385 # do they affect methods?
387 x = 12
388 class Global:
389 global x
390 x = 13
391 def set(self, val):
392 x = val
393 def get(self):
394 return x
396 g = Global()
397 self.assertEqual(g.get(), 13)
398 g.set(15)
399 self.assertEqual(g.get(), 13)
400 """)
402 def testLeaks(self):
404 class Foo:
405 count = 0
407 def __init__(self):
408 Foo.count += 1
410 def __del__(self):
411 Foo.count -= 1
413 def f1():
414 x = Foo()
415 def f2():
416 return x
417 f2()
419 for i in range(100):
420 f1()
422 self.assertEqual(Foo.count, 0)
424 def testClassAndGlobal(self):
426 exec("""\
427 def test(x):
428 class Foo:
429 global x
430 def __call__(self, y):
431 return x + y
432 return Foo()
434 x = 0
435 self.assertEqual(test(6)(2), 8)
436 x = -1
437 self.assertEqual(test(3)(2), 5)
439 looked_up_by_load_name = False
440 class X:
441 # Implicit globals inside classes are be looked up by LOAD_NAME, not
442 # LOAD_GLOBAL.
443 locals()['looked_up_by_load_name'] = True
444 passed = looked_up_by_load_name
446 self.assert_(X.passed)
447 """)
449 def testLocalsFunction(self):
451 def f(x):
452 def g(y):
453 def h(z):
454 return y + z
455 w = x + y
456 y += 3
457 return locals()
458 return g
460 d = f(2)(4)
461 self.assert_('h' in d)
462 del d['h']
463 self.assertEqual(d, {'x': 2, 'y': 7, 'w': 6})
465 def testLocalsClass(self):
466 # This test verifies that calling locals() does not pollute
467 # the local namespace of the class with free variables. Old
468 # versions of Python had a bug, where a free variable being
469 # passed through a class namespace would be inserted into
470 # locals() by locals() or exec or a trace function.
472 # The real bug lies in frame code that copies variables
473 # between fast locals and the locals dict, e.g. when executing
474 # a trace function.
476 def f(x):
477 class C:
478 x = 12
479 def m(self):
480 return x
481 locals()
482 return C
484 self.assertEqual(f(1).x, 12)
486 def f(x):
487 class C:
488 y = x
489 def m(self):
490 return x
491 z = list(locals())
492 return C
494 varnames = f(1).z
495 self.assert_("x" not in varnames)
496 self.assert_("y" in varnames)
498 def testLocalsClass_WithTrace(self):
499 # Issue23728: after the trace function returns, the locals()
500 # dictionary is used to update all variables, this used to
501 # include free variables. But in class statements, free
502 # variables are not inserted...
503 import sys
504 sys.settrace(lambda a,b,c:None)
505 try:
506 x = 12
508 class C:
509 def f(self):
510 return x
512 self.assertEquals(x, 12) # Used to raise UnboundLocalError
513 finally:
514 sys.settrace(None)
516 def testBoundAndFree(self):
517 # var is bound and free in class
519 def f(x):
520 class C:
521 def m(self):
522 return x
523 a = x
524 return C
526 inst = f(3)()
527 self.assertEqual(inst.a, inst.m())
529 def testInteractionWithTraceFunc(self):
531 import sys
532 def tracer(a,b,c):
533 return tracer
535 def adaptgetter(name, klass, getter):
536 kind, des = getter
537 if kind == 1: # AV happens when stepping from this line to next
538 if des == "":
539 des = "_%s__%s" % (klass.__name__, name)
540 return lambda obj: getattr(obj, des)
542 class TestClass:
543 pass
545 sys.settrace(tracer)
546 adaptgetter("foo", TestClass, (1, ""))
547 sys.settrace(None)
549 self.assertRaises(TypeError, sys.settrace)
551 def testEvalExecFreeVars(self):
553 def f(x):
554 return lambda: x + 1
556 g = f(3)
557 self.assertRaises(TypeError, eval, g.__code__)
559 try:
560 exec(g.__code__, {})
561 except TypeError:
562 pass
563 else:
564 self.fail("exec should have failed, because code contained free vars")
566 def testListCompLocalVars(self):
568 try:
569 print(bad)
570 except NameError:
571 pass
572 else:
573 print("bad should not be defined")
575 def x():
576 [bad for s in 'a b' for bad in s.split()]
579 try:
580 print(bad)
581 except NameError:
582 pass
584 def testEvalFreeVars(self):
586 def f(x):
587 def g():
589 eval("x + 1")
590 return g
592 f(4)()
594 def testFreeingCell(self):
595 # Test what happens when a finalizer accesses
596 # the cell where the object was stored.
597 class Special:
598 def __del__(self):
599 nestedcell_get()
601 def testNonLocalFunction(self):
603 def f(x):
604 def inc():
605 nonlocal x
606 x += 1
607 return x
608 def dec():
609 nonlocal x
610 x -= 1
611 return x
612 return inc, dec
614 inc, dec = f(0)
615 self.assertEqual(inc(), 1)
616 self.assertEqual(inc(), 2)
617 self.assertEqual(dec(), 1)
618 self.assertEqual(dec(), 0)
620 def testNonLocalMethod(self):
621 def f(x):
622 class c:
623 def inc(self):
624 nonlocal x
625 x += 1
626 return x
627 def dec(self):
628 nonlocal x
629 x -= 1
630 return x
631 return c()
632 c = f(0)
633 self.assertEqual(c.inc(), 1)
634 self.assertEqual(c.inc(), 2)
635 self.assertEqual(c.dec(), 1)
636 self.assertEqual(c.dec(), 0)
638 def testGlobalInParallelNestedFunctions(self):
639 # A symbol table bug leaked the global statement from one
640 # function to other nested functions in the same block.
641 # This test verifies that a global statement in the first
642 # function does not affect the second function.
643 CODE = """def f():
644 y = 1
645 def g():
646 global y
647 return y
648 def h():
649 return y + 1
650 return g, h
651 y = 9
652 g, h = f()
653 result9 = g()
654 result2 = h()
656 local_ns = {}
657 global_ns = {}
658 exec(CODE, local_ns, global_ns)
659 self.assertEqual(2, global_ns["result2"])
660 self.assertEqual(9, global_ns["result9"])
662 def testNonLocalClass(self):
664 def f(x):
665 class c:
666 nonlocal x
667 x += 1
668 def get(self):
669 return x
670 return c()
672 c = f(0)
673 self.assertEqual(c.get(), 1)
674 self.assert_("x" not in c.__class__.__dict__)
677 def testNonLocalGenerator(self):
679 def f(x):
680 def g(y):
681 nonlocal x
682 for i in range(y):
683 x += 1
684 yield x
685 return g
687 g = f(0)
688 self.assertEqual(list(g(5)), [1, 2, 3, 4, 5])
690 def testNestedNonLocal(self):
692 def f(x):
693 def g():
694 nonlocal x
695 x -= 2
696 def h():
697 nonlocal x
698 x += 4
699 return x
700 return h
701 return g
703 g = f(1)
704 h = g()
705 self.assertEqual(h(), 3)
708 def test_main():
709 run_unittest(ScopeTests)
711 if __name__ == '__main__':
712 test_main()