annotate Lib/test/test_scope.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents e74f4d6c0c81
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
1 import unittest
83772
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
2 import weakref
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
3
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
4 from test.support import check_syntax_error, cpython_only, run_unittest
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
5
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
6
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
7 class ScopeTests(unittest.TestCase):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
9 def testSimpleNesting(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
11 def make_adder(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
12 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
13 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
14 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
16 inc = make_adder(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
17 plus10 = make_adder(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
19 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
20 self.assertEqual(plus10(-2), 8)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
21
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
22 def testExtraNesting(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
24 def make_adder2(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
25 def extra(): # check freevars passing through non-use scopes
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
26 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
27 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
28 return adder
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
29 return extra()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
31 inc = make_adder2(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
32 plus10 = make_adder2(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
34 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
35 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
37 def testSimpleAndRebinding(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
38
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
39 def make_adder3(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
40 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
41 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
42 x = x + 1 # check tracking of assignment to x in defining scope
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
43 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
44
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
45 inc = make_adder3(0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
46 plus10 = make_adder3(9)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
48 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
49 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
50
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
51 def testNestingGlobalNoFree(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
52
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
53 def make_adder4(): # XXX add exta level of indirection
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
54 def nest():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
55 def nest():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
56 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
57 return global_x + y # check that plain old globals work
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
58 return adder
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
59 return nest()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
60 return nest()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
62 global_x = 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
63 adder = make_adder4()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
64 self.assertEqual(adder(1), 2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
65
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
66 global_x = 10
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
67 self.assertEqual(adder(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
69 def testNestingThroughClass(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
70
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
71 def make_adder5(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
72 class Adder:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
73 def __call__(self, y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
74 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
75 return Adder()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
76
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
77 inc = make_adder5(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
78 plus10 = make_adder5(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
79
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
80 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
81 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
83 def testNestingPlusFreeRefToGlobal(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
84
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
85 def make_adder6(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
86 global global_nest_x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
87 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
88 return global_nest_x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
89 global_nest_x = x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
90 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
92 inc = make_adder6(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
93 plus10 = make_adder6(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
94
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
95 self.assertEqual(inc(1), 11) # there's only one global
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
96 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
98 def testNearestEnclosingScope(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
99
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
100 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
101 def g(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
102 x = 42 # check that this masks binding in f()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
103 def h(z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
104 return x + z
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
105 return h
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
106 return g(2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
107
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
108 test_func = f(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
109 self.assertEqual(test_func(5), 47)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
110
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
111 def testMixedFreevarsAndCellvars(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
112
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
113 def identity(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
114 return x
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
115
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
116 def f(x, y, z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
117 def g(a, b, c):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
118 a = a + x # 3
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
119 def h():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
120 # z * (4 + 9)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
121 # 3 * 13
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
122 return identity(z * (b + y))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
123 y = c + z # 9
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
124 return h
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
125 return g
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
126
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
127 g = f(1, 2, 3)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
128 h = g(2, 4, 6)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
129 self.assertEqual(h(), 39)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
130
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
131 def testFreeVarInMethod(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
132
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
133 def test():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
134 method_and_var = "var"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
135 class Test:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
136 def method_and_var(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
137 return "method"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
138 def test(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
139 return method_and_var
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
140 def actual_global(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
141 return str("global")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
142 def str(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
143 return str(self)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
144 return Test()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
145
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
146 t = test()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
147 self.assertEqual(t.test(), "var")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
148 self.assertEqual(t.method_and_var(), "method")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
149 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
150
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
151 method_and_var = "var"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
152 class Test:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
153 # this class is not nested, so the rules are different
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
154 def method_and_var(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
155 return "method"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
156 def test(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
157 return method_and_var
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
158 def actual_global(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
159 return str("global")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
160 def str(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
161 return str(self)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
162
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
163 t = Test()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
164 self.assertEqual(t.test(), "var")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
165 self.assertEqual(t.method_and_var(), "method")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
166 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
167
43996
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
168 def testCellIsKwonlyArg(self):
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
169 # Issue 1409: Initialisation of a cell value,
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
170 # when it comes from a keyword-only parameter
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
171 def foo(*, a=17):
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
172 def bar():
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
173 return a + 5
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
174 return bar() + 3
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
175
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
176 self.assertEqual(foo(a=42), 50)
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
177 self.assertEqual(foo(), 25)
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
178
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
179 def testRecursion(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
180
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
181 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
182 def fact(n):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
183 if n == 0:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
184 return 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
185 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
186 return n * fact(n - 1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
187 if x >= 0:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
188 return fact(x)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
189 else:
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42455
diff changeset
190 raise ValueError("x must be >= 0")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
191
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
192 self.assertEqual(f(6), 720)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
193
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
194
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
195 def testUnoptimizedNamespaces(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
196
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
197 check_syntax_error(self, """if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
198 def unoptimized_clash1(strip):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
199 def f(s):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
200 from sys import *
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
201 return getrefcount(s) # ambiguity: free or local
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
202 return f
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
203 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
204
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
205 check_syntax_error(self, """if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
206 def unoptimized_clash2():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
207 from sys import *
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
208 def f(s):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
209 return getrefcount(s) # ambiguity: global or local
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
210 return f
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
211 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
212
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
213 check_syntax_error(self, """if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
214 def unoptimized_clash2():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
215 from sys import *
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
216 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
217 def f(s):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
218 return getrefcount(s) # ambiguity: global or local
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
219 return f
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
220 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
221
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
222 check_syntax_error(self, """if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
223 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
224 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
225 from sys import *
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
226 return getrefcount # global or local?
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
227 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
228
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
229 def testLambdas(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
230
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
231 f1 = lambda x: lambda y: x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
232 inc = f1(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
233 plus10 = f1(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
234 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
235 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
236
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
237 f2 = lambda x: (lambda : lambda y: x + y)()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
238 inc = f2(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
239 plus10 = f2(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
240 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
241 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
242
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
243 f3 = lambda x: lambda y: global_x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
244 global_x = 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
245 inc = f3(None)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
246 self.assertEqual(inc(2), 3)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
247
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
248 f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
249 g = f8(1, 2, 3)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
250 h = g(2, 4, 6)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
251 self.assertEqual(h(), 18)
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
252
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
253 def testUnboundLocal(self):
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
254
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
255 def errorInOuter():
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
256 print(y)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
257 def inner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
258 return y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
259 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
260
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
261 def errorInInner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
262 def inner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
263 return y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
264 inner()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
265 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
266
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
267 self.assertRaises(UnboundLocalError, errorInOuter)
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
268 self.assertRaises(NameError, errorInInner)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
269
64620
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
270 def testUnboundLocal_AfterDel(self):
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
271 # #4617: It is now legal to delete a cell variable.
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
272 # The following functions must obviously compile,
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
273 # and give the correct error when accessing the deleted name.
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
274 def errorInOuter():
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
275 y = 1
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
276 del y
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
277 print(y)
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
278 def inner():
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
279 return y
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
280
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
281 def errorInInner():
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
282 def inner():
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
283 return y
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
284 y = 1
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
285 del y
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
286 inner()
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
287
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
288 self.assertRaises(UnboundLocalError, errorInOuter)
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
289 self.assertRaises(NameError, errorInInner)
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
290
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 64614
diff changeset
291 def testUnboundLocal_AugAssign(self):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
292 # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
293 exec("""if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
294 global_x = 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
295 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
296 global_x += 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
297 try:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
298 f()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
299 except UnboundLocalError:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
300 pass
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
301 else:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
302 fail('scope of global_x not correctly determined')
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
303 """, {'fail': self.fail})
39037
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
304
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
305 def testComplexDefinitions(self):
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
306
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
307 def makeReturner(*lst):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
308 def returner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
309 return lst
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
310 return returner
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
311
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
312 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
313
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
314 def makeReturner2(**kwargs):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
315 def returner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
316 return kwargs
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
317 return returner
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
318
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
319 self.assertEqual(makeReturner2(a=11)()['a'], 11)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
320
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
321 def testScopeOfGlobalStmt(self):
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
322 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
323
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
324 exec("""if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
325 # I
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
326 x = 7
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
327 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
328 x = 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
329 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
330 global x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
331 def i():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
332 def h():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
333 return x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
334 return h()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
335 return i()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
336 return g()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
337 self.assertEqual(f(), 7)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
338 self.assertEqual(x, 7)
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
339
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
340 # II
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
341 x = 7
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
342 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
343 x = 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
344 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
345 x = 2
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
346 def i():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
347 def h():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
348 return x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
349 return h()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
350 return i()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
351 return g()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
352 self.assertEqual(f(), 2)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
353 self.assertEqual(x, 7)
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
354
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
355 # III
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
356 x = 7
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
357 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
358 x = 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
359 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
360 global x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
361 x = 2
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
362 def i():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
363 def h():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
364 return x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
365 return h()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
366 return i()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
367 return g()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
368 self.assertEqual(f(), 2)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
369 self.assertEqual(x, 2)
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
370
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
371 # IV
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
372 x = 7
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
373 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
374 x = 3
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
375 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
376 global x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
377 x = 2
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
378 def i():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
379 def h():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
380 return x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
381 return h()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
382 return i()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
383 return g()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
384 self.assertEqual(f(), 2)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
385 self.assertEqual(x, 2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
386
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
387 # XXX what about global statements in class blocks?
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
388 # do they affect methods?
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
389
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
390 x = 12
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
391 class Global:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
392 global x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
393 x = 13
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
394 def set(self, val):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
395 x = val
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
396 def get(self):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
397 return x
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
398
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
399 g = Global()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
400 self.assertEqual(g.get(), 13)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
401 g.set(15)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
402 self.assertEqual(g.get(), 13)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
403 """)
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
404
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
405 def testLeaks(self):
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
406
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
407 class Foo:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
408 count = 0
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
409
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
410 def __init__(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
411 Foo.count += 1
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
412
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
413 def __del__(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
414 Foo.count -= 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
415
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
416 def f1():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
417 x = Foo()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
418 def f2():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
419 return x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
420 f2()
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
421
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
422 for i in range(100):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
423 f1()
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
424
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
425 self.assertEqual(Foo.count, 0)
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
426
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
427 def testClassAndGlobal(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
428
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
429 exec("""if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
430 def test(x):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
431 class Foo:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
432 global x
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
433 def __call__(self, y):
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
434 return x + y
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
435 return Foo()
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
436
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
437 x = 0
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
438 self.assertEqual(test(6)(2), 8)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
439 x = -1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
440 self.assertEqual(test(3)(2), 5)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
441
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
442 looked_up_by_load_name = False
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
443 class X:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
444 # Implicit globals inside classes are be looked up by LOAD_NAME, not
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
445 # LOAD_GLOBAL.
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
446 locals()['looked_up_by_load_name'] = True
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
447 passed = looked_up_by_load_name
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
448
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
449 self.assertTrue(X.passed)
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
450 """)
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
451
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
452 def testLocalsFunction(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
453
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
454 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
455 def g(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
456 def h(z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
457 return y + z
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
458 w = x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
459 y += 3
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
460 return locals()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
461 return g
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
462
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
463 d = f(2)(4)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
464 self.assertIn('h', d)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
465 del d['h']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
466 self.assertEqual(d, {'x': 2, 'y': 7, 'w': 6})
17494
a34293ced63b Change error message raised when free variable is not yet bound. It
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17105
diff changeset
467
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
468 def testLocalsClass(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
469 # This test verifies that calling locals() does not pollute
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
470 # the local namespace of the class with free variables. Old
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
471 # versions of Python had a bug, where a free variable being
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
472 # passed through a class namespace would be inserted into
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
473 # locals() by locals() or exec or a trace function.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
474 #
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
475 # The real bug lies in frame code that copies variables
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
476 # between fast locals and the locals dict, e.g. when executing
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
477 # a trace function.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
478
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
479 def f(x):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
480 class C:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
481 x = 12
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
482 def m(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
483 return x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
484 locals()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
485 return C
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
486
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
487 self.assertEqual(f(1).x, 12)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
488
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
489 def f(x):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
490 class C:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
491 y = x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
492 def m(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
493 return x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
494 z = list(locals())
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
495 return C
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
496
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
497 varnames = f(1).z
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
498 self.assertNotIn("x", varnames)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
499 self.assertIn("y", varnames)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
500
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
501 @cpython_only
48563
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
502 def testLocalsClass_WithTrace(self):
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
503 # Issue23728: after the trace function returns, the locals()
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
504 # dictionary is used to update all variables, this used to
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
505 # include free variables. But in class statements, free
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
506 # variables are not inserted...
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
507 import sys
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
508 self.addCleanup(sys.settrace, sys.gettrace())
48563
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
509 sys.settrace(lambda a,b,c:None)
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
510 x = 12
48563
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
511
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
512 class C:
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
513 def f(self):
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
514 return x
48563
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
515
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
516 self.assertEqual(x, 12) # Used to raise UnboundLocalError
48563
b51cd5d3795c Merged revisions 65012,65035,65037-65040,65048,65057,65077,65091-65095,65097-65099,65127-65128,65131,65133-65136,65139,65149-65151,65155,65158-65159,65176-65178,65183-65184,65187-65190,65192,65194 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
517
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
518 def testBoundAndFree(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
519 # var is bound and free in class
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17494
diff changeset
520
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
521 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
522 class C:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
523 def m(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
524 return x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
525 a = x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
526 return C
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17494
diff changeset
527
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
528 inst = f(3)()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
529 self.assertEqual(inst.a, inst.m())
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
530
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
531 @cpython_only
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
532 def testInteractionWithTraceFunc(self):
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
533
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
534 import sys
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
535 def tracer(a,b,c):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
536 return tracer
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
537
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
538 def adaptgetter(name, klass, getter):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
539 kind, des = getter
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
540 if kind == 1: # AV happens when stepping from this line to next
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
541 if des == "":
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
542 des = "_%s__%s" % (klass.__name__, name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
543 return lambda obj: getattr(obj, des)
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
544
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
545 class TestClass:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
546 pass
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
547
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
548 self.addCleanup(sys.settrace, sys.gettrace())
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
549 sys.settrace(tracer)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
550 adaptgetter("foo", TestClass, (1, ""))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
551 sys.settrace(None)
18886
c7bb9563b149 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18524
diff changeset
552
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
553 self.assertRaises(TypeError, sys.settrace)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
554
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
555 def testEvalExecFreeVars(self):
22119
0859e764509e SF #506611, fix sys.setprofile(), sys.settrace() core dumps
Neal Norwitz <nnorwitz@gmail.com>
parents: 21431
diff changeset
556
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
557 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
558 return lambda: x + 1
18886
c7bb9563b149 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18524
diff changeset
559
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
560 g = f(3)
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
561 self.assertRaises(TypeError, eval, g.__code__)
18886
c7bb9563b149 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18524
diff changeset
562
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
563 try:
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
564 exec(g.__code__, {})
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
565 except TypeError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
566 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
567 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
568 self.fail("exec should have failed, because code contained free vars")
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
569
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
570 def testListCompLocalVars(self):
21395
560222e9d471 Add test for SF bug [ #492403 ] exec() segfaults on closure's func_code
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20518
diff changeset
571
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
572 try:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
573 print(bad)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
574 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
575 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
576 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
577 print("bad should not be defined")
20518
23dc7bb0be4f Add test for local assigned to only in a nested list comp
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20089
diff changeset
578
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
579 def x():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
580 [bad for s in 'a b' for bad in s.split()]
20518
23dc7bb0be4f Add test for local assigned to only in a nested list comp
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20089
diff changeset
581
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
582 x()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
583 try:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
584 print(bad)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
585 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
586 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
587
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
588 def testEvalFreeVars(self):
20518
23dc7bb0be4f Add test for local assigned to only in a nested list comp
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20089
diff changeset
589
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
590 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
591 def g():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
592 x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
593 eval("x + 1")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
594 return g
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
595
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
596 f(4)()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
597
45377
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
598 def testFreeingCell(self):
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
599 # Test what happens when a finalizer accesses
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
600 # the cell where the object was stored.
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
601 class Special:
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
602 def __del__(self):
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
603 nestedcell_get()
1872bd6cf4b4 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60873 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43996
diff changeset
604
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
605 def testNonLocalFunction(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
606
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
607 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
608 def inc():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
609 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
610 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
611 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
612 def dec():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
613 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
614 x -= 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
615 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
616 return inc, dec
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
617
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
618 inc, dec = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
619 self.assertEqual(inc(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
620 self.assertEqual(inc(), 2)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
621 self.assertEqual(dec(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
622 self.assertEqual(dec(), 0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
623
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
624 def testNonLocalMethod(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
625 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
626 class c:
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
627 def inc(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
628 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
629 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
630 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
631 def dec(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
632 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
633 x -= 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
634 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
635 return c()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
636 c = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
637 self.assertEqual(c.inc(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
638 self.assertEqual(c.inc(), 2)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
639 self.assertEqual(c.dec(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
640 self.assertEqual(c.dec(), 0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
641
52536
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
642 def testGlobalInParallelNestedFunctions(self):
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
643 # A symbol table bug leaked the global statement from one
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
644 # function to other nested functions in the same block.
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
645 # This test verifies that a global statement in the first
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
646 # function does not affect the second function.
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
647 local_ns = {}
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
648 global_ns = {}
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
649 exec("""if 1:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
650 def f():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
651 y = 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
652 def g():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
653 global y
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
654 return y
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
655 def h():
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
656 return y + 1
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
657 return g, h
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
658 y = 9
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
659 g, h = f()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
660 result9 = g()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
661 result2 = h()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
662 """, local_ns, global_ns)
52536
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
663 self.assertEqual(2, global_ns["result2"])
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
664 self.assertEqual(9, global_ns["result9"])
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
665
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
666 def testNonLocalClass(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
667
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
668 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
669 class c:
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
670 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
671 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
672 def get(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
673 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
674 return c()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
675
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
676 c = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
677 self.assertEqual(c.get(), 1)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
678 self.assertNotIn("x", c.__class__.__dict__)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
679
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
680
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
681 def testNonLocalGenerator(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
682
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
683 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
684 def g(y):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
685 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
686 for i in range(y):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
687 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
688 yield x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
689 return g
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
690
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
691 g = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
692 self.assertEqual(list(g(5)), [1, 2, 3, 4, 5])
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
693
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
694 def testNestedNonLocal(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
695
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
696 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
697 def g():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
698 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
699 x -= 2
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
700 def h():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
701 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
702 x += 4
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
703 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
704 return h
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
705 return g
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
706
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
707 g = f(1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
708 h = g()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
709 self.assertEqual(h(), 3)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
710
65393
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
711 def testTopIsNotSignificant(self):
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
712 # See #9997.
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
713 def top(a):
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
714 pass
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
715 def b():
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
716 global a
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
717
83572
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
718 def testClassNamespaceOverridesClosure(self):
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
719 # See #17853.
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
720 x = 42
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
721 class X:
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
722 locals()["x"] = 43
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
723 y = x
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
724 self.assertEqual(X.y, 43)
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
725 class X:
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
726 locals()["x"] = 43
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
727 del x
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
728 self.assertFalse(hasattr(X, "x"))
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
729 self.assertEqual(x, 42)
cf65c7a75f55 check local class namespace before reaching for cells (closes #17853)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
730
83772
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
731 @cpython_only
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
732 def testCellLeak(self):
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
733 # Issue 17927.
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
734 #
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
735 # The issue was that if self was part of a cycle involving the
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
736 # frame of a method call, *and* the method contained a nested
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
737 # function referencing self, thereby forcing 'self' into a
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
738 # cell, setting self to None would not be enough to break the
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
739 # frame -- the frame had another reference to the instance,
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
740 # which could not be cleared by the code running in the frame
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
741 # (though it will be cleared when the frame is collected).
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
742 # Without the lambda, setting self to None is enough to break
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
743 # the cycle.
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
744 class Tester:
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
745 def dig(self):
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
746 if 0:
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
747 lambda: self
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
748 try:
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
749 1/0
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
750 except Exception as exc:
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
751 self.exc = exc
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
752 self = None # Break the cycle
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
753 tester = Tester()
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
754 tester.dig()
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
755 ref = weakref.ref(tester)
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
756 del tester
2b4b289c1abb when arguments are cells clear the locals slot (backport of #17927)
Benjamin Peterson <benjamin@python.org>
parents: 68258
diff changeset
757 self.assertIsNone(ref())
65393
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
758
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
759
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
760 def test_main():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
761 run_unittest(ScopeTests)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
762
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
763 if __name__ == '__main__':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
764 test_main()