annotate Lib/test/test_scope.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
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
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
2 from test.test_support import check_syntax_error, check_py3k_warnings, \
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
3 check_warnings, run_unittest
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
5
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
6 class ScopeTests(unittest.TestCase):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
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 def testSimpleNesting(self):
40284
ff678669aa6e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40217
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 make_adder(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
11 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
12 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
13 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
15 inc = make_adder(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
16 plus10 = make_adder(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
18 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
19 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
20
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
21 def testExtraNesting(self):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
23 def make_adder2(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
24 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
25 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
26 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
27 return adder
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
28 return extra()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
29
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
30 inc = make_adder2(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
31 plus10 = make_adder2(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
32
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
33 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
34 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
36 def testSimpleAndRebinding(self):
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 make_adder3(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
39 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
40 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
41 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
42 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
43
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
44 inc = make_adder3(0)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
45 plus10 = make_adder3(9)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
46
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
47 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
48 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
49
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
50 def testNestingGlobalNoFree(self):
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 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
53 def nest():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
54 def nest():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
55 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
56 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
57 return adder
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
58 return nest()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
59 return nest()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
61 global_x = 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
62 adder = make_adder4()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
63 self.assertEqual(adder(1), 2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
65 global_x = 10
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
66 self.assertEqual(adder(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
68 def testNestingThroughClass(self):
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 make_adder5(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
71 class Adder:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
72 def __call__(self, y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
73 return x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
74 return Adder()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
75
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
76 inc = make_adder5(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
77 plus10 = make_adder5(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
78
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
79 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
80 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
82 def testNestingPlusFreeRefToGlobal(self):
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 make_adder6(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
85 global global_nest_x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
86 def adder(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
87 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
88 global_nest_x = x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
89 return adder
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
90
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
91 inc = make_adder6(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
92 plus10 = make_adder6(10)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
93
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
94 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
95 self.assertEqual(plus10(-2), 8)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
96
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
97 def testNearestEnclosingScope(self):
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 f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
100 def g(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
101 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
102 def h(z):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
103 return x + z
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
104 return h
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
105 return g(2)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
106
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
107 test_func = f(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
108 self.assertEqual(test_func(5), 47)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
109
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
110 def testMixedFreevarsAndCellvars(self):
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 identity(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
113 return x
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
114
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
115 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
116 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
117 a = a + x # 3
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
118 def h():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
119 # z * (4 + 9)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
120 # 3 * 13
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
121 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
122 y = c + z # 9
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
123 return h
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
124 return g
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
125
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
126 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
127 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
128 self.assertEqual(h(), 39)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
129
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
130 def testFreeVarInMethod(self):
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 test():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
133 method_and_var = "var"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
134 class Test:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
135 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
136 return "method"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
137 def test(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
138 return method_and_var
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
139 def actual_global(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
140 return str("global")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
141 def str(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
142 return str(self)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
143 return Test()
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
144
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
145 t = test()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
146 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
147 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
148 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
149
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
150 method_and_var = "var"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
151 class Test:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
152 # 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
153 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
154 return "method"
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
155 def test(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
156 return method_and_var
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
157 def actual_global(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
158 return str("global")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
159 def str(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
160 return str(self)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
161
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
162 t = Test()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
163 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
164 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
165 self.assertEqual(t.actual_global(), "global")
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
166
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
167 def testRecursion(self):
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 f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
170 def fact(n):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
171 if n == 0:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
172 return 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
173 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
174 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
175 if x >= 0:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
176 return fact(x)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
177 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
178 raise ValueError, "x must be >= 0"
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
179
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
180 self.assertEqual(f(6), 720)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
181
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
182
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
183 def testUnoptimizedNamespaces(self):
16262
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 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
186 def unoptimized_clash1(strip):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
187 def f(s):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
188 from string import *
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
189 return strip(s) # ambiguity: free or local
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
190 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
191 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
192
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
193 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
194 def unoptimized_clash2():
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
195 from string import *
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
196 def f(s):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
197 return strip(s) # ambiguity: global or local
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
198 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
199 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
200
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
201 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
202 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
203 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
204 def g():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
205 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
206 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
207 return f
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
208 """)
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
209
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
210 # 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
211 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
212 def error(y):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
213 exec "a = 1"
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
214 def f(x):
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
215 return x + y
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
216 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
217 """)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
218
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
219 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
220 def f(x):
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
221 def g():
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
222 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
223 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
224 """)
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
225
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
226 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
227 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
228 def g():
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
229 from string import *
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
230 return strip # global or local?
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
231 """)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
232
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
233 # 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
234
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
235 exec """
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
236 def noproblem1():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
237 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
238 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
239
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
240 def noproblem2():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
241 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
242 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
243 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
244
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
245 def noproblem3():
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
246 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
247 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
248 global y
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
249 y = x
19035
c1729cdc0734 silence warnings about import *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18886
diff changeset
250 """
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
251
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
252 def testLambdas(self):
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 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
255 inc = f1(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
256 plus10 = f1(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
257 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
258 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
259
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
260 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
261 inc = f2(1)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
262 plus10 = f2(10)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
263 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
264 self.assertEqual(plus10(5), 15)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
265
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
266 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
267 global_x = 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
268 inc = f3(None)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
269 self.assertEqual(inc(2), 3)
16262
a704ea5eceb6 PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
270
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
271 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
272 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
273 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
274 self.assertEqual(h(), 18)
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
275
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
276 def testUnboundLocal(self):
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 errorInOuter():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
279 print y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
280 def inner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
281 return y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
282 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
283
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
284 def errorInInner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
285 def inner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
286 return y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
287 inner()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
288 y = 1
16454
ddce715e49ba Fix test 9 (caught by ?!ng)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16360
diff changeset
289
55063
7b758f808330 condense with assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
290 self.assertRaises(UnboundLocalError, errorInOuter)
7b758f808330 condense with assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
291 self.assertRaises(NameError, errorInInner)
16541
8eed373bce7f update test cases for recent compiler changes: exec/import * in nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16536
diff changeset
292
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
293 # 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
294 exec """
39037
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
295 global_x = 1
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
296 def f():
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
297 global_x += 1
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
298 try:
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
299 f()
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
300 except UnboundLocalError:
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
301 pass
9f70f6ba7b69 Fix AST compiler bug #1501934: incorrect LOAD/STORE_GLOBAL generation.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35773
diff changeset
302 else:
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
303 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
304 """ 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
305
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
306 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
307
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
308 def makeReturner(*lst):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
309 def returner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
310 return lst
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
311 return returner
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
312
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
313 self.assertEqual(makeReturner(1,2,3)(), (1,2,3))
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16541
diff changeset
314
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
315 def makeReturner2(**kwargs):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
316 def returner():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
317 return kwargs
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
318 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
319
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
320 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
321
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
322 with check_py3k_warnings(("tuple parameter unpacking has been removed",
63650
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
323 SyntaxWarning)):
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
324 exec """\
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
325 def makeAddPair((a, b)):
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
326 def addPair((c, d)):
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
327 return (a + c, b + d)
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
328 return addPair
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
329 """ in locals()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
330 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
331
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
332 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
333 # 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
334
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
335 exec """\
16910
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
336 # I
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
337 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
338 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
339 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
340 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
341 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
342 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
343 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
344 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
345 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
346 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
347 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
348 self.assertEqual(f(), 7)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
349 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
350
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
351 # II
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
352 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
353 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
354 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
355 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
356 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
357 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
358 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
359 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
360 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
361 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
362 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
363 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
364 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
365
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
366 # III
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
367 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
368 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
369 x = 1
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
370 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
371 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
372 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
373 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
374 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
375 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
376 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
377 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
378 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
379 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
380 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
381
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
382 # IV
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
383 x = 7
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
384 def f():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
385 x = 3
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
386 def g():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
387 global x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
388 x = 2
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
389 def i():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
390 def h():
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
391 return x
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
392 return h()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
393 return i()
203ad6f3ef8c Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
Guido van Rossum <guido@python.org>
parents: 16803
diff changeset
394 return g()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
395 self.assertEqual(f(), 2)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
396 self.assertEqual(x, 2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
397
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
398 # XXX what about global statements in class blocks?
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
399 # do they affect methods?
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
400
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
401 x = 12
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
402 class Global:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
403 global x
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
404 x = 13
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
405 def set(self, val):
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
406 x = val
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
407 def get(self):
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
408 return x
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
409
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
410 g = Global()
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
411 self.assertEqual(g.get(), 13)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24364
diff changeset
412 g.set(15)
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
413 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
414 """
17035
7e1e6725509e Add test to verify that nested functions with free variables don't
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16910
diff changeset
415
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
416 def testLeaks(self):
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
417
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
418 class Foo:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
419 count = 0
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
420
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
421 def __init__(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
422 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
423
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
424 def __del__(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
425 Foo.count -= 1
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
426
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
427 def f1():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
428 x = Foo()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
429 def f2():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
430 return x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
431 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
432
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
433 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
434 f1()
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17035
diff changeset
435
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
436 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
437
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
438 def testClassAndGlobal(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
439
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
440 exec """\
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
441 def test(x):
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
442 class Foo:
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
443 global x
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
444 def __call__(self, y):
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
445 return x + y
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
446 return Foo()
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
447
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
448 x = 0
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
449 self.assertEqual(test(6)(2), 8)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
450 x = -1
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
451 self.assertEqual(test(3)(2), 5)
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
452
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
453 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
454 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
455 # 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
456 # 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
457 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
458 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
459
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52520
diff changeset
460 self.assertTrue(X.passed)
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
461 """
35773
0aef351223da Fix arigo's funky LOAD_NAME bug: implicit globals inside classes have
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35742
diff changeset
462
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
463 def testLocalsFunction(self):
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
464
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
465 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
466 def g(y):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
467 def h(z):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
468 return y + z
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
469 w = x + y
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
470 y += 3
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
471 return locals()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
472 return g
17105
8ff24abeb388 Add tests for recent changes:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17055
diff changeset
473
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
474 d = f(2)(4)
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
475 self.assertIn('h', d)
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
476 del d['h']
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
477 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
478
40938
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
479 def testLocalsClass(self):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
480 # 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
481 # 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
482 # 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
483 # 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
484 # 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
485 #
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
486 # 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
487 # 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
488 # a trace function.
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
489
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
490 def f(x):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
491 class C:
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
492 x = 12
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
493 def m(self):
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
494 return x
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
495 locals()
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
496 return C
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
497
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
498 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
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 y = x
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 z = list(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 varnames = f(1).z
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
509 self.assertNotIn("x", varnames)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
510 self.assertIn("y", varnames)
40938
047ee23c0f87 Do not copy free variables to locals in class namespaces.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40284
diff changeset
511
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
512 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
513 # 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
514 # 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
515 # 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
516 # 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
517 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
518 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
519 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
520 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
521
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 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
523 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
524 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
525
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65394
diff changeset
526 self.assertEqual(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
527 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
528 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
529
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
530 def testBoundAndFree(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
531 # 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
532
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
533 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
534 class C:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
535 def m(self):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
536 return x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
537 a = x
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
538 return C
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17494
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 inst = f(3)()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
541 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
542
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
543 def testInteractionWithTraceFunc(self):
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
544
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
545 import sys
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
546 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
547 return tracer
17701
ded41be84da4 SF patch 419176 from MvL; fixed bug 418977
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
548
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
549 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
550 kind, des = getter
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
551 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
552 if des == "":
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
553 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
554 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
555
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
556 class TestClass:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
557 pass
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 sys.settrace(tracer)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
560 adaptgetter("foo", TestClass, (1, ""))
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
561 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
562
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
563 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
564
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
565 def testEvalExecFreeVars(self):
22119
0859e764509e SF #506611, fix sys.setprofile(), sys.settrace() core dumps
Neal Norwitz <nnorwitz@gmail.com>
parents: 21431
diff changeset
566
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
567 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
568 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
569
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
570 g = f(3)
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
571 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
572
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
573 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
574 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
575 except TypeError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
576 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
577 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
578 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
579
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
580 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
581
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
582 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
583 print bad
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
584 except NameError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
585 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
586 else:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
587 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
588
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
589 def x():
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
590 [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
591
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
592 x()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
593 try:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
594 print bad
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
595 except NameError:
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
596 pass
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
597
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
598 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
599
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
600 def f(x):
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
601 def g():
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 eval("x + 1")
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
604 return g
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
605
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
606 f(4)()
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
607
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
608 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
609 # 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
610 # 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
611 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
612 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
613 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
614
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
615 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
616 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
617 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
618 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
619
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 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
621 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
622
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 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
624
52520
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
625 def testGlobalInParallelNestedFunctions(self):
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
626 # 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
627 # 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
628 # 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
629 # 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
630 CODE = """def f():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
631 y = 1
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
632 def g():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
633 global y
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
634 return y
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
635 def h():
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
636 return y + 1
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
637 return g, h
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
638
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
639 y = 9
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
640 g, h = f()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
641 result9 = g()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
642 result2 = h()
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
643 """
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
644 local_ns = {}
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
645 global_ns = {}
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
646 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
647 self.assertEqual(2, global_ns["result2"])
6910792fcd4b Merged revisions 70801,70809 via svnmerge from
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48045
diff changeset
648 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
649
65394
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
650 def testTopIsNotSignificant(self):
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
651 # See #9997.
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
652 def top(a):
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
653 pass
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
654 def b():
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
655 global a
0bd0e87a6851 Merged revisions 85562 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59726
diff changeset
656
23187
7c0e8dbcf57a Add test for eval() w/ free variables.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22119
diff changeset
657
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
658 def test_main():
63650
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
659 with check_warnings(("import \* only allowed at module level",
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
660 SyntaxWarning)):
3efb4581790a Fix deprecation warnings in test_scope.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 52520
diff changeset
661 run_unittest(ScopeTests)
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
662
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
663 if __name__ == '__main__':
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39037
diff changeset
664 test_main()