annotate Lib/test/test_scope.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 6910792fcd4b
children 3efb4581790a 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
1 import unittest
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
2 from test.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 import warnings
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
5 warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<test string>")
21431
fd9d7ae3f94a Don't call resetwarnings(). Be more restrictive in what we filter out
Guido van Rossum <guido@python.org>
parents: 21402
diff changeset
6 warnings.filterwarnings("ignore", r"import \*", SyntaxWarning, "<string>")
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
7
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
8 class ScopeTests(unittest.TestCase):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
10 def testSimpleNesting(self):
40284
ff678669aa6e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40217
diff changeset
11
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
12 def make_adder(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
13 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
14 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
15 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
17 inc = make_adder(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
18 plus10 = make_adder(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
20 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
21 self.assertEqual(plus10(-2), 8)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
22
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
23 def testExtraNesting(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
25 def make_adder2(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
26 def extra(): # check freevars passing through non-use scopes
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
27 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
28 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
29 return adder
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
30 return extra()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
32 inc = make_adder2(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
33 plus10 = make_adder2(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
35 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
36 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
38 def testSimpleAndRebinding(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
40 def make_adder3(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
41 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
42 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
43 x = x + 1 # check tracking of assignment to x in defining scope
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
44 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
45
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
46 inc = make_adder3(0)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
47 plus10 = make_adder3(9)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
49 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
50 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
51
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
52 def testNestingGlobalNoFree(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
54 def make_adder4(): # XXX add exta level of indirection
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
55 def nest():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
56 def nest():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
57 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
58 return global_x + y # check that plain old globals work
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
59 return adder
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
60 return nest()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
61 return nest()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
62
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
63 global_x = 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
64 adder = make_adder4()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
65 self.assertEqual(adder(1), 2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
67 global_x = 10
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
68 self.assertEqual(adder(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
70 def testNestingThroughClass(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
72 def make_adder5(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
73 class Adder:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
74 def __call__(self, y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
75 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
76 return Adder()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
78 inc = make_adder5(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
79 plus10 = make_adder5(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
81 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
82 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
83
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
84 def testNestingPlusFreeRefToGlobal(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
86 def make_adder6(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
87 global global_nest_x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
88 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
89 return global_nest_x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
90 global_nest_x = x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
91 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
93 inc = make_adder6(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
94 plus10 = make_adder6(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
95
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
96 self.assertEqual(inc(1), 11) # there's only one global
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
97 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
98
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
99 def testNearestEnclosingScope(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
100
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
101 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
102 def g(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
103 x = 42 # check that this masks binding in f()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
104 def h(z):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
105 return x + z
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
106 return h
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
107 return g(2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
108
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
109 test_func = f(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
110 self.assertEqual(test_func(5), 47)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
111
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
112 def testMixedFreevarsAndCellvars(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
113
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
114 def identity(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
115 return x
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
116
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
117 def f(x, y, z):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
118 def g(a, b, c):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
119 a = a + x # 3
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
120 def h():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
121 # z * (4 + 9)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
122 # 3 * 13
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
123 return identity(z * (b + y))
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
124 y = c + z # 9
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
125 return h
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
126 return g
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
127
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
128 g = f(1, 2, 3)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
129 h = g(2, 4, 6)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
130 self.assertEqual(h(), 39)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
131
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
132 def testFreeVarInMethod(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
134 def test():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
135 method_and_var = "var"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
136 class Test:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
137 def method_and_var(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
138 return "method"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
139 def test(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
140 return method_and_var
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
141 def actual_global(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
142 return str("global")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
143 def str(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
144 return str(self)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
145 return Test()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
146
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
147 t = test()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
148 self.assertEqual(t.test(), "var")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
149 self.assertEqual(t.method_and_var(), "method")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
150 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
151
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
152 method_and_var = "var"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
153 class Test:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
154 # this class is not nested, so the rules are different
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
155 def method_and_var(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
156 return "method"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
157 def test(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
158 return method_and_var
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
159 def actual_global(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
160 return str("global")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
161 def str(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
162 return str(self)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
163
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
164 t = Test()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
165 self.assertEqual(t.test(), "var")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
166 self.assertEqual(t.method_and_var(), "method")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
167 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
168
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
169 def testRecursion(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
170
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
171 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
172 def fact(n):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
173 if n == 0:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
174 return 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
175 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
176 return n * fact(n - 1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
177 if x >= 0:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
178 return fact(x)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
179 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
180 raise ValueError, "x must be >= 0"
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
181
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
182 self.assertEqual(f(6), 720)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
183
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
184
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
185 def testUnoptimizedNamespaces(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
186
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
187 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
188 def unoptimized_clash1(strip):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
189 def f(s):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
190 from string import *
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
191 return strip(s) # ambiguity: free or local
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
192 return f
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
193 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
194
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
195 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
196 def unoptimized_clash2():
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
197 from string import *
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
198 def f(s):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
199 return strip(s) # ambiguity: global or local
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
200 return f
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
201 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
202
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
203 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
204 def unoptimized_clash2():
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
205 from string import *
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
206 def g():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
207 def f(s):
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
208 return strip(s) # ambiguity: global or local
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
209 return f
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
210 """)
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
211
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
212 # XXX could allow this for exec with const argument, but what's the point
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
213 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
214 def error(y):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
215 exec "a = 1"
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
216 def f(x):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
217 return x + y
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
218 return f
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
219 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
220
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
221 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
222 def f(x):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
223 def g():
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
224 return x
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
225 del x # can't del name
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
226 """)
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
227
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
228 check_syntax_error(self, """\
16803
44458fe947dd add from __future__ import nested_scopes to strings passed to compile
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
229 def f():
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
230 def g():
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
231 from string import *
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
232 return strip # global or local?
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
233 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
234
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
235 # and verify a few cases that should work
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
236
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
237 exec """
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
238 def noproblem1():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
239 from string import *
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
240 f = lambda x:x
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
241
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
242 def noproblem2():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
243 from string import *
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
244 def f(x):
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
245 return x + 1
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
246
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
247 def noproblem3():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
248 from string import *
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
249 def f(x):
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
250 global y
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
251 y = x
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
252 """
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
253
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
254 def testLambdas(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
255
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
256 f1 = lambda x: lambda y: x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
257 inc = f1(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
258 plus10 = f1(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
259 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
260 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
261
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
262 f2 = lambda x: (lambda : lambda y: x + y)()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
263 inc = f2(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
264 plus10 = f2(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
265 self.assertEqual(inc(1), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
266 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
267
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
268 f3 = lambda x: lambda y: global_x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
269 global_x = 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
270 inc = f3(None)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
271 self.assertEqual(inc(2), 3)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
272
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
273 f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
274 g = f8(1, 2, 3)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
275 h = g(2, 4, 6)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
276 self.assertEqual(h(), 18)
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
277
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
278 def testUnboundLocal(self):
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
279
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
280 def errorInOuter():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
281 print y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
282 def inner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
283 return y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
284 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
285
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
286 def errorInInner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
287 def inner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
288 return y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
289 inner()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
290 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
291
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
292 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
293 errorInOuter()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
294 except UnboundLocalError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
295 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
296 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
297 self.fail()
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
298
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
299 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
300 errorInInner()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
301 except NameError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
302 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
303 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
304 self.fail()
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
305
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
306 # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
307 exec """
39037
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
308 global_x = 1
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
309 def f():
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
310 global_x += 1
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
311 try:
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
312 f()
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
313 except UnboundLocalError:
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
314 pass
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
315 else:
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
316 fail('scope of global_x not correctly determined')
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
317 """ in {'fail': self.fail}
39037
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
318
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
319 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
320
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
321 def makeReturner(*lst):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
322 def returner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
323 return lst
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
324 return returner
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
325
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
326 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
327
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
328 def makeReturner2(**kwargs):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
329 def returner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
330 return kwargs
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
331 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
332
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
333 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
334
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
335 def makeAddPair((a, b)):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
336 def addPair((c, d)):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
337 return (a + c, b + d)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
338 return addPair
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
339
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
340 self.assertEqual(makeAddPair((1, 2))((100, 200)), (101,202))
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
341
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
342 def testScopeOfGlobalStmt(self):
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
343 # Examples posted by Samuele Pedroni to python-dev on 3/1/2001
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
344
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
345 exec """\
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
346 # I
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
347 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
348 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
349 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
350 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
351 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
352 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
353 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
354 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
355 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
356 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
357 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
358 self.assertEqual(f(), 7)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
359 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
360
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
361 # II
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
362 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
363 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
364 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
365 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
366 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
367 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
368 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
369 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
370 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
371 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
372 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
373 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
374 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
375
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
376 # III
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
377 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
378 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
379 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
380 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
381 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
382 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
383 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
384 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
385 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
386 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
387 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
388 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
389 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
390 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
391
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
392 # IV
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
393 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
394 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
395 x = 3
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
396 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
397 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
398 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
399 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
400 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
401 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
402 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
403 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
404 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
405 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
406 self.assertEqual(x, 2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
407
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
408 # XXX what about global statements in class blocks?
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
409 # do they affect methods?
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
410
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
411 x = 12
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
412 class Global:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
413 global x
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
414 x = 13
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
415 def set(self, val):
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
416 x = val
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
417 def get(self):
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
418 return x
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
419
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
420 g = Global()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
421 self.assertEqual(g.get(), 13)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
422 g.set(15)
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
423 self.assertEqual(g.get(), 13)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
424 """
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
425
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
426 def testLeaks(self):
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
427
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
428 class Foo:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
429 count = 0
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
430
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
431 def __init__(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
432 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
433
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
434 def __del__(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
435 Foo.count -= 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
436
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
437 def f1():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
438 x = Foo()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
439 def f2():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
440 return x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
441 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
442
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
443 for i in range(100):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
444 f1()
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
445
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
446 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
447
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
448 def testClassAndGlobal(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
449
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
450 exec """\
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
451 def test(x):
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
452 class Foo:
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
453 global x
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
454 def __call__(self, y):
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
455 return x + y
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
456 return Foo()
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
457
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
458 x = 0
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
459 self.assertEqual(test(6)(2), 8)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
460 x = -1
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
461 self.assertEqual(test(3)(2), 5)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
462
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
463 looked_up_by_load_name = False
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
464 class X:
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
465 # Implicit globals inside classes are be looked up by LOAD_NAME, not
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
466 # LOAD_GLOBAL.
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
467 locals()['looked_up_by_load_name'] = True
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
468 passed = looked_up_by_load_name
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
469
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
470 self.assert_(X.passed)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
471 """
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
472
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
473 def testLocalsFunction(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
474
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
475 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
476 def g(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
477 def h(z):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
478 return y + z
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
479 w = x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
480 y += 3
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
481 return locals()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
482 return g
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
483
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
484 d = f(2)(4)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
485 self.assert_(d.has_key('h'))
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
486 del d['h']
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
487 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
488
40938
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
489 def testLocalsClass(self):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
490 # This test verifies that calling locals() does not pollute
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
491 # the local namespace of the class with free variables. Old
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
492 # versions of Python had a bug, where a free variable being
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
493 # passed through a class namespace would be inserted into
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
494 # locals() by locals() or exec or a trace function.
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
495 #
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
496 # The real bug lies in frame code that copies variables
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
497 # between fast locals and the locals dict, e.g. when executing
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
498 # a trace function.
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
499
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
500 def f(x):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
501 class C:
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
502 x = 12
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
503 def m(self):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
504 return x
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
505 locals()
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
506 return C
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
507
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
508 self.assertEqual(f(1).x, 12)
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
509
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
510 def f(x):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
511 class C:
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
512 y = x
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
513 def m(self):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
514 return x
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
515 z = list(locals())
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
516 return C
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
517
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
518 varnames = f(1).z
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
519 self.assert_("x" not in varnames)
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
520 self.assert_("y" in varnames)
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
521
48044
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
522 def testLocalsClass_WithTrace(self):
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
523 # Issue23728: after the trace function returns, the locals()
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
524 # dictionary is used to update all variables, this used to
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
525 # include free variables. But in class statements, free
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
526 # variables are not inserted...
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
527 import sys
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
528 sys.settrace(lambda a,b,c:None)
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
529 try:
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
530 x = 12
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
531
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
532 class C:
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
533 def f(self):
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
534 return x
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
535
48045
ba2767bd4620 don't use assert statement
Benjamin Peterson <benjamin@python.org>
parents: 48044
diff changeset
536 self.assertEquals(x, 12) # Used to raise UnboundLocalError
48044
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
537 finally:
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
538 sys.settrace(None)
b836bf25f176 Issue2378: pdb would delete free variables when stepping into a class statement.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45374
diff changeset
539
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
540 def testBoundAndFree(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
541 # 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
542
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
543 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
544 class C:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
545 def m(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
546 return x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
547 a = x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
548 return C
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17494
diff changeset
549
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
550 inst = f(3)()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
551 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
552
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
553 def testInteractionWithTraceFunc(self):
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
554
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
555 import sys
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
556 def tracer(a,b,c):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
557 return tracer
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
558
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
559 def adaptgetter(name, klass, getter):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
560 kind, des = getter
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
561 if kind == 1: # AV happens when stepping from this line to next
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
562 if des == "":
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
563 des = "_%s__%s" % (klass.__name__, name)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
564 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
565
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
566 class TestClass:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
567 pass
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
568
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
569 sys.settrace(tracer)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
570 adaptgetter("foo", TestClass, (1, ""))
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
571 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
572
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
573 self.assertRaises(TypeError, sys.settrace)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
574
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
575 def testEvalExecFreeVars(self):
22119
0859e764509e SF #506611, fix sys.setprofile(), sys.settrace() core dumps
Neal Norwitz <nnorwitz@gmail.com>
parents: 21431
diff changeset
576
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
577 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
578 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
579
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
580 g = f(3)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
581 self.assertRaises(TypeError, eval, g.func_code)
18886
c7bb9563b149 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18524
diff changeset
582
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
583 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
584 exec g.func_code in {}
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
585 except TypeError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
586 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
587 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
588 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
589
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
590 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
591
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
592 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
593 print bad
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
594 except NameError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
595 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
596 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
597 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
598
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
599 def x():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
600 [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
601
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
602 x()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
603 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
604 print bad
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
605 except NameError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
606 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
607
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
608 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
609
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
610 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
611 def g():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
612 x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
613 eval("x + 1")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
614 return g
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
615
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
616 f(4)()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
617
45374
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
618 def testFreeingCell(self):
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
619 # Test what happens when a finalizer accesses
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
620 # the cell where the object was stored.
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
621 class Special:
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
622 def __del__(self):
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
623 nestedcell_get()
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
624
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
625 def f():
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
626 global nestedcell_get
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
627 def nestedcell_get():
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
628 return c
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
629
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
630 c = (Special(),)
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
631 c = 2
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
632
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
633 f() # used to crash the interpreter...
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
634
52520
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
635 def testGlobalInParallelNestedFunctions(self):
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
636 # A symbol table bug leaked the global statement from one
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
637 # function to other nested functions in the same block.
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
638 # This test verifies that a global statement in the first
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
639 # function does not affect the second function.
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
640 CODE = """def f():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
641 y = 1
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
642 def g():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
643 global y
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
644 return y
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
645 def h():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
646 return y + 1
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
647 return g, h
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
648
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
649 y = 9
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
650 g, h = f()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
651 result9 = g()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
652 result2 = h()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
653 """
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
654 local_ns = {}
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
655 global_ns = {}
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
656 exec CODE in local_ns, global_ns
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
657 self.assertEqual(2, global_ns["result2"])
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
658 self.assertEqual(9, global_ns["result9"])
45374
270fcd2e9782 Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 40938
diff changeset
659
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
660
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
661 def test_main():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
662 run_unittest(ScopeTests)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
663
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
664 if __name__ == '__main__':
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
665 test_main()