Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 1 | from test.test_support import verify, TestFailed |
| 2 | |
| 3 | print "1. simple nesting" |
| 4 | |
| 5 | def make_adder(x): |
| 6 | def adder(y): |
| 7 | return x + y |
| 8 | return adder |
| 9 | |
| 10 | inc = make_adder(1) |
| 11 | plus10 = make_adder(10) |
| 12 | |
| 13 | verify(inc(1) == 2) |
| 14 | verify(plus10(-2) == 8) |
| 15 | |
| 16 | print "2. extra nesting" |
| 17 | |
| 18 | def make_adder2(x): |
| 19 | def extra(): # check freevars passing through non-use scopes |
| 20 | def adder(y): |
| 21 | return x + y |
| 22 | return adder |
| 23 | return extra() |
| 24 | |
| 25 | inc = make_adder2(1) |
| 26 | plus10 = make_adder2(10) |
| 27 | |
| 28 | verify(inc(1) == 2) |
| 29 | verify(plus10(-2) == 8) |
| 30 | |
| 31 | print "3. simple nesting + rebinding" |
| 32 | |
| 33 | def make_adder3(x): |
| 34 | def adder(y): |
| 35 | return x + y |
| 36 | x = x + 1 # check tracking of assignment to x in defining scope |
| 37 | return adder |
| 38 | |
| 39 | inc = make_adder3(0) |
| 40 | plus10 = make_adder3(9) |
| 41 | |
| 42 | verify(inc(1) == 2) |
| 43 | verify(plus10(-2) == 8) |
| 44 | |
| 45 | print "4. nesting with global but no free" |
| 46 | |
| 47 | def make_adder4(): # XXX add exta level of indirection |
| 48 | def nest(): |
| 49 | def nest(): |
| 50 | def adder(y): |
| 51 | return global_x + y # check that plain old globals work |
| 52 | return adder |
| 53 | return nest() |
| 54 | return nest() |
| 55 | |
| 56 | global_x = 1 |
| 57 | adder = make_adder4() |
| 58 | verify(adder(1) == 2) |
| 59 | |
| 60 | global_x = 10 |
| 61 | verify(adder(-2) == 8) |
| 62 | |
| 63 | print "5. nesting through class" |
| 64 | |
| 65 | def make_adder5(x): |
| 66 | class Adder: |
| 67 | def __call__(self, y): |
| 68 | return x + y |
| 69 | return Adder() |
| 70 | |
| 71 | inc = make_adder5(1) |
| 72 | plus10 = make_adder5(10) |
| 73 | |
| 74 | verify(inc(1) == 2) |
| 75 | verify(plus10(-2) == 8) |
| 76 | |
| 77 | print "6. nesting plus free ref to global" |
| 78 | |
| 79 | def make_adder6(x): |
| 80 | global global_nest_x |
| 81 | def adder(y): |
| 82 | return global_nest_x + y |
| 83 | global_nest_x = x |
| 84 | return adder |
| 85 | |
| 86 | inc = make_adder6(1) |
| 87 | plus10 = make_adder6(10) |
| 88 | |
Jeremy Hylton | 251ef96 | 2001-01-30 01:26:53 +0000 | [diff] [blame] | 89 | verify(inc(1) == 11) # there's only one global |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 90 | verify(plus10(-2) == 8) |
| 91 | |
| 92 | print "7. nearest enclosing scope" |
| 93 | |
| 94 | def f(x): |
| 95 | def g(y): |
| 96 | x = 42 # check that this masks binding in f() |
| 97 | def h(z): |
| 98 | return x + z |
| 99 | return h |
| 100 | return g(2) |
| 101 | |
| 102 | test_func = f(10) |
| 103 | verify(test_func(5) == 47) |
| 104 | |
| 105 | print "8. mixed freevars and cellvars" |
| 106 | |
| 107 | def identity(x): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 108 | return x |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 109 | |
| 110 | def f(x, y, z): |
| 111 | def g(a, b, c): |
| 112 | a = a + x # 3 |
| 113 | def h(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 114 | # z * (4 + 9) |
| 115 | # 3 * 13 |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 116 | return identity(z * (b + y)) |
| 117 | y = c + z # 9 |
| 118 | return h |
| 119 | return g |
| 120 | |
| 121 | g = f(1, 2, 3) |
| 122 | h = g(2, 4, 6) |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 123 | verify(h() == 39) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 124 | |
| 125 | print "9. free variable in method" |
| 126 | |
| 127 | def test(): |
| 128 | method_and_var = "var" |
| 129 | class Test: |
| 130 | def method_and_var(self): |
| 131 | return "method" |
| 132 | def test(self): |
| 133 | return method_and_var |
| 134 | def actual_global(self): |
| 135 | return str("global") |
| 136 | def str(self): |
| 137 | return str(self) |
| 138 | return Test() |
| 139 | |
| 140 | t = test() |
| 141 | verify(t.test() == "var") |
| 142 | verify(t.method_and_var() == "method") |
| 143 | verify(t.actual_global() == "global") |
| 144 | |
| 145 | method_and_var = "var" |
| 146 | class Test: |
| 147 | # this class is not nested, so the rules are different |
| 148 | def method_and_var(self): |
| 149 | return "method" |
| 150 | def test(self): |
| 151 | return method_and_var |
| 152 | def actual_global(self): |
| 153 | return str("global") |
| 154 | def str(self): |
| 155 | return str(self) |
| 156 | |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 157 | t = Test() |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 158 | verify(t.test() == "var") |
| 159 | verify(t.method_and_var() == "method") |
| 160 | verify(t.actual_global() == "global") |
| 161 | |
| 162 | print "10. recursion" |
| 163 | |
| 164 | def f(x): |
| 165 | def fact(n): |
| 166 | if n == 0: |
| 167 | return 1 |
| 168 | else: |
| 169 | return n * fact(n - 1) |
| 170 | if x >= 0: |
| 171 | return fact(x) |
| 172 | else: |
| 173 | raise ValueError, "x must be >= 0" |
| 174 | |
| 175 | verify(f(6) == 720) |
| 176 | |
| 177 | |
| 178 | print "11. unoptimized namespaces" |
| 179 | |
| 180 | def check_syntax(s): |
| 181 | try: |
| 182 | compile(s, '?', 'exec') |
| 183 | except SyntaxError: |
| 184 | pass |
| 185 | else: |
| 186 | raise TestFailed |
| 187 | |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 188 | check_syntax("""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 | |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 195 | check_syntax("""def unoptimized_clash2(): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 196 | from string import * |
| 197 | def f(s): |
| 198 | return strip(s) # ambiguity: global or local |
| 199 | return f |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 200 | """) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 201 | |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 202 | check_syntax("""def unoptimized_clash2(): |
| 203 | from string import * |
| 204 | def g(): |
| 205 | def f(s): |
| 206 | return strip(s) # ambiguity: global or local |
| 207 | return f |
| 208 | """) |
| 209 | |
| 210 | # XXX could allow this for exec with const argument, but what's the point |
| 211 | check_syntax("""def error(y): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 212 | exec "a = 1" |
| 213 | def f(x): |
| 214 | return x + y |
| 215 | return f |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 216 | """) |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 217 | |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 218 | check_syntax("""def f(x): |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 219 | def g(): |
| 220 | return x |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 221 | del x # can't del name |
| 222 | """) |
| 223 | |
| 224 | check_syntax("""def f(): |
| 225 | def g(): |
| 226 | from string import * |
| 227 | return strip # global or local? |
| 228 | """) |
| 229 | |
| 230 | # and verify a few cases that should work |
| 231 | |
| 232 | def noproblem1(): |
| 233 | from string import * |
| 234 | f = lambda x:x |
| 235 | |
| 236 | def noproblem2(): |
| 237 | from string import * |
| 238 | def f(x): |
| 239 | return x + 1 |
| 240 | |
| 241 | def noproblem3(): |
| 242 | from string import * |
| 243 | def f(x): |
| 244 | global y |
| 245 | y = x |
Jeremy Hylton | 4588c78 | 2001-01-25 20:11:23 +0000 | [diff] [blame] | 246 | |
| 247 | print "12. lambdas" |
| 248 | |
| 249 | f1 = lambda x: lambda y: x + y |
| 250 | inc = f1(1) |
| 251 | plus10 = f1(10) |
| 252 | verify(inc(1) == 2) |
| 253 | verify(plus10(5) == 15) |
| 254 | |
| 255 | f2 = lambda x: (lambda : lambda y: x + y)() |
| 256 | inc = f2(1) |
| 257 | plus10 = f2(10) |
| 258 | verify(inc(1) == 2) |
| 259 | verify(plus10(5) == 15) |
| 260 | |
| 261 | f3 = lambda x: lambda y: global_x + y |
| 262 | global_x = 1 |
| 263 | inc = f3(None) |
| 264 | verify(inc(2) == 3) |
| 265 | |
| 266 | f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y) |
| 267 | g = f8(1, 2, 3) |
| 268 | h = g(2, 4, 6) |
| 269 | verify(h() == 18) |
Jeremy Hylton | de60248 | 2001-02-05 17:35:20 +0000 | [diff] [blame] | 270 | |
| 271 | print "13. UnboundLocal" |
| 272 | |
| 273 | def errorInOuter(): |
| 274 | print y |
| 275 | def inner(): |
| 276 | return y |
| 277 | y = 1 |
| 278 | |
| 279 | def errorInInner(): |
| 280 | def inner(): |
| 281 | return y |
| 282 | inner() |
| 283 | y = 1 |
| 284 | |
| 285 | try: |
| 286 | errorInOuter() |
| 287 | except UnboundLocalError: |
| 288 | pass |
| 289 | else: |
| 290 | raise TestFailed |
| 291 | |
| 292 | try: |
| 293 | errorInInner() |
| 294 | except UnboundLocalError: |
| 295 | pass |
| 296 | else: |
| 297 | raise TestFailed |
Jeremy Hylton | 97a0167 | 2001-02-09 22:56:46 +0000 | [diff] [blame^] | 298 | |
| 299 | print "14. complex definitions" |
| 300 | |
| 301 | def makeReturner(*lst): |
| 302 | def returner(): |
| 303 | return lst |
| 304 | return returner |
| 305 | |
| 306 | verify(makeReturner(1,2,3)() == (1,2,3)) |
| 307 | |
| 308 | def makeReturner2(**kwargs): |
| 309 | def returner(): |
| 310 | return kwargs |
| 311 | return returner |
| 312 | |
| 313 | verify(makeReturner2(a=11)()['a'] == 11) |
| 314 | |
| 315 | def makeAddPair((a, b)): |
| 316 | def addPair((c, d)): |
| 317 | return (a + c, b + d) |
| 318 | return addPair |
| 319 | |
| 320 | verify(makeAddPair((1, 2))((100, 200)) == (101,202)) |