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