Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
[python.git] / Lib / test / test_scope.py
blobcf30ff9c2df7cfee8df72921eed73eda2432cc12
1 import unittest
2 from test.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 testRecursion(self):
171 def f(x):
172 def fact(n):
173 if n == 0:
174 return 1
175 else:
176 return n * fact(n - 1)
177 if x >= 0:
178 return fact(x)
179 else:
180 raise ValueError, "x must be >= 0"
182 self.assertEqual(f(6), 720)
185 def testUnoptimizedNamespaces(self):
187 check_syntax_error(self, """\
188 def unoptimized_clash1(strip):
189 def f(s):
190 from string import *
191 return strip(s) # ambiguity: free or local
192 return f
193 """)
195 check_syntax_error(self, """\
196 def unoptimized_clash2():
197 from string import *
198 def f(s):
199 return strip(s) # ambiguity: global or local
200 return f
201 """)
203 check_syntax_error(self, """\
204 def unoptimized_clash2():
205 from string import *
206 def g():
207 def f(s):
208 return strip(s) # ambiguity: global or local
209 return f
210 """)
212 # XXX could allow this for exec with const argument, but what's the point
213 check_syntax_error(self, """\
214 def error(y):
215 exec "a = 1"
216 def f(x):
217 return x + y
218 return f
219 """)
221 check_syntax_error(self, """\
222 def f(x):
223 def g():
224 return x
225 del x # can't del name
226 """)
228 check_syntax_error(self, """\
229 def f():
230 def g():
231 from string import *
232 return strip # global or local?
233 """)
235 # and verify a few cases that should work
237 exec """
238 def noproblem1():
239 from string import *
240 f = lambda x:x
242 def noproblem2():
243 from string import *
244 def f(x):
245 return x + 1
247 def noproblem3():
248 from string import *
249 def f(x):
250 global y
251 y = x
254 def testLambdas(self):
256 f1 = lambda x: lambda y: x + y
257 inc = f1(1)
258 plus10 = f1(10)
259 self.assertEqual(inc(1), 2)
260 self.assertEqual(plus10(5), 15)
262 f2 = lambda x: (lambda : lambda y: x + y)()
263 inc = f2(1)
264 plus10 = f2(10)
265 self.assertEqual(inc(1), 2)
266 self.assertEqual(plus10(5), 15)
268 f3 = lambda x: lambda y: global_x + y
269 global_x = 1
270 inc = f3(None)
271 self.assertEqual(inc(2), 3)
273 f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y)
274 g = f8(1, 2, 3)
275 h = g(2, 4, 6)
276 self.assertEqual(h(), 18)
278 def testUnboundLocal(self):
280 def errorInOuter():
281 print y
282 def inner():
283 return y
284 y = 1
286 def errorInInner():
287 def inner():
288 return y
289 inner()
290 y = 1
292 self.assertRaises(UnboundLocalError, errorInOuter)
293 self.assertRaises(NameError, errorInInner)
295 # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
296 exec """
297 global_x = 1
298 def f():
299 global_x += 1
300 try:
302 except UnboundLocalError:
303 pass
304 else:
305 fail('scope of global_x not correctly determined')
306 """ in {'fail': self.fail}
308 def testComplexDefinitions(self):
310 def makeReturner(*lst):
311 def returner():
312 return lst
313 return returner
315 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
317 def makeReturner2(**kwargs):
318 def returner():
319 return kwargs
320 return returner
322 self.assertEqual(makeReturner2(a=11)()['a'], 11)
324 def makeAddPair((a, b)):
325 def addPair((c, d)):
326 return (a + c, b + d)
327 return addPair
329 self.assertEqual(makeAddPair((1, 2))((100, 200)), (101,202))
331 def testScopeOfGlobalStmt(self):
332 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
334 exec """\
336 x = 7
337 def f():
338 x = 1
339 def g():
340 global x
341 def i():
342 def h():
343 return x
344 return h()
345 return i()
346 return g()
347 self.assertEqual(f(), 7)
348 self.assertEqual(x, 7)
350 # II
351 x = 7
352 def f():
353 x = 1
354 def g():
355 x = 2
356 def i():
357 def h():
358 return x
359 return h()
360 return i()
361 return g()
362 self.assertEqual(f(), 2)
363 self.assertEqual(x, 7)
365 # III
366 x = 7
367 def f():
368 x = 1
369 def g():
370 global x
371 x = 2
372 def i():
373 def h():
374 return x
375 return h()
376 return i()
377 return g()
378 self.assertEqual(f(), 2)
379 self.assertEqual(x, 2)
381 # IV
382 x = 7
383 def f():
384 x = 3
385 def g():
386 global x
387 x = 2
388 def i():
389 def h():
390 return x
391 return h()
392 return i()
393 return g()
394 self.assertEqual(f(), 2)
395 self.assertEqual(x, 2)
397 # XXX what about global statements in class blocks?
398 # do they affect methods?
400 x = 12
401 class Global:
402 global x
403 x = 13
404 def set(self, val):
405 x = val
406 def get(self):
407 return x
409 g = Global()
410 self.assertEqual(g.get(), 13)
411 g.set(15)
412 self.assertEqual(g.get(), 13)
415 def testLeaks(self):
417 class Foo:
418 count = 0
420 def __init__(self):
421 Foo.count += 1
423 def __del__(self):
424 Foo.count -= 1
426 def f1():
427 x = Foo()
428 def f2():
429 return x
430 f2()
432 for i in range(100):
433 f1()
435 self.assertEqual(Foo.count, 0)
437 def testClassAndGlobal(self):
439 exec """\
440 def test(x):
441 class Foo:
442 global x
443 def __call__(self, y):
444 return x + y
445 return Foo()
447 x = 0
448 self.assertEqual(test(6)(2), 8)
449 x = -1
450 self.assertEqual(test(3)(2), 5)
452 looked_up_by_load_name = False
453 class X:
454 # Implicit globals inside classes are be looked up by LOAD_NAME, not
455 # LOAD_GLOBAL.
456 locals()['looked_up_by_load_name'] = True
457 passed = looked_up_by_load_name
459 self.assertTrue(X.passed)
462 def testLocalsFunction(self):
464 def f(x):
465 def g(y):
466 def h(z):
467 return y + z
468 w = x + y
469 y += 3
470 return locals()
471 return g
473 d = f(2)(4)
474 self.assertTrue(d.has_key('h'))
475 del d['h']
476 self.assertEqual(d, {'x': 2, 'y': 7, 'w': 6})
478 def testLocalsClass(self):
479 # This test verifies that calling locals() does not pollute
480 # the local namespace of the class with free variables. Old
481 # versions of Python had a bug, where a free variable being
482 # passed through a class namespace would be inserted into
483 # locals() by locals() or exec or a trace function.
485 # The real bug lies in frame code that copies variables
486 # between fast locals and the locals dict, e.g. when executing
487 # a trace function.
489 def f(x):
490 class C:
491 x = 12
492 def m(self):
493 return x
494 locals()
495 return C
497 self.assertEqual(f(1).x, 12)
499 def f(x):
500 class C:
501 y = x
502 def m(self):
503 return x
504 z = list(locals())
505 return C
507 varnames = f(1).z
508 self.assertTrue("x" not in varnames)
509 self.assertTrue("y" in varnames)
511 def testLocalsClass_WithTrace(self):
512 # Issue23728: after the trace function returns, the locals()
513 # dictionary is used to update all variables, this used to
514 # include free variables. But in class statements, free
515 # variables are not inserted...
516 import sys
517 sys.settrace(lambda a,b,c:None)
518 try:
519 x = 12
521 class C:
522 def f(self):
523 return x
525 self.assertEquals(x, 12) # Used to raise UnboundLocalError
526 finally:
527 sys.settrace(None)
529 def testBoundAndFree(self):
530 # var is bound and free in class
532 def f(x):
533 class C:
534 def m(self):
535 return x
536 a = x
537 return C
539 inst = f(3)()
540 self.assertEqual(inst.a, inst.m())
542 def testInteractionWithTraceFunc(self):
544 import sys
545 def tracer(a,b,c):
546 return tracer
548 def adaptgetter(name, klass, getter):
549 kind, des = getter
550 if kind == 1: # AV happens when stepping from this line to next
551 if des == "":
552 des = "_%s__%s" % (klass.__name__, name)
553 return lambda obj: getattr(obj, des)
555 class TestClass:
556 pass
558 sys.settrace(tracer)
559 adaptgetter("foo", TestClass, (1, ""))
560 sys.settrace(None)
562 self.assertRaises(TypeError, sys.settrace)
564 def testEvalExecFreeVars(self):
566 def f(x):
567 return lambda: x + 1
569 g = f(3)
570 self.assertRaises(TypeError, eval, g.func_code)
572 try:
573 exec g.func_code in {}
574 except TypeError:
575 pass
576 else:
577 self.fail("exec should have failed, because code contained free vars")
579 def testListCompLocalVars(self):
581 try:
582 print bad
583 except NameError:
584 pass
585 else:
586 print "bad should not be defined"
588 def x():
589 [bad for s in 'a b' for bad in s.split()]
592 try:
593 print bad
594 except NameError:
595 pass
597 def testEvalFreeVars(self):
599 def f(x):
600 def g():
602 eval("x + 1")
603 return g
605 f(4)()
607 def testFreeingCell(self):
608 # Test what happens when a finalizer accesses
609 # the cell where the object was stored.
610 class Special:
611 def __del__(self):
612 nestedcell_get()
614 def f():
615 global nestedcell_get
616 def nestedcell_get():
617 return c
619 c = (Special(),)
620 c = 2
622 f() # used to crash the interpreter...
624 def testGlobalInParallelNestedFunctions(self):
625 # A symbol table bug leaked the global statement from one
626 # function to other nested functions in the same block.
627 # This test verifies that a global statement in the first
628 # function does not affect the second function.
629 CODE = """def f():
630 y = 1
631 def g():
632 global y
633 return y
634 def h():
635 return y + 1
636 return g, h
638 y = 9
639 g, h = f()
640 result9 = g()
641 result2 = h()
643 local_ns = {}
644 global_ns = {}
645 exec CODE in local_ns, global_ns
646 self.assertEqual(2, global_ns["result2"])
647 self.assertEqual(9, global_ns["result9"])
650 def test_main():
651 run_unittest(ScopeTests)
653 if __name__ == '__main__':
654 test_main()