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 | import warnings |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8 | class ScopeTests(unittest.TestCase): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 10 | def testSimpleNesting(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | def make_adder(x): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 13 | def adder(y): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 23 | def testExtraNesting(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | def testSimpleAndRebinding(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 39 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 52 | def testNestingGlobalNoFree(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 70 | def testNestingThroughClass(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 71 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | def testNestingPlusFreeRefToGlobal(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 85 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | def testNearestEnclosingScope(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Amaury Forgeot d'Arc | e670bd4 | 2007-11-24 00:29:24 +0000 | [diff] [blame] | 169 | def testCellIsKwonlyArg(self): |
| 170 | # Issue 1409: Initialisation of a cell value, |
| 171 | # when it comes from a keyword-only parameter |
| 172 | def foo(*, a=17): |
| 173 | def bar(): |
| 174 | return a + 5 |
| 175 | return bar() + 3 |
| 176 | |
| 177 | self.assertEqual(foo(a=42), 50) |
| 178 | self.assertEqual(foo(), 25) |
| 179 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 180 | def testRecursion(self): |
| 181 | |
| 182 | def f(x): |
| 183 | def fact(n): |
| 184 | if n == 0: |
| 185 | return 1 |
| 186 | else: |
| 187 | return n * fact(n - 1) |
| 188 | if x >= 0: |
| 189 | return fact(x) |
| 190 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 191 | raise ValueError("x must be >= 0") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | |
| 193 | self.assertEqual(f(6), 720) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 194 | |
| 195 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 196 | def testUnoptimizedNamespaces(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 197 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 198 | check_syntax_error(self, """\ |
Jeremy Hylton | 5941d19 | 2001-02-27 20:23:58 +0000 | [diff] [blame] | 199 | def unoptimized_clash1(strip): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 200 | def f(s): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 201 | from sys import * |
| 202 | return getrefcount(s) # ambiguity: free or local |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 203 | return f |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 204 | """) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 205 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 206 | check_syntax_error(self, """\ |
Jeremy Hylton | 5941d19 | 2001-02-27 20:23:58 +0000 | [diff] [blame] | 207 | def unoptimized_clash2(): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 208 | from sys import * |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 209 | def f(s): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 210 | return getrefcount(s) # ambiguity: global or local |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 211 | return f |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 212 | """) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 213 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 214 | check_syntax_error(self, """\ |
Jeremy Hylton | 5941d19 | 2001-02-27 20:23:58 +0000 | [diff] [blame] | 215 | def unoptimized_clash2(): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 216 | from sys import * |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 217 | def g(): |
| 218 | def f(s): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 219 | return getrefcount(s) # ambiguity: global or local |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 220 | return f |
| 221 | """) |
| 222 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 223 | check_syntax_error(self, """\ |
Jeremy Hylton | 5941d19 | 2001-02-27 20:23:58 +0000 | [diff] [blame] | 224 | def f(x): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 225 | def g(): |
| 226 | return x |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 227 | del x # can't del name |
| 228 | """) |
| 229 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 230 | check_syntax_error(self, """\ |
Jeremy Hylton | 5941d19 | 2001-02-27 20:23:58 +0000 | [diff] [blame] | 231 | def f(): |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 232 | def g(): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 233 | from sys import * |
| 234 | return getrefcount # global or local? |
Tim Peters | 0e6d213 | 2001-02-15 23:56:39 +0000 | [diff] [blame] | 235 | """) |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 236 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 237 | def testLambdas(self): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 238 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 239 | f1 = lambda x: lambda y: x + y |
| 240 | inc = f1(1) |
| 241 | plus10 = f1(10) |
| 242 | self.assertEqual(inc(1), 2) |
| 243 | self.assertEqual(plus10(5), 15) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 244 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 245 | f2 = lambda x: (lambda : lambda y: x + y)() |
| 246 | inc = f2(1) |
| 247 | plus10 = f2(10) |
| 248 | self.assertEqual(inc(1), 2) |
| 249 | self.assertEqual(plus10(5), 15) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 250 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 251 | f3 = lambda x: lambda y: global_x + y |
| 252 | global_x = 1 |
| 253 | inc = f3(None) |
| 254 | self.assertEqual(inc(2), 3) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 255 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y) |
| 257 | g = f8(1, 2, 3) |
| 258 | h = g(2, 4, 6) |
| 259 | self.assertEqual(h(), 18) |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 260 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 261 | def testUnboundLocal(self): |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 262 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 263 | def errorInOuter(): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 264 | print(y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 265 | def inner(): |
| 266 | return y |
| 267 | y = 1 |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 268 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 269 | def errorInInner(): |
| 270 | def inner(): |
| 271 | return y |
| 272 | inner() |
| 273 | y = 1 |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 274 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 275 | try: |
| 276 | errorInOuter() |
| 277 | except UnboundLocalError: |
| 278 | pass |
| 279 | else: |
| 280 | self.fail() |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 281 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 282 | try: |
| 283 | errorInInner() |
| 284 | except NameError: |
| 285 | pass |
| 286 | else: |
| 287 | self.fail() |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 288 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 289 | # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation |
| 290 | exec(""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | global_x = 1 |
| 292 | def f(): |
| 293 | global_x += 1 |
| 294 | try: |
| 295 | f() |
| 296 | except UnboundLocalError: |
| 297 | pass |
| 298 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 299 | fail('scope of global_x not correctly determined') |
| 300 | """, {'fail': self.fail}) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 302 | def testComplexDefinitions(self): |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 303 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 304 | def makeReturner(*lst): |
| 305 | def returner(): |
| 306 | return lst |
| 307 | return returner |
Tim Peters | 0e6d213 | 2001-02-15 23:56:39 +0000 | [diff] [blame] | 308 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 309 | self.assertEqual(makeReturner(1,2,3)(), (1,2,3)) |
Tim Peters | 0e6d213 | 2001-02-15 23:56:39 +0000 | [diff] [blame] | 310 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 311 | def makeReturner2(**kwargs): |
| 312 | def returner(): |
| 313 | return kwargs |
| 314 | return returner |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 315 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 316 | self.assertEqual(makeReturner2(a=11)()['a'], 11) |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame] | 317 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 318 | def testScopeOfGlobalStmt(self): |
Guido van Rossum | 9aa643c | 2001-03-01 20:35:45 +0000 | [diff] [blame] | 319 | # Examples posted by Samuele Pedroni to python-dev on 3/1/2001 |
| 320 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 321 | exec("""\ |
Guido van Rossum | 9aa643c | 2001-03-01 20:35:45 +0000 | [diff] [blame] | 322 | # I |
| 323 | x = 7 |
| 324 | def f(): |
| 325 | x = 1 |
| 326 | def g(): |
| 327 | global x |
| 328 | def i(): |
| 329 | def h(): |
| 330 | return x |
| 331 | return h() |
| 332 | return i() |
| 333 | return g() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 334 | self.assertEqual(f(), 7) |
| 335 | self.assertEqual(x, 7) |
Guido van Rossum | 9aa643c | 2001-03-01 20:35:45 +0000 | [diff] [blame] | 336 | |
| 337 | # II |
| 338 | x = 7 |
| 339 | def f(): |
| 340 | x = 1 |
| 341 | def g(): |
| 342 | x = 2 |
| 343 | def i(): |
| 344 | def h(): |
| 345 | return x |
| 346 | return h() |
| 347 | return i() |
| 348 | return g() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 349 | self.assertEqual(f(), 2) |
| 350 | self.assertEqual(x, 7) |
Guido van Rossum | 9aa643c | 2001-03-01 20:35:45 +0000 | [diff] [blame] | 351 | |
| 352 | # III |
| 353 | x = 7 |
| 354 | def f(): |
| 355 | x = 1 |
| 356 | def g(): |
| 357 | global x |
| 358 | x = 2 |
| 359 | def i(): |
| 360 | def h(): |
| 361 | return x |
| 362 | return h() |
| 363 | return i() |
| 364 | return g() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 365 | self.assertEqual(f(), 2) |
| 366 | self.assertEqual(x, 2) |
Guido van Rossum | 9aa643c | 2001-03-01 20:35:45 +0000 | [diff] [blame] | 367 | |
| 368 | # IV |
| 369 | x = 7 |
| 370 | def f(): |
| 371 | x = 3 |
| 372 | def g(): |
| 373 | global x |
| 374 | x = 2 |
| 375 | def i(): |
| 376 | def h(): |
| 377 | return x |
| 378 | return h() |
| 379 | return i() |
| 380 | return g() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 381 | self.assertEqual(f(), 2) |
| 382 | self.assertEqual(x, 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | |
| 384 | # XXX what about global statements in class blocks? |
| 385 | # do they affect methods? |
| 386 | |
| 387 | x = 12 |
| 388 | class Global: |
| 389 | global x |
| 390 | x = 13 |
| 391 | def set(self, val): |
| 392 | x = val |
| 393 | def get(self): |
| 394 | return x |
| 395 | |
| 396 | g = Global() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 397 | self.assertEqual(g.get(), 13) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 398 | g.set(15) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 399 | self.assertEqual(g.get(), 13) |
| 400 | """) |
Jeremy Hylton | 5b44a67 | 2001-03-13 02:01:12 +0000 | [diff] [blame] | 401 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 402 | def testLeaks(self): |
Jeremy Hylton | 5b44a67 | 2001-03-13 02:01:12 +0000 | [diff] [blame] | 403 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 404 | class Foo: |
| 405 | count = 0 |
Tim Peters | 30edd23 | 2001-03-16 08:29:48 +0000 | [diff] [blame] | 406 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 407 | def __init__(self): |
| 408 | Foo.count += 1 |
Jeremy Hylton | 5b44a67 | 2001-03-13 02:01:12 +0000 | [diff] [blame] | 409 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 410 | def __del__(self): |
| 411 | Foo.count -= 1 |
Jeremy Hylton | 5b44a67 | 2001-03-13 02:01:12 +0000 | [diff] [blame] | 412 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 413 | def f1(): |
| 414 | x = Foo() |
| 415 | def f2(): |
| 416 | return x |
| 417 | f2() |
Tim Peters | 30edd23 | 2001-03-16 08:29:48 +0000 | [diff] [blame] | 418 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 419 | for i in range(100): |
| 420 | f1() |
Jeremy Hylton | 5b44a67 | 2001-03-13 02:01:12 +0000 | [diff] [blame] | 421 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 422 | self.assertEqual(Foo.count, 0) |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 423 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 424 | def testClassAndGlobal(self): |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 425 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 426 | exec("""\ |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 427 | def test(x): |
| 428 | class Foo: |
| 429 | global x |
| 430 | def __call__(self, y): |
| 431 | return x + y |
| 432 | return Foo() |
| 433 | |
| 434 | x = 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 435 | self.assertEqual(test(6)(2), 8) |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 436 | x = -1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 437 | self.assertEqual(test(3)(2), 5) |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 438 | |
Neil Schemenauer | d403c45 | 2005-10-23 04:24:49 +0000 | [diff] [blame] | 439 | looked_up_by_load_name = False |
| 440 | class X: |
| 441 | # Implicit globals inside classes are be looked up by LOAD_NAME, not |
| 442 | # LOAD_GLOBAL. |
| 443 | locals()['looked_up_by_load_name'] = True |
| 444 | passed = looked_up_by_load_name |
| 445 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 446 | self.assert_(X.passed) |
| 447 | """) |
Neil Schemenauer | d403c45 | 2005-10-23 04:24:49 +0000 | [diff] [blame] | 448 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 449 | def testLocalsFunction(self): |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 450 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 451 | def f(x): |
| 452 | def g(y): |
| 453 | def h(z): |
| 454 | return y + z |
| 455 | w = x + y |
| 456 | y += 3 |
| 457 | return locals() |
| 458 | return g |
Jeremy Hylton | 5c7a251 | 2001-03-21 16:44:39 +0000 | [diff] [blame] | 459 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 460 | d = f(2)(4) |
| 461 | self.assert_('h' in d) |
| 462 | del d['h'] |
| 463 | self.assertEqual(d, {'x': 2, 'y': 7, 'w': 6}) |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 464 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 465 | def testLocalsClass(self): |
| 466 | # This test verifies that calling locals() does not pollute |
| 467 | # the local namespace of the class with free variables. Old |
| 468 | # versions of Python had a bug, where a free variable being |
| 469 | # passed through a class namespace would be inserted into |
| 470 | # locals() by locals() or exec or a trace function. |
| 471 | # |
| 472 | # The real bug lies in frame code that copies variables |
| 473 | # between fast locals and the locals dict, e.g. when executing |
| 474 | # a trace function. |
| 475 | |
| 476 | def f(x): |
| 477 | class C: |
| 478 | x = 12 |
| 479 | def m(self): |
| 480 | return x |
| 481 | locals() |
| 482 | return C |
| 483 | |
| 484 | self.assertEqual(f(1).x, 12) |
| 485 | |
| 486 | def f(x): |
| 487 | class C: |
| 488 | y = x |
| 489 | def m(self): |
| 490 | return x |
| 491 | z = list(locals()) |
| 492 | return C |
| 493 | |
| 494 | varnames = f(1).z |
| 495 | self.assert_("x" not in varnames) |
| 496 | self.assert_("y" in varnames) |
| 497 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 498 | def testBoundAndFree(self): |
| 499 | # var is bound and free in class |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 500 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 501 | def f(x): |
| 502 | class C: |
| 503 | def m(self): |
| 504 | return x |
| 505 | a = x |
| 506 | return C |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 507 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 508 | inst = f(3)() |
| 509 | self.assertEqual(inst.a, inst.m()) |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 510 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 511 | def testInteractionWithTraceFunc(self): |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 512 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 513 | import sys |
| 514 | def tracer(a,b,c): |
| 515 | return tracer |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 516 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 517 | def adaptgetter(name, klass, getter): |
| 518 | kind, des = getter |
| 519 | if kind == 1: # AV happens when stepping from this line to next |
| 520 | if des == "": |
| 521 | des = "_%s__%s" % (klass.__name__, name) |
| 522 | return lambda obj: getattr(obj, des) |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 523 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 524 | class TestClass: |
| 525 | pass |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 526 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 527 | sys.settrace(tracer) |
| 528 | adaptgetter("foo", TestClass, (1, "")) |
| 529 | sys.settrace(None) |
Jeremy Hylton | 5121e7d | 2001-07-30 21:55:29 +0000 | [diff] [blame] | 530 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 531 | self.assertRaises(TypeError, sys.settrace) |
Neal Norwitz | 290d31e | 2002-03-03 15:12:58 +0000 | [diff] [blame] | 532 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 533 | def testEvalExecFreeVars(self): |
Jeremy Hylton | 5121e7d | 2001-07-30 21:55:29 +0000 | [diff] [blame] | 534 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 535 | def f(x): |
| 536 | return lambda: x + 1 |
Jeremy Hylton | 5121e7d | 2001-07-30 21:55:29 +0000 | [diff] [blame] | 537 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 538 | g = f(3) |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 539 | self.assertRaises(TypeError, eval, g.__code__) |
Jeremy Hylton | cd73836 | 2001-08-07 16:38:19 +0000 | [diff] [blame] | 540 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 541 | try: |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 542 | exec(g.__code__, {}) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 543 | except TypeError: |
| 544 | pass |
| 545 | else: |
| 546 | self.fail("exec should have failed, because code contained free vars") |
Jeremy Hylton | ccae8377 | 2001-12-13 19:45:04 +0000 | [diff] [blame] | 547 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 548 | def testListCompLocalVars(self): |
Jeremy Hylton | cf672f1 | 2001-10-18 16:23:11 +0000 | [diff] [blame] | 549 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 550 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 551 | print(bad) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 552 | except NameError: |
| 553 | pass |
| 554 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 555 | print("bad should not be defined") |
Jeremy Hylton | cf672f1 | 2001-10-18 16:23:11 +0000 | [diff] [blame] | 556 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 557 | def x(): |
| 558 | [bad for s in 'a b' for bad in s.split()] |
Jeremy Hylton | cf672f1 | 2001-10-18 16:23:11 +0000 | [diff] [blame] | 559 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 560 | x() |
| 561 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 562 | print(bad) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 563 | except NameError: |
| 564 | pass |
Jeremy Hylton | 954aed8 | 2002-04-20 04:51:39 +0000 | [diff] [blame] | 565 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 566 | def testEvalFreeVars(self): |
Jeremy Hylton | 954aed8 | 2002-04-20 04:51:39 +0000 | [diff] [blame] | 567 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 568 | def f(x): |
| 569 | def g(): |
| 570 | x |
| 571 | eval("x + 1") |
| 572 | return g |
Jeremy Hylton | 954aed8 | 2002-04-20 04:51:39 +0000 | [diff] [blame] | 573 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 574 | f(4)() |
| 575 | |
Christian Heimes | 18c6689 | 2008-02-17 13:31:39 +0000 | [diff] [blame] | 576 | def testFreeingCell(self): |
| 577 | # Test what happens when a finalizer accesses |
| 578 | # the cell where the object was stored. |
| 579 | class Special: |
| 580 | def __del__(self): |
| 581 | nestedcell_get() |
| 582 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 583 | def testNonLocalFunction(self): |
| 584 | |
| 585 | def f(x): |
| 586 | def inc(): |
| 587 | nonlocal x |
| 588 | x += 1 |
| 589 | return x |
| 590 | def dec(): |
| 591 | nonlocal x |
| 592 | x -= 1 |
| 593 | return x |
| 594 | return inc, dec |
| 595 | |
| 596 | inc, dec = f(0) |
| 597 | self.assertEqual(inc(), 1) |
| 598 | self.assertEqual(inc(), 2) |
| 599 | self.assertEqual(dec(), 1) |
| 600 | self.assertEqual(dec(), 0) |
| 601 | |
| 602 | def testNonLocalMethod(self): |
| 603 | |
| 604 | def f(x): |
| 605 | class c: |
| 606 | def inc(self): |
| 607 | nonlocal x |
| 608 | x += 1 |
| 609 | return x |
| 610 | def dec(self): |
| 611 | nonlocal x |
| 612 | x -= 1 |
| 613 | return x |
| 614 | return c() |
| 615 | |
| 616 | c = f(0) |
| 617 | self.assertEqual(c.inc(), 1) |
| 618 | self.assertEqual(c.inc(), 2) |
| 619 | self.assertEqual(c.dec(), 1) |
| 620 | self.assertEqual(c.dec(), 0) |
| 621 | |
| 622 | def testNonLocalClass(self): |
| 623 | |
| 624 | def f(x): |
| 625 | class c: |
| 626 | nonlocal x |
| 627 | x += 1 |
| 628 | def get(self): |
| 629 | return x |
| 630 | return c() |
| 631 | |
| 632 | c = f(0) |
| 633 | self.assertEqual(c.get(), 1) |
| 634 | self.assert_("x" not in c.__class__.__dict__) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 635 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 636 | |
| 637 | def testNonLocalGenerator(self): |
| 638 | |
| 639 | def f(x): |
| 640 | def g(y): |
| 641 | nonlocal x |
| 642 | for i in range(y): |
| 643 | x += 1 |
| 644 | yield x |
| 645 | return g |
| 646 | |
| 647 | g = f(0) |
| 648 | self.assertEqual(list(g(5)), [1, 2, 3, 4, 5]) |
| 649 | |
| 650 | def testNestedNonLocal(self): |
| 651 | |
| 652 | def f(x): |
| 653 | def g(): |
| 654 | nonlocal x |
| 655 | x -= 2 |
| 656 | def h(): |
| 657 | nonlocal x |
| 658 | x += 4 |
| 659 | return x |
| 660 | return h |
| 661 | return g |
| 662 | |
| 663 | g = f(1) |
| 664 | h = g() |
| 665 | self.assertEqual(h(), 3) |
| 666 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 667 | |
| 668 | def test_main(): |
| 669 | run_unittest(ScopeTests) |
| 670 | |
| 671 | if __name__ == '__main__': |
| 672 | test_main() |