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