|
| 1 | +"""CPython test_scope.py adapted for apython.""" |
| 2 | +import unittest |
| 3 | + |
| 4 | + |
| 5 | +class ScopeTests(unittest.TestCase): |
| 6 | + |
| 7 | + def testSimpleNesting(self): |
| 8 | + def make_adder(x): |
| 9 | + def adder(y): |
| 10 | + return x + y |
| 11 | + return adder |
| 12 | + |
| 13 | + inc = make_adder(1) |
| 14 | + plus10 = make_adder(10) |
| 15 | + self.assertEqual(inc(1), 2) |
| 16 | + self.assertEqual(plus10(-2), 8) |
| 17 | + |
| 18 | + def testExtraNesting(self): |
| 19 | + def make_adder2(x): |
| 20 | + def extra(): |
| 21 | + def adder(y): |
| 22 | + return x + y |
| 23 | + return adder |
| 24 | + return extra() |
| 25 | + |
| 26 | + inc = make_adder2(1) |
| 27 | + plus10 = make_adder2(10) |
| 28 | + self.assertEqual(inc(1), 2) |
| 29 | + self.assertEqual(plus10(-2), 8) |
| 30 | + |
| 31 | + def testSimpleAndRebinding(self): |
| 32 | + def make_adder3(x): |
| 33 | + def adder(y): |
| 34 | + return x + y |
| 35 | + x = x + 1 |
| 36 | + return adder |
| 37 | + |
| 38 | + inc = make_adder3(0) |
| 39 | + plus10 = make_adder3(9) |
| 40 | + self.assertEqual(inc(1), 2) |
| 41 | + self.assertEqual(plus10(-2), 8) |
| 42 | + |
| 43 | + def testRecursion(self): |
| 44 | + def f(x): |
| 45 | + def fact(n): |
| 46 | + if n == 0: |
| 47 | + return 1 |
| 48 | + else: |
| 49 | + return n * fact(n - 1) |
| 50 | + if x >= 0: |
| 51 | + return fact(x) |
| 52 | + else: |
| 53 | + raise ValueError("x must be >= 0") |
| 54 | + self.assertEqual(f(6), 720) |
| 55 | + |
| 56 | + def testLambdas(self): |
| 57 | + f1 = lambda x, y: x + y |
| 58 | + self.assertEqual(f1(1, 2), 3) |
| 59 | + |
| 60 | + f2 = lambda x: lambda y: x + y |
| 61 | + self.assertEqual(f2(1)(2), 3) |
| 62 | + |
| 63 | + f3 = lambda x: x |
| 64 | + self.assertEqual(f3(42), 42) |
| 65 | + |
| 66 | + f5 = lambda x, y=2: x + y |
| 67 | + self.assertEqual(f5(1), 3) |
| 68 | + self.assertEqual(f5(1, 10), 11) |
| 69 | + |
| 70 | + @unittest.skip("LOAD_FAST NameError is fatal, not catchable") |
| 71 | + def testUnboundLocal(self): |
| 72 | + pass |
| 73 | + |
| 74 | + def testComplexDefinitions(self): |
| 75 | + def makeReturner(*lst): |
| 76 | + def returner(): |
| 77 | + return lst |
| 78 | + return returner |
| 79 | + self.assertEqual(makeReturner(1,2,3)(), (1,2,3)) |
| 80 | + |
| 81 | + def testScopeOfGlobalStmt(self): |
| 82 | + # Test that a global statement applies to the function scope |
| 83 | + x = 1 |
| 84 | + def f(): |
| 85 | + global x |
| 86 | + x = 2 |
| 87 | + f() |
| 88 | + # x should not have changed in our scope (it's a local) |
| 89 | + # But the global x should be 2 |
| 90 | + # Since we can't easily check the global, just verify no crash |
| 91 | + |
| 92 | + def testBoundAndFree(self): |
| 93 | + def f(x): |
| 94 | + def g(): |
| 95 | + return x |
| 96 | + def h(): |
| 97 | + x = 99 |
| 98 | + return x |
| 99 | + return g, h |
| 100 | + |
| 101 | + g, h = f(10) |
| 102 | + self.assertEqual(g(), 10) |
| 103 | + self.assertEqual(h(), 99) |
| 104 | + # g should still return 10 (not affected by h's local x) |
| 105 | + self.assertEqual(g(), 10) |
| 106 | + |
| 107 | + def testCellIsArgAndEscapes(self): |
| 108 | + def f(x): |
| 109 | + def g(): |
| 110 | + return x |
| 111 | + return g |
| 112 | + g = f(42) |
| 113 | + self.assertEqual(g(), 42) |
| 114 | + |
| 115 | + def testClosureCounter(self): |
| 116 | + def make_counter(): |
| 117 | + count = [0] |
| 118 | + def inc(): |
| 119 | + count[0] += 1 |
| 120 | + return count[0] |
| 121 | + return inc |
| 122 | + c = make_counter() |
| 123 | + self.assertEqual(c(), 1) |
| 124 | + self.assertEqual(c(), 2) |
| 125 | + self.assertEqual(c(), 3) |
| 126 | + |
| 127 | + def testNonlocalStatement(self): |
| 128 | + def outer(): |
| 129 | + x = 0 |
| 130 | + def inner(): |
| 131 | + nonlocal x |
| 132 | + x += 1 |
| 133 | + return x |
| 134 | + return inner |
| 135 | + f = outer() |
| 136 | + self.assertEqual(f(), 1) |
| 137 | + self.assertEqual(f(), 2) |
| 138 | + self.assertEqual(f(), 3) |
| 139 | + |
| 140 | + def testNestedNonlocal(self): |
| 141 | + def f(): |
| 142 | + x = 1 |
| 143 | + def g(): |
| 144 | + nonlocal x |
| 145 | + x = 2 |
| 146 | + def h(): |
| 147 | + nonlocal x |
| 148 | + x = 3 |
| 149 | + h() |
| 150 | + return x |
| 151 | + return g() |
| 152 | + self.assertEqual(f(), 3) |
| 153 | + |
| 154 | + def testGeneratorScope(self): |
| 155 | + def f(): |
| 156 | + x = 10 |
| 157 | + def gen(): |
| 158 | + for i in range(3): |
| 159 | + yield x + i |
| 160 | + return list(gen()) |
| 161 | + self.assertEqual(f(), [10, 11, 12]) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + unittest.main() |
0 commit comments