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