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