annotate Lib/test/test_scope.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 3c9232a684ae
children b39270c6b8d1
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
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45377
diff changeset
2 from test.support import check_syntax_error, run_unittest
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
4
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
5 class ScopeTests(unittest.TestCase):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
6
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
7 def testSimpleNesting(self):
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 make_adder(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
10 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
11 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
12 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
14 inc = make_adder(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
15 plus10 = make_adder(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
17 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
18 self.assertEqual(plus10(-2), 8)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
19
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
20 def testExtraNesting(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
22 def make_adder2(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
23 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
24 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
25 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
26 return adder
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
27 return extra()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
28
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
29 inc = make_adder2(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
30 plus10 = make_adder2(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
32 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
33 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
35 def testSimpleAndRebinding(self):
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 make_adder3(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
38 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
39 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
40 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
41 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
43 inc = make_adder3(0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
44 plus10 = make_adder3(9)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
45
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
46 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
47 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
49 def testNestingGlobalNoFree(self):
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 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
52 def nest():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
53 def nest():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
54 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
55 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
56 return adder
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
57 return nest()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
58 return nest()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
60 global_x = 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
61 adder = make_adder4()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
62 self.assertEqual(adder(1), 2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
64 global_x = 10
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
65 self.assertEqual(adder(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
67 def testNestingThroughClass(self):
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 make_adder5(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
70 class Adder:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
71 def __call__(self, y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
72 return x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
73 return Adder()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
75 inc = make_adder5(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
76 plus10 = make_adder5(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
78 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
79 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
81 def testNestingPlusFreeRefToGlobal(self):
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 make_adder6(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
84 global global_nest_x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
85 def adder(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
86 return global_nest_x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
87 global_nest_x = x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
88 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
89
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
90 inc = make_adder6(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
91 plus10 = make_adder6(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
93 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
94 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
95
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
96 def testNearestEnclosingScope(self):
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 f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
99 def g(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
100 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
101 def h(z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
102 return x + z
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
103 return h
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
104 return g(2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
105
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
106 test_func = f(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
107 self.assertEqual(test_func(5), 47)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
108
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
109 def testMixedFreevarsAndCellvars(self):
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 identity(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
112 return x
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
113
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
114 def f(x, y, z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
115 def g(a, b, c):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
116 a = a + x # 3
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
117 def h():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
118 # z * (4 + 9)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
119 # 3 * 13
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
120 return identity(z * (b + y))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
121 y = c + z # 9
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
122 return h
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
123 return g
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
124
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
125 g = f(1, 2, 3)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
126 h = g(2, 4, 6)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
127 self.assertEqual(h(), 39)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
128
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
129 def testFreeVarInMethod(self):
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 test():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
132 method_and_var = "var"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
133 class Test:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
134 def method_and_var(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
135 return "method"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
136 def test(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
137 return method_and_var
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
138 def actual_global(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
139 return str("global")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
140 def str(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
141 return str(self)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
142 return Test()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
143
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
144 t = test()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
145 self.assertEqual(t.test(), "var")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
146 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
147 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
148
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
149 method_and_var = "var"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
150 class Test:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
151 # 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
152 def method_and_var(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
153 return "method"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
154 def test(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
155 return method_and_var
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
156 def actual_global(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
157 return str("global")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
158 def str(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
159 return str(self)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
160
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
161 t = Test()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
162 self.assertEqual(t.test(), "var")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
163 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
164 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
165
43996
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
166 def testCellIsKwonlyArg(self):
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
167 # 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
168 # 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
169 def foo(*, a=17):
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
170 def bar():
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
171 return a + 5
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
172 return bar() + 3
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
173
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
174 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
175 self.assertEqual(foo(), 25)
b86e6bf7438f fix #1409: cell variables were not initialized,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43014
diff changeset
176
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
177 def testRecursion(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
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 f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
180 def fact(n):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
181 if n == 0:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
182 return 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
183 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
184 return n * fact(n - 1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
185 if x >= 0:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
186 return fact(x)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
187 else:
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42455
diff changeset
188 raise ValueError("x must be >= 0")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
189
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
190 self.assertEqual(f(6), 720)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
191
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
192
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
193 def testUnoptimizedNamespaces(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
194
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
195 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
196 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
197 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
198 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
199 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
200 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
201 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
202
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
203 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
204 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
205 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
206 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
207 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
208 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
209 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
210
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
219
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
220 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
221 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
222 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
223 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
224 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
225 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
226
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
227 def testLambdas(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
228
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
229 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
230 inc = f1(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
231 plus10 = f1(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
232 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
233 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
234
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
235 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
236 inc = f2(1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
237 plus10 = f2(10)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
238 self.assertEqual(inc(1), 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
239 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
240
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
241 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
242 global_x = 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
243 inc = f3(None)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
244 self.assertEqual(inc(2), 3)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
245
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
246 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
247 g = f8(1, 2, 3)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
248 h = g(2, 4, 6)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
249 self.assertEqual(h(), 18)
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
250
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
251 def testUnboundLocal(self):
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 errorInOuter():
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
254 print(y)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
255 def inner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
256 return y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
257 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
258
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
259 def errorInInner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
260 def inner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
261 return y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
262 inner()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
263 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
264
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
265 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
266 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
267
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
268 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
269 # #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
270 # 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
271 # 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
272 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
273 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
274 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
275 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
276 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
277 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
278
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 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
280 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
281 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
282 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
283 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
284 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
285
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 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
287 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
288
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 def testUnboundLocal_AugAssign(self):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
290 # 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
291 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
292 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
293 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
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 try:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
296 f()
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
297 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
298 pass
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
299 else:
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
300 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
301 """, {'fail': self.fail})
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 35773
diff changeset
302
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
303 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
304
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
305 def makeReturner(*lst):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
306 def returner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
307 return lst
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
308 return returner
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
309
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
310 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
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 def makeReturner2(**kwargs):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
313 def returner():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
314 return kwargs
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
315 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
316
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
317 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
318
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
319 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
320 # 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
321
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 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
323 # I
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
324 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
325 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
326 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
327 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
328 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
329 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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
338 # II
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
339 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
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
353 # III
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
354 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
355 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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363 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
364 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
365 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
366 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
367 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
368
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
369 # IV
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
370 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
371 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
372 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
373 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
374 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
375 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
376 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
377 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
378 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
379 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
380 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
381 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
382 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
383 self.assertEqual(x, 2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
384
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
385 # 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
386 # do they affect methods?
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
387
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
388 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
389 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
390 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
391 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
392 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
393 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
394 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
395 return x
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
396
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
397 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
398 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
399 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
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 """)
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
402
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
403 def testLeaks(self):
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
404
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
405 class Foo:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
406 count = 0
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
407
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
408 def __init__(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
409 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
410
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
411 def __del__(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
412 Foo.count -= 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
413
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
414 def f1():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
415 x = Foo()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
416 def f2():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
417 return x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
418 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
419
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
420 for i in range(100):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
421 f1()
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
422
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
423 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
424
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
425 def testClassAndGlobal(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
426
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
427 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
428 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
429 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
430 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
431 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
432 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
433 return Foo()
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
434
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
435 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
436 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
437 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
438 self.assertEqual(test(3)(2), 5)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
439
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
440 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
441 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
442 # 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
443 # 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
444 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
445 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
446
64614
5c4be63cab4a Use the "if 1:" prefix so that quoted code appears nicely
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 63114
diff changeset
447 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
448 """)
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
449
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
450 def testLocalsFunction(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
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 f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
453 def g(y):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
454 def h(z):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
455 return y + z
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
456 w = x + y
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
457 y += 3
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
458 return locals()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
459 return g
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
460
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
461 d = f(2)(4)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
462 self.assertIn('h', d)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
463 del d['h']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
464 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
465
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
466 def testLocalsClass(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
467 # 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
468 # 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
469 # 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
470 # 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
471 # 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
472 #
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
473 # 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
474 # 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
475 # a trace function.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
476
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
477 def f(x):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
478 class C:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
479 x = 12
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
480 def m(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
481 return x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
482 locals()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
483 return C
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
484
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
485 self.assertEqual(f(1).x, 12)
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 def f(x):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
488 class C:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
489 y = x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
490 def m(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
491 return x
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
492 z = list(locals())
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
493 return C
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
494
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
495 varnames = f(1).z
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
496 self.assertNotIn("x", varnames)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
497 self.assertIn("y", varnames)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
498
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
499 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
500 # 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
501 # 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
502 # 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
503 # 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
504 import sys
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 sys.settrace(lambda a,b,c:None)
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 try:
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 x = 12
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
508
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 class C:
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
510 def f(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
511 return x
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
512
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65393
diff changeset
513 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
514 finally:
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 sys.settrace(None)
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
516
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
517 def testBoundAndFree(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
518 # 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
519
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
520 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
521 class C:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
522 def m(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
523 return x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
524 a = x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
525 return C
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17494
diff changeset
526
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
527 inst = f(3)()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
528 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
529
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
530 def testInteractionWithTraceFunc(self):
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
531
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
532 import sys
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
533 def tracer(a,b,c):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
534 return tracer
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
535
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
536 def adaptgetter(name, klass, getter):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
537 kind, des = getter
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
538 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
539 if des == "":
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
540 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
541 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
542
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
543 class TestClass:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
544 pass
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
545
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
546 sys.settrace(tracer)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
547 adaptgetter("foo", TestClass, (1, ""))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
548 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
549
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
550 self.assertRaises(TypeError, sys.settrace)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
551
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
552 def testEvalExecFreeVars(self):
22119
0859e764509e SF #506611, fix sys.setprofile(), sys.settrace() core dumps
Neal Norwitz <nnorwitz@gmail.com>
parents: 21431
diff changeset
553
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
554 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
555 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
556
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
557 g = f(3)
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
558 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
559
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
560 try:
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
561 exec(g.__code__, {})
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
562 except TypeError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
563 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
564 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
565 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
566
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
567 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
568
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
569 try:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
570 print(bad)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
571 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
572 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
573 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
574 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
575
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
576 def x():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
577 [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
578
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
579 x()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
580 try:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
581 print(bad)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
582 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
583 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
584
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
585 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
586
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
587 def f(x):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
588 def g():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
589 x
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
590 eval("x + 1")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
591 return g
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
592
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
593 f(4)()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
594
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
595 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
596 # 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
597 # 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
598 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
599 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
600 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
601
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
602 def testNonLocalFunction(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
603
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
604 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
605 def inc():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
606 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
607 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
608 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
609 def dec():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
610 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
611 x -= 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
612 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
613 return inc, dec
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
614
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
615 inc, dec = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
616 self.assertEqual(inc(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
617 self.assertEqual(inc(), 2)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
618 self.assertEqual(dec(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
619 self.assertEqual(dec(), 0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
620
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
621 def testNonLocalMethod(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
622 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
623 class c:
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
624 def inc(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
625 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
626 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
627 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
628 def dec(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
629 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
630 x -= 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
631 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
632 return c()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
633 c = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
634 self.assertEqual(c.inc(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
635 self.assertEqual(c.inc(), 2)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
636 self.assertEqual(c.dec(), 1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
637 self.assertEqual(c.dec(), 0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
638
52536
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
639 def testGlobalInParallelNestedFunctions(self):
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
640 # 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
641 # 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
642 # 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
643 # function does not affect the second function.
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
644 local_ns = {}
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
645 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
646 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
647 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
648 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
649 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
650 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
651 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
652 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
653 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
654 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
655 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
656 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
657 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
658 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
659 """, local_ns, global_ns)
52536
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
660 self.assertEqual(2, global_ns["result2"])
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
661 self.assertEqual(9, global_ns["result9"])
d8ceb26c66c4 Merged revisions 70823 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48563
diff changeset
662
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
663 def testNonLocalClass(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
664
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
665 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
666 class c:
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
667 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
668 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
669 def get(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
670 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
671 return c()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
672
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
673 c = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
674 self.assertEqual(c.get(), 1)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
675 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
676
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
677
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
678 def testNonLocalGenerator(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
679
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
680 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
681 def g(y):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
682 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
683 for i in range(y):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
684 x += 1
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
685 yield x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
686 return g
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
687
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
688 g = f(0)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
689 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
690
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
691 def testNestedNonLocal(self):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
692
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
693 def f(x):
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
694 def g():
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
695 nonlocal x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
696 x -= 2
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
697 def h():
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 += 4
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
700 return x
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
701 return h
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
702 return g
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
703
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
704 g = f(1)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
705 h = g()
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
706 self.assertEqual(h(), 3)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40910
diff changeset
707
65393
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
708 def testTopIsNotSignificant(self):
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
709 # See #9997.
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
710 def top(a):
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
711 pass
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
712 def b():
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
713 global a
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
714
3618d9d7902d Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55609
diff changeset
715
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
716
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
717 def test_main():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
718 run_unittest(ScopeTests)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
719
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
720 if __name__ == '__main__':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
721 test_main()