| 1 |
from pytest import raises |
| 2 |
import types |
| 3 |
|
| 4 |
def test_nested_scope(): |
| 5 |
x = 42 |
| 6 |
def f(): return x |
| 7 |
assert f() == 42 |
| 8 |
|
| 9 |
def test_nested_scope2(): |
| 10 |
x = 42 |
| 11 |
y = 3 |
| 12 |
def f(): return x |
| 13 |
assert f() == 42 |
| 14 |
|
| 15 |
def test_nested_scope3(): |
| 16 |
x = 42 |
| 17 |
def f(): |
| 18 |
def g(): |
| 19 |
return x |
| 20 |
return g |
| 21 |
assert f()() == 42 |
| 22 |
|
| 23 |
def test_nested_scope4(): |
| 24 |
def f(): |
| 25 |
x = 3 |
| 26 |
def g(): |
| 27 |
return x |
| 28 |
a = g() |
| 29 |
x = 4 |
| 30 |
b = g() |
| 31 |
return (a, b) |
| 32 |
assert f() == (3, 4) |
| 33 |
|
| 34 |
def test_nested_scope_locals(): |
| 35 |
def f(): |
| 36 |
x = 3 |
| 37 |
def g(): |
| 38 |
i = x |
| 39 |
return locals() |
| 40 |
return g() |
| 41 |
d = f() |
| 42 |
assert d == {'i':3, 'x':3} |
| 43 |
|
| 44 |
def test_deeply_nested_scope_locals(): |
| 45 |
def f(): |
| 46 |
x = 3 |
| 47 |
def g(): |
| 48 |
def h(): |
| 49 |
i = x |
| 50 |
return locals() |
| 51 |
return locals(), h() |
| 52 |
return g() |
| 53 |
outer_locals, inner_locals = f() |
| 54 |
assert inner_locals == {'i':3, 'x':3} |
| 55 |
keys = sorted(outer_locals.keys()) |
| 56 |
assert keys == ['h', 'x'] |
| 57 |
|
| 58 |
def test_lambda_in_genexpr(): |
| 59 |
assert [x() for x in (lambda: x for x in range(10))] == list(range(10)) |
| 60 |
|
| 61 |
def test_cell_repr(): |
| 62 |
import re |
| 63 |
from reprlib import repr as r |
| 64 |
|
| 65 |
def get_cell(): |
| 66 |
x = 42 |
| 67 |
def inner(): |
| 68 |
return x |
| 69 |
return inner |
| 70 |
x = get_cell().__closure__[0] |
| 71 |
assert re.match(r'<cell at 0x[0-9A-Fa-f]+: int object at 0x[0-9A-Fa-f]+>', repr(x)) |
| 72 |
assert re.match(r'<cell at 0x.*\.\.\..*>', r(x)) |
| 73 |
|
| 74 |
def get_cell(): |
| 75 |
if False: |
| 76 |
x = 42 |
| 77 |
def inner(): |
| 78 |
return x |
| 79 |
return inner |
| 80 |
x = get_cell().__closure__[0] |
| 81 |
assert re.match(r'<cell at 0x[0-9A-Fa-f]+: empty>', repr(x)) |
| 82 |
|
| 83 |
def test_cell_contents(): |
| 84 |
def f(x): |
| 85 |
def f(y): |
| 86 |
return x + y |
| 87 |
return f |
| 88 |
|
| 89 |
g = f(10) |
| 90 |
assert g.__closure__[0].cell_contents == 10 |
| 91 |
|
| 92 |
def test_set_cell_contents(): |
| 93 |
def f(x): |
| 94 |
def g(y): |
| 95 |
return x + y |
| 96 |
return g |
| 97 |
|
| 98 |
g10 = f(10) |
| 99 |
assert g10(5) == 15 |
| 100 |
assert g10.__closure__[0].cell_contents == 10 |
| 101 |
g10.__closure__[0].cell_contents = 20 |
| 102 |
assert g10.__closure__[0].cell_contents == 20 |
| 103 |
assert g10(5) == 25 |
| 104 |
|
| 105 |
def test_del_cell_contents(): |
| 106 |
def f(x): |
| 107 |
def g(y): |
| 108 |
return x + y |
| 109 |
return g |
| 110 |
|
| 111 |
g10 = f(10) |
| 112 |
del g10.__closure__[0].cell_contents |
| 113 |
with raises(ValueError): |
| 114 |
g10.__closure__[0].cell_contents |
| 115 |
del g10.__closure__[0].cell_contents |
| 116 |
with raises(NameError): |
| 117 |
g10(6) |
| 118 |
|
| 119 |
g10.__closure__[0].cell_contents = 20 |
| 120 |
assert g10(5) == 25 |
| 121 |
|
| 122 |
def test_empty_cell_contents(): |
| 123 |
|
| 124 |
def f(): |
| 125 |
def f(y): |
| 126 |
return x + y |
| 127 |
return f |
| 128 |
x = 1 |
| 129 |
|
| 130 |
g = f() |
| 131 |
with raises(ValueError): |
| 132 |
g.__closure__[0].cell_contents |
| 133 |
|
| 134 |
def test_compare_cells(): |
| 135 |
def f(n): |
| 136 |
if n: |
| 137 |
x = n |
| 138 |
def f(y): |
| 139 |
return x + y |
| 140 |
return f |
| 141 |
|
| 142 |
empty_cell_1 = f(0).__closure__[0] |
| 143 |
empty_cell_2 = f(0).__closure__[0] |
| 144 |
g1 = f(1).__closure__[0] |
| 145 |
g2 = f(2).__closure__[0] |
| 146 |
assert g1 < g2 |
| 147 |
assert g1 <= g2 |
| 148 |
assert g2 > g1 |
| 149 |
assert g2 >= g1 |
| 150 |
assert not g1 == g2 |
| 151 |
assert g1 != g2 |
| 152 |
assert g1 == g1 |
| 153 |
assert not g1 != g1 |
| 154 |
|
| 155 |
assert empty_cell_1 == empty_cell_2 |
| 156 |
assert empty_cell_1 <= empty_cell_2 |
| 157 |
assert empty_cell_1 >= empty_cell_2 |
| 158 |
assert not empty_cell_1 != empty_cell_2 |
| 159 |
assert empty_cell_1 < g1 |
| 160 |
assert not g1 < empty_cell_1 |
| 161 |
assert g1 > empty_cell_1 |
| 162 |
assert not empty_cell_1 > g1 |
| 163 |
assert empty_cell_1 <= g1 |
| 164 |
assert not g1 <= empty_cell_1 |
| 165 |
assert g1 >= empty_cell_1 |
| 166 |
assert not empty_cell_1 >= g1 |
| 167 |
|
| 168 |
def test_leaking_class_locals(): |
| 169 |
def f(x): |
| 170 |
class X: |
| 171 |
x = 12 |
| 172 |
def f(self): |
| 173 |
return x |
| 174 |
locals() |
| 175 |
return X |
| 176 |
assert f(1).x == 12 |
| 177 |
|
| 178 |
def test_nested_scope_locals_mutating_cellvars(): |
| 179 |
def f(): |
| 180 |
x = 12 |
| 181 |
def m(): |
| 182 |
locals() |
| 183 |
x |
| 184 |
locals() |
| 185 |
return x |
| 186 |
return m |
| 187 |
assert f()() == 12 |
| 188 |
|
| 189 |
|
| 190 |
def test_unbound_local_after_del(): |
| 191 |
""" |
| 192 |
# #4617: It is now legal to delete a cell variable. |
| 193 |
# The following functions must obviously compile, |
| 194 |
# and give the correct error when accessing the deleted name. |
| 195 |
def errorInOuter(): |
| 196 |
y = 1 |
| 197 |
del y |
| 198 |
print(y) |
| 199 |
def inner(): |
| 200 |
return y |
| 201 |
|
| 202 |
def errorInInner(): |
| 203 |
def inner(): |
| 204 |
return y |
| 205 |
y = 1 |
| 206 |
del y |
| 207 |
inner() |
| 208 |
|
| 209 |
raises(UnboundLocalError, "errorInOuter()") |
| 210 |
raises(NameError, "errorInInner()") |
| 211 |
""" |
| 212 |
|
| 213 |
def test_new_cell(): |
| 214 |
c = types.CellType(1) |
| 215 |
assert c.cell_contents == 1 |
| 216 |
|
| 217 |
def test_cell_richcmp(): |
| 218 |
class A: |
| 219 |
def __le__(self, other): |
| 220 |
return 12 |
| 221 |
assert (types.CellType(A()) <= types.CellType(A())) == 12 |