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