| 1 |
import py |
| 2 |
from rpython.annotator.model import UnionError |
| 3 |
from rpython.rlib import rgc, nonconst |
| 4 |
from rpython.rlib.rweakref import RWeakValueDictionary |
| 5 |
from rpython.rtyper.test.test_llinterp import interpret |
| 6 |
from rpython.translator.c.test.test_genc import compile |
| 7 |
|
| 8 |
class X(object): |
| 9 |
pass |
| 10 |
|
| 11 |
class Y(X): |
| 12 |
pass |
| 13 |
|
| 14 |
|
| 15 |
def make_test(loop=100, keyclass=str): |
| 16 |
if keyclass is str: |
| 17 |
make_key = str |
| 18 |
keys = ["abc", "def", "ghi", "hello"] |
| 19 |
elif keyclass is int: |
| 20 |
make_key = int |
| 21 |
keys = [123, 456, 789, 1234] |
| 22 |
|
| 23 |
def g(d): |
| 24 |
assert d.get(keys[3]) is None |
| 25 |
x1 = X(); x2 = X(); x3 = X() |
| 26 |
d.set(keys[0], x1) |
| 27 |
d.set(keys[1], x2) |
| 28 |
d.set(keys[2], x3) |
| 29 |
assert d.get(keys[0]) is x1 |
| 30 |
assert d.get(keys[1]) is x2 |
| 31 |
assert d.get(keys[2]) is x3 |
| 32 |
assert d.get(keys[3]) is None |
| 33 |
return x1, x3 |
| 34 |
def f(): |
| 35 |
d = RWeakValueDictionary(keyclass, X) |
| 36 |
x1, x3 = g(d) |
| 37 |
rgc.collect(); rgc.collect() |
| 38 |
assert d.get(keys[0]) is x1 |
| 39 |
assert d.get(keys[1]) is None |
| 40 |
assert d.get(keys[2]) is x3 |
| 41 |
assert d.get(keys[3]) is None |
| 42 |
d.set(keys[0], None) |
| 43 |
assert d.get(keys[0]) is None |
| 44 |
assert d.get(keys[1]) is None |
| 45 |
assert d.get(keys[2]) is x3 |
| 46 |
assert d.get(keys[3]) is None |
| 47 |
|
| 48 |
for i in range(loop): |
| 49 |
d.set(make_key(i), x1) |
| 50 |
for i in range(loop): |
| 51 |
assert d.get(make_key(i)) is x1 |
| 52 |
assert d.get(keys[0]) is None |
| 53 |
assert d.get(keys[1]) is None |
| 54 |
assert d.get(keys[2]) is x3 |
| 55 |
assert d.get(keys[3]) is None |
| 56 |
|
| 57 |
y = Y() |
| 58 |
d.set(keys[3], y) |
| 59 |
assert d.get(keys[3]) is y |
| 60 |
|
| 61 |
for i in range(loop, loop*2-5): |
| 62 |
d.set(make_key(1000 + i), x1) |
| 63 |
for i in range(loop): |
| 64 |
d.set(make_key(i), None) |
| 65 |
for i in range(loop): |
| 66 |
assert d.get(make_key(i)) is None |
| 67 |
assert d.get(keys[0]) is None |
| 68 |
assert d.get(keys[1]) is None |
| 69 |
assert d.get(keys[2]) is x3 |
| 70 |
assert d.get(keys[3]) is y |
| 71 |
for i in range(loop, loop*2-5): |
| 72 |
assert d.get(make_key(1000 + i)) is x1 |
| 73 |
return f |
| 74 |
|
| 75 |
def test_RWeakValueDictionary(): |
| 76 |
make_test()() |
| 77 |
|
| 78 |
def test_RWeakValueDictionary_int(): |
| 79 |
make_test(keyclass=int)() |
| 80 |
|
| 81 |
def test_rpython_RWeakValueDictionary(): |
| 82 |
interpret(make_test(loop=12), []) |
| 83 |
|
| 84 |
def test_rpython_RWeakValueDictionary_int(): |
| 85 |
interpret(make_test(loop=12, keyclass=int), []) |
| 86 |
|
| 87 |
def test_rpython_prebuilt(): |
| 88 |
d = RWeakValueDictionary(str, X) |
| 89 |
living = [X() for i in range(8)] |
| 90 |
for i in range(8): |
| 91 |
d.set(str(i), living[i]) |
| 92 |
|
| 93 |
def f(): |
| 94 |
x = X() |
| 95 |
for i in range(8, 13): |
| 96 |
d.set(str(i), x) |
| 97 |
for i in range(0, 8): |
| 98 |
assert d.get(str(i)) is living[i] |
| 99 |
for i in range(8, 13): |
| 100 |
assert d.get(str(i)) is x |
| 101 |
assert d.get("foobar") is None |
| 102 |
|
| 103 |
f() |
| 104 |
interpret(f, []) |
| 105 |
|
| 106 |
def test_rpython_merge_RWeakValueDictionary(): |
| 107 |
empty = RWeakValueDictionary(str, X) |
| 108 |
def f(n): |
| 109 |
x = X() |
| 110 |
if n: |
| 111 |
d = empty |
| 112 |
else: |
| 113 |
d = RWeakValueDictionary(str, X) |
| 114 |
d.set("a", x) |
| 115 |
return d.get("a") is x |
| 116 |
assert f(0) |
| 117 |
assert interpret(f, [0]) |
| 118 |
assert not f(1) |
| 119 |
assert not interpret(f, [1]) |
| 120 |
|
| 121 |
|
| 122 |
def test_rpython_merge_RWeakValueDictionary2(): |
| 123 |
class A(object): |
| 124 |
def __init__(self): |
| 125 |
self.d = RWeakValueDictionary(str, A) |
| 126 |
def f(self, key): |
| 127 |
a = A() |
| 128 |
self.d.set(key, a) |
| 129 |
return a |
| 130 |
empty = A() |
| 131 |
def f(x): |
| 132 |
a = A() |
| 133 |
if x: |
| 134 |
a = empty |
| 135 |
a2 = a.f("a") |
| 136 |
assert a.d.get("a") is a2 |
| 137 |
f(0) |
| 138 |
interpret(f, [0]) |
| 139 |
f(1) |
| 140 |
interpret(f, [1]) |
| 141 |
|
| 142 |
|
| 143 |
@py.test.mark.xfail( |
| 144 |
reason="may fail with AssertionError, depending on annotation order") |
| 145 |
def test_rpython_merge_RWeakValueDictionary3(): |
| 146 |
def g(x): |
| 147 |
if x: |
| 148 |
d = RWeakValueDictionary(str, X) |
| 149 |
else: |
| 150 |
d = RWeakValueDictionary(str, Y) |
| 151 |
d.set("x", X()) |
| 152 |
|
| 153 |
with py.test.raises(UnionError): |
| 154 |
interpret(g, [1]) |
| 155 |
|
| 156 |
|
| 157 |
def test_rpython_RWeakValueDictionary_or_None(): |
| 158 |
def g(d, key): |
| 159 |
if d is None: |
| 160 |
return None |
| 161 |
return d.get(key) |
| 162 |
def f(n): |
| 163 |
x = X() |
| 164 |
if n: |
| 165 |
d = None |
| 166 |
else: |
| 167 |
d = RWeakValueDictionary(str, X) |
| 168 |
d.set("a", x) |
| 169 |
return g(d, "a") is x |
| 170 |
assert f(0) |
| 171 |
assert interpret(f, [0]) |
| 172 |
assert not f(1) |
| 173 |
assert not interpret(f, [1]) |
| 174 |
|
| 175 |
|
| 176 |
def test_bogus_makekey(): |
| 177 |
class X: pass |
| 178 |
class Y: pass |
| 179 |
def g(): |
| 180 |
X(); Y() |
| 181 |
RWeakValueDictionary(str, X).get("foobar") |
| 182 |
RWeakValueDictionary(int, Y).get(42) |
| 183 |
interpret(g, []) |
| 184 |
|
| 185 |
def test_key_instance(): |
| 186 |
class K(object): |
| 187 |
pass |
| 188 |
keys = [K(), K(), K()] |
| 189 |
|
| 190 |
def g(d): |
| 191 |
assert d.get(keys[3]) is None |
| 192 |
x1 = X(); x2 = X(); x3 = X() |
| 193 |
d.set(keys[0], x1) |
| 194 |
d.set(keys[1], x2) |
| 195 |
d.set(keys[2], x3) |
| 196 |
assert d.get(keys[0]) is x1 |
| 197 |
assert d.get(keys[1]) is x2 |
| 198 |
assert d.get(keys[2]) is x3 |
| 199 |
assert d.get(keys[3]) is None |
| 200 |
return x1, x3 |
| 201 |
def f(): |
| 202 |
keys.append(K()) |
| 203 |
d = RWeakValueDictionary(K, X) |
| 204 |
x1, x3 = g(d) |
| 205 |
rgc.collect(); rgc.collect() |
| 206 |
assert d.get(keys[0]) is x1 |
| 207 |
assert d.get(keys[1]) is None |
| 208 |
assert d.get(keys[2]) is x3 |
| 209 |
assert d.get(keys[3]) is None |
| 210 |
d.set(keys[0], None) |
| 211 |
assert d.get(keys[0]) is None |
| 212 |
assert d.get(keys[1]) is None |
| 213 |
assert d.get(keys[2]) is x3 |
| 214 |
assert d.get(keys[3]) is None |
| 215 |
f() |
| 216 |
interpret(f, []) |
| 217 |
|
| 218 |
def test_translation_prebuilt_1(): |
| 219 |
class K: |
| 220 |
pass |
| 221 |
d = RWeakValueDictionary(K, X) |
| 222 |
k1 = K(); k2 = K() |
| 223 |
x1 = X(); x2 = X() |
| 224 |
d.set(k1, x1) |
| 225 |
d.set(k2, x2) |
| 226 |
def f(): |
| 227 |
assert d.get(k1) is x1 |
| 228 |
assert d.get(k2) is x2 |
| 229 |
f() |
| 230 |
fc = compile(f, [], gcpolicy="boehm", rweakref=True) |
| 231 |
fc() |
| 232 |
|
| 233 |
def test_translation_prebuilt_2(): |
| 234 |
from rpython.rlib import rsiphash |
| 235 |
d = RWeakValueDictionary(str, X) |
| 236 |
k1 = "key1"; k2 = "key2" |
| 237 |
x1 = X(); x2 = X() |
| 238 |
d.set(k1, x1) |
| 239 |
d.set(k2, x2) |
| 240 |
def f(): |
| 241 |
rsiphash.enable_siphash24() |
| 242 |
i = nonconst.NonConstant(1) |
| 243 |
assert d.get("key%d" % (i,)) is x1 |
| 244 |
assert d.get("key%d" % (i+1,)) is x2 |
| 245 |
fc = compile(f, [], gcpolicy="boehm", rweakref=True) |
| 246 |
fc() |