| 1 |
|
| 2 |
|
| 3 |
def test_simple(): |
| 4 |
import _weakref, gc |
| 5 |
class A(object): |
| 6 |
pass |
| 7 |
a = A() |
| 8 |
assert _weakref.getweakrefcount(a) == 0 |
| 9 |
ref = _weakref.ref(a) |
| 10 |
assert ref() is a |
| 11 |
assert a.__weakref__ is ref |
| 12 |
assert _weakref.getweakrefcount(a) == 1 |
| 13 |
del a |
| 14 |
gc.collect() |
| 15 |
assert ref() is None |
| 16 |
|
| 17 |
def test_missing_arg(): |
| 18 |
import _weakref |
| 19 |
raises(TypeError, _weakref.ref) |
| 20 |
|
| 21 |
def test_no_kwargs(): |
| 22 |
import _weakref |
| 23 |
class C(object): |
| 24 |
pass |
| 25 |
raises(TypeError, _weakref.ref, C(), callback=None) |
| 26 |
|
| 27 |
def test_callback(): |
| 28 |
import _weakref, gc |
| 29 |
class A(object): |
| 30 |
pass |
| 31 |
a1 = A() |
| 32 |
a2 = A() |
| 33 |
def callback(ref): |
| 34 |
a2.ref = ref() |
| 35 |
ref1 = _weakref.ref(a1, callback) |
| 36 |
ref2 = _weakref.ref(a1) |
| 37 |
assert ref1.__callback__ is callback |
| 38 |
assert ref2.__callback__ is None |
| 39 |
assert _weakref.getweakrefcount(a1) == 2 |
| 40 |
del a1 |
| 41 |
gc.collect() |
| 42 |
assert ref1() is None |
| 43 |
assert ref1.__callback__ is None |
| 44 |
assert a2.ref is None |
| 45 |
|
| 46 |
def test_callback_order(): |
| 47 |
import _weakref, gc |
| 48 |
class A(object): |
| 49 |
pass |
| 50 |
a1 = A() |
| 51 |
a2 = A() |
| 52 |
def callback1(ref): |
| 53 |
a2.x = 42 |
| 54 |
def callback2(ref): |
| 55 |
a2.x = 43 |
| 56 |
ref1 = _weakref.ref(a1, callback1) |
| 57 |
ref2 = _weakref.ref(a1, callback2) |
| 58 |
del a1 |
| 59 |
gc.collect() |
| 60 |
assert a2.x == 42 |
| 61 |
|
| 62 |
def test_dont_callback_if_weakref_dead(): |
| 63 |
import _weakref, gc |
| 64 |
class A(object): |
| 65 |
pass |
| 66 |
a1 = A() |
| 67 |
a1.x = 40 |
| 68 |
a2 = A() |
| 69 |
def callback(ref): |
| 70 |
a1.x = 42 |
| 71 |
assert _weakref.getweakrefcount(a2) == 0 |
| 72 |
ref = _weakref.ref(a2, callback) |
| 73 |
assert _weakref.getweakrefcount(a2) == 1 |
| 74 |
ref = None |
| 75 |
gc.collect() |
| 76 |
assert _weakref.getweakrefcount(a2) == 0 |
| 77 |
a2 = None |
| 78 |
gc.collect() |
| 79 |
assert a1.x == 40 |
| 80 |
|
| 81 |
def test_callback_cannot_ressurect(): |
| 82 |
import _weakref, gc |
| 83 |
class A(object): |
| 84 |
pass |
| 85 |
a = A() |
| 86 |
alive = A() |
| 87 |
alive.a = 1 |
| 88 |
def callback(ref2): |
| 89 |
alive.a = ref1() |
| 90 |
ref1 = _weakref.ref(a, callback) |
| 91 |
ref2 = _weakref.ref(a, callback) |
| 92 |
del a |
| 93 |
gc.collect() |
| 94 |
assert alive.a is None |
| 95 |
|
| 96 |
def test_weakref_reusing(): |
| 97 |
import _weakref, gc |
| 98 |
class A(object): |
| 99 |
pass |
| 100 |
a = A() |
| 101 |
ref1 = _weakref.ref(a) |
| 102 |
ref2 = _weakref.ref(a) |
| 103 |
assert ref1 is ref2 |
| 104 |
class wref(_weakref.ref): |
| 105 |
pass |
| 106 |
wref1 = wref(a) |
| 107 |
assert isinstance(wref1, wref) |
| 108 |
|
| 109 |
def test_correct_weakrefcount_after_death(): |
| 110 |
import _weakref, gc |
| 111 |
class A(object): |
| 112 |
pass |
| 113 |
a = A() |
| 114 |
ref1 = _weakref.ref(a) |
| 115 |
ref2 = _weakref.ref(a) |
| 116 |
assert _weakref.getweakrefcount(a) == 1 |
| 117 |
del ref1 |
| 118 |
gc.collect() |
| 119 |
assert _weakref.getweakrefcount(a) == 1 |
| 120 |
del ref2 |
| 121 |
gc.collect() |
| 122 |
assert _weakref.getweakrefcount(a) == 0 |
| 123 |
|
| 124 |
def test_weakref_equality(): |
| 125 |
import _weakref, gc |
| 126 |
class A(object): |
| 127 |
def __eq__(self, other): |
| 128 |
return True |
| 129 |
def __ne__(self, other): |
| 130 |
return False |
| 131 |
a1 = A() |
| 132 |
a2 = A() |
| 133 |
ref1 = _weakref.ref(a1) |
| 134 |
ref2 = _weakref.ref(a2) |
| 135 |
assert ref1 == ref2 |
| 136 |
assert not (ref1 != ref2) |
| 137 |
assert not (ref1 == []) |
| 138 |
assert ref1 != [] |
| 139 |
del a1 |
| 140 |
gc.collect() |
| 141 |
assert not ref1 == ref2 |
| 142 |
assert ref1 != ref2 |
| 143 |
assert not (ref1 == []) |
| 144 |
assert ref1 != [] |
| 145 |
del a2 |
| 146 |
gc.collect() |
| 147 |
assert not ref1 == ref2 |
| 148 |
assert ref1 != ref2 |
| 149 |
assert not (ref1 == []) |
| 150 |
assert ref1 != [] |
| 151 |
|
| 152 |
def test_ne(): |
| 153 |
import _weakref |
| 154 |
class X(object): |
| 155 |
pass |
| 156 |
ref1 = _weakref.ref(X()) |
| 157 |
assert ref1.__eq__(X()) is NotImplemented |
| 158 |
assert ref1.__ne__(X()) is NotImplemented |
| 159 |
|
| 160 |
def test_getweakrefs(): |
| 161 |
import _weakref, gc |
| 162 |
class A(object): |
| 163 |
pass |
| 164 |
a = A() |
| 165 |
assert _weakref.getweakrefs(a) == [] |
| 166 |
assert _weakref.getweakrefs(None) == [] |
| 167 |
ref1 = _weakref.ref(a) |
| 168 |
assert _weakref.getweakrefs(a) == [ref1] |
| 169 |
|
| 170 |
def test_hashing(): |
| 171 |
import _weakref, gc |
| 172 |
class A(object): |
| 173 |
def __hash__(self): |
| 174 |
return 42 |
| 175 |
a = A() |
| 176 |
w = _weakref.ref(a) |
| 177 |
assert hash(a) == hash(w) |
| 178 |
del a |
| 179 |
gc.collect() |
| 180 |
assert hash(w) == 42 |
| 181 |
w = _weakref.ref(A()) |
| 182 |
gc.collect() |
| 183 |
raises(TypeError, hash, w) |
| 184 |
|
| 185 |
def test_weakref_subclassing(): |
| 186 |
import _weakref, gc |
| 187 |
class A(object): |
| 188 |
pass |
| 189 |
class Ref(_weakref.ref): |
| 190 |
def __init__(self, ob, callback=None, **other): |
| 191 |
self.__dict__.update(other) |
| 192 |
def callable(ref): |
| 193 |
b.a = 42 |
| 194 |
a = A() |
| 195 |
b = A() |
| 196 |
b.a = 1 |
| 197 |
w = Ref(a, callable, x=1, y=2) |
| 198 |
assert w.x == 1 |
| 199 |
assert w.y == 2 |
| 200 |
assert a.__weakref__ is w |
| 201 |
assert b.__weakref__ is None |
| 202 |
w1 = _weakref.ref(a) |
| 203 |
w2 = _weakref.ref(a, callable) |
| 204 |
assert a.__weakref__ is w1 |
| 205 |
del a |
| 206 |
gc.collect() |
| 207 |
assert w1() is None |
| 208 |
assert w() is None |
| 209 |
assert w2() is None |
| 210 |
assert b.a == 42 |
| 211 |
|
| 212 |
def test_function_weakrefable(): |
| 213 |
import _weakref, gc |
| 214 |
def f(x): |
| 215 |
return 42 |
| 216 |
wf = _weakref.ref(f) |
| 217 |
assert wf()(63) == 42 |
| 218 |
del f |
| 219 |
gc.collect() |
| 220 |
assert wf() is None |
| 221 |
|
| 222 |
def test_method_weakrefable(): |
| 223 |
import _weakref, gc |
| 224 |
class A(object): |
| 225 |
def f(self): |
| 226 |
return 42 |
| 227 |
a = A() |
| 228 |
meth = A.f |
| 229 |
w_unbound = _weakref.ref(meth) |
| 230 |
assert w_unbound()(A()) == 42 |
| 231 |
meth = A().f |
| 232 |
w_bound = _weakref.ref(meth) |
| 233 |
assert w_bound()() == 42 |
| 234 |
del meth |
| 235 |
gc.collect() |
| 236 |
|
| 237 |
|
| 238 |
assert w_unbound() is A.f |
| 239 |
assert w_bound() is None |
| 240 |
|
| 241 |
def test_set_weakrefable(): |
| 242 |
import _weakref, gc |
| 243 |
s = set([1, 2, 3, 4]) |
| 244 |
w = _weakref.ref(s) |
| 245 |
assert w() is s |
| 246 |
del s |
| 247 |
gc.collect() |
| 248 |
assert w() is None |
| 249 |
|
| 250 |
def test_generator_weakrefable(): |
| 251 |
import _weakref, gc |
| 252 |
def f(x): |
| 253 |
for i in range(x): |
| 254 |
yield i |
| 255 |
g = f(10) |
| 256 |
w = _weakref.ref(g) |
| 257 |
r = next(w()) |
| 258 |
assert r == 0 |
| 259 |
r = next(g) |
| 260 |
assert r == 1 |
| 261 |
del g |
| 262 |
gc.collect() |
| 263 |
assert w() is None |
| 264 |
g = f(10) |
| 265 |
w = _weakref.ref(g) |
| 266 |
assert list(g) == list(range(10)) |
| 267 |
del g |
| 268 |
gc.collect() |
| 269 |
assert w() is None |
| 270 |
|
| 271 |
def test_weakref_subclass_with_del(): |
| 272 |
import _weakref, gc |
| 273 |
class Ref(_weakref.ref): |
| 274 |
def __del__(self): |
| 275 |
b.a = 42 |
| 276 |
class A(object): |
| 277 |
pass |
| 278 |
a = A() |
| 279 |
b = A() |
| 280 |
b.a = 1 |
| 281 |
w = Ref(a) |
| 282 |
del w |
| 283 |
gc.collect() |
| 284 |
assert b.a == 42 |
| 285 |
if _weakref.getweakrefcount(a) > 0: |
| 286 |
|
| 287 |
|
| 288 |
assert _weakref.getweakrefs(a)[0]() is a |
| 289 |
|
| 290 |
def test_buggy_case(): |
| 291 |
import gc, weakref |
| 292 |
gone = [] |
| 293 |
class A(object): |
| 294 |
def __del__(): |
| 295 |
gone.append(True) |
| 296 |
a = A() |
| 297 |
w = weakref.ref(a) |
| 298 |
del a |
| 299 |
tries = 5 |
| 300 |
for i in range(5): |
| 301 |
if not gone: |
| 302 |
gc.collect() |
| 303 |
if gone: |
| 304 |
a1 = w() |
| 305 |
assert a1 is None |
| 306 |
|
| 307 |
def test_del_and_callback_and_id(): |
| 308 |
import gc, weakref |
| 309 |
seen_del = [] |
| 310 |
class A(object): |
| 311 |
acount = 1010 |
| 312 |
def __init__(self): |
| 313 |
self.acount = A.acount |
| 314 |
A.acount += 1 |
| 315 |
def __del__(self): |
| 316 |
seen_del.append(self.acount) |
| 317 |
seen_del.append(w1() is None) |
| 318 |
seen_del.append(w2() is None) |
| 319 |
seen_callback = [] |
| 320 |
def callback(r): |
| 321 |
seen_callback.append(r is w2) |
| 322 |
seen_callback.append(w1() is None) |
| 323 |
seen_callback.append(w2() is None) |
| 324 |
a = A() |
| 325 |
w1 = weakref.ref(a) |
| 326 |
w2 = weakref.ref(a, callback) |
| 327 |
acount = a.acount |
| 328 |
del a |
| 329 |
for i in range(5): |
| 330 |
gc.collect() |
| 331 |
if seen_del: |
| 332 |
assert seen_del == [acount, True, True] |
| 333 |
if seen_callback: |
| 334 |
assert seen_callback == [True, True, True] |
| 335 |
|
| 336 |
def test_type_weakrefable(): |
| 337 |
import _weakref, gc |
| 338 |
w = _weakref.ref(list) |
| 339 |
assert w() is list |
| 340 |
gc.collect() |
| 341 |
assert w() is list |
| 342 |
|
| 343 |
def test_simple(): |
| 344 |
import _weakref, gc |
| 345 |
class A(object): |
| 346 |
def __init__(self, x): |
| 347 |
self.x = x |
| 348 |
a = A(1) |
| 349 |
p = _weakref.proxy(a) |
| 350 |
assert p.x == 1 |
| 351 |
assert str(p) == str(a) |
| 352 |
with raises(TypeError): |
| 353 |
p() |
| 354 |
|
| 355 |
def test_caching(): |
| 356 |
import _weakref, gc |
| 357 |
class A(object): pass |
| 358 |
a = A() |
| 359 |
assert _weakref.proxy(a) is _weakref.proxy(a) |
| 360 |
assert _weakref.proxy(a) is _weakref.proxy(a, None) |
| 361 |
|
| 362 |
def test_callable_proxy(): |
| 363 |
import _weakref, gc |
| 364 |
class A(object): |
| 365 |
def __call__(self): |
| 366 |
global_a.x = 1 |
| 367 |
global_a = A() |
| 368 |
global_a.x = 41 |
| 369 |
A_ = _weakref.proxy(A) |
| 370 |
a = A_() |
| 371 |
assert isinstance(a, A) |
| 372 |
a_ = _weakref.proxy(a) |
| 373 |
a_() |
| 374 |
assert global_a.x == 1 |
| 375 |
|
| 376 |
def test_callable_proxy_type(): |
| 377 |
import _weakref, gc |
| 378 |
class Callable(object): |
| 379 |
def __call__(self, x): |
| 380 |
pass |
| 381 |
o = Callable() |
| 382 |
ref1 = _weakref.proxy(o) |
| 383 |
assert type(ref1) is _weakref.CallableProxyType |
| 384 |
|
| 385 |
def test_dont_create_directly(): |
| 386 |
import _weakref, gc |
| 387 |
raises(TypeError, _weakref.ProxyType, []) |
| 388 |
raises(TypeError, _weakref.CallableProxyType, []) |
| 389 |
|
| 390 |
def test_dont_hash(): |
| 391 |
import _weakref, gc |
| 392 |
class A(object): |
| 393 |
pass |
| 394 |
a = A() |
| 395 |
p = _weakref.proxy(a) |
| 396 |
raises(TypeError, hash, p) |
| 397 |
|
| 398 |
def test_subclassing_not_allowed(): |
| 399 |
import _weakref, gc |
| 400 |
def tryit(): |
| 401 |
class A(_weakref.ProxyType): |
| 402 |
pass |
| 403 |
return A |
| 404 |
raises(TypeError, tryit) |
| 405 |
|
| 406 |
def test_proxy_to_dead_object(): |
| 407 |
import _weakref, gc |
| 408 |
class A(object): |
| 409 |
pass |
| 410 |
p = _weakref.proxy(A()) |
| 411 |
gc.collect() |
| 412 |
with raises(ReferenceError): |
| 413 |
p + 1 |
| 414 |
|
| 415 |
def test_proxy_with_callback(): |
| 416 |
import _weakref, gc |
| 417 |
class A(object): |
| 418 |
pass |
| 419 |
a2 = A() |
| 420 |
def callback(proxy): |
| 421 |
a2.seen = proxy |
| 422 |
p = _weakref.proxy(A(), callback) |
| 423 |
gc.collect() |
| 424 |
with raises(ReferenceError): |
| 425 |
p + 1 |
| 426 |
assert a2.seen is p |
| 427 |
|
| 428 |
def test_repr(): |
| 429 |
import _weakref, gc |
| 430 |
for kind in ('ref', 'proxy'): |
| 431 |
def foobaz(): |
| 432 |
"A random function not returning None." |
| 433 |
return 42 |
| 434 |
w = getattr(_weakref, kind)(foobaz) |
| 435 |
s = repr(w) |
| 436 |
print(s) |
| 437 |
if kind == 'ref': |
| 438 |
assert s.startswith('<weakref.ReferenceType at ') |
| 439 |
else: |
| 440 |
assert (s.startswith('<weakref.CallableProxyType at ') or |
| 441 |
s.startswith('<weakeref.ProxyType at ')) |
| 442 |
assert type(w).__module__ == "weakref" |
| 443 |
assert "function" in s |
| 444 |
del foobaz |
| 445 |
try: |
| 446 |
for i in range(10): |
| 447 |
if w() is None: |
| 448 |
break |
| 449 |
gc.collect() |
| 450 |
except ReferenceError: |
| 451 |
pass |
| 452 |
s = repr(w) |
| 453 |
print(s) |
| 454 |
assert "dead" in s |
| 455 |
|
| 456 |
def test_bytes(): |
| 457 |
import _weakref |
| 458 |
class C(object): |
| 459 |
def __bytes__(self): |
| 460 |
return b"string" |
| 461 |
instance = C() |
| 462 |
assert "__bytes__" in dir(_weakref.proxy(instance)) |
| 463 |
assert bytes(_weakref.proxy(instance)) == b"string" |
| 464 |
|
| 465 |
def test_reversed(): |
| 466 |
import _weakref |
| 467 |
class C(object): |
| 468 |
def __reversed__(self): |
| 469 |
return b"string" |
| 470 |
instance = C() |
| 471 |
assert "__reversed__" in dir(_weakref.proxy(instance)) |
| 472 |
assert reversed(_weakref.proxy(instance)) == b"string" |
| 473 |
|
| 474 |
def test_eq(): |
| 475 |
import _weakref |
| 476 |
class A(object): |
| 477 |
pass |
| 478 |
|
| 479 |
a = A() |
| 480 |
assert not(_weakref.ref(a) == a) |
| 481 |
assert _weakref.ref(a) != a |
| 482 |
|
| 483 |
class A(object): |
| 484 |
def __eq__(self, other): |
| 485 |
return True |
| 486 |
def __ne__(self, other): |
| 487 |
return False |
| 488 |
|
| 489 |
a = A() |
| 490 |
assert _weakref.ref(a) == a |
| 491 |
|
| 492 |
def test_callback_raises(): |
| 493 |
import _weakref, gc |
| 494 |
class A(object): |
| 495 |
pass |
| 496 |
a1 = A() |
| 497 |
def callback(ref): |
| 498 |
explode |
| 499 |
ref1 = _weakref.ref(a1, callback) |
| 500 |
del a1 |
| 501 |
gc.collect() |
| 502 |
assert ref1() is None |
| 503 |
|
| 504 |
def test_init(): |
| 505 |
import _weakref, gc |
| 506 |
|
| 507 |
|
| 508 |
r = _weakref.ref(Exception) |
| 509 |
raises(TypeError, r.__init__, 0, 0, 0, 0, 0) |
| 510 |
|
| 511 |
gc.collect() |
| 512 |
|
| 513 |
def test_add(): |
| 514 |
import _weakref |
| 515 |
class A(object): |
| 516 |
def __add__(self, other): |
| 517 |
return other |
| 518 |
a1 = A() |
| 519 |
a2 = A() |
| 520 |
p1 = _weakref.proxy(a1) |
| 521 |
p2 = _weakref.proxy(a2) |
| 522 |
a3 = p1 + p2 |
| 523 |
assert a3 is a2 |
| 524 |
|
| 525 |
def test_inplace_add(): |
| 526 |
import _weakref |
| 527 |
class A(object): |
| 528 |
def __add__(self, other): |
| 529 |
return other |
| 530 |
a1 = A() |
| 531 |
a2 = A() |
| 532 |
p1 = _weakref.proxy(a1) |
| 533 |
p2 = _weakref.proxy(a2) |
| 534 |
p1 += p2 |
| 535 |
assert p1 is a2 |
| 536 |
|
| 537 |
def test_setattr(): |
| 538 |
import _weakref |
| 539 |
class A(object): |
| 540 |
def __setitem__(self, key, value): |
| 541 |
self.setkey = key |
| 542 |
self.setvalue = value |
| 543 |
a1 = A() |
| 544 |
a2 = A() |
| 545 |
p1 = _weakref.proxy(a1) |
| 546 |
p2 = _weakref.proxy(a2) |
| 547 |
p1[p2] = 42 |
| 548 |
assert a1.setkey is p2 |
| 549 |
assert a1.setvalue == 42 |
| 550 |
|
| 551 |
p1[42] = p2 |
| 552 |
assert a1.setkey == 42 |
| 553 |
assert a1.setvalue is p2 |
| 554 |
|
| 555 |
def test_error_message_wrong_self(): |
| 556 |
import _weakref |
| 557 |
unboundmeth = _weakref.ref.__repr__ |
| 558 |
e = raises(TypeError, unboundmeth, 42) |
| 559 |
assert "weakref" in str(e.value) |
| 560 |
if hasattr(unboundmeth, 'im_func'): |
| 561 |
e = raises(TypeError, unboundmeth.im_func, 42) |
| 562 |
assert "'weakref-or-proxy'" in str(e.value) |
| 563 |
|
| 564 |
def test_reverse_add(): |
| 565 |
import _weakref |
| 566 |
class A: |
| 567 |
def __add__(self, other): |
| 568 |
return 17 |
| 569 |
|
| 570 |
def __radd__(self, other): |
| 571 |
return 20 |
| 572 |
|
| 573 |
def __iadd__(self, other): |
| 574 |
return 19 |
| 575 |
|
| 576 |
assert A() + 12 == 17 |
| 577 |
assert 12 + A() == 20 |
| 578 |
a = A() |
| 579 |
a += -1 |
| 580 |
assert a == 19 |
| 581 |
|
| 582 |
a = A() |
| 583 |
p = _weakref.proxy(a) |
| 584 |
assert p + 12 == 17 |
| 585 |
print(12 + p) |
| 586 |
assert 12 + p == 20 |
| 587 |
p += -1 |
| 588 |
assert p == 19 |
| 589 |
|
| 590 |
def test_gt_lt(): |
| 591 |
import _weakref |
| 592 |
class A: |
| 593 |
def __gt__(self, other): |
| 594 |
return True |
| 595 |
|
| 596 |
def __lt__(self, other): |
| 597 |
return False |
| 598 |
|
| 599 |
assert (A() > 12) == True |
| 600 |
assert (12 > A()) == False |
| 601 |
|
| 602 |
a = A() |
| 603 |
p = _weakref.proxy(a) |
| 604 |
assert (p > 12) == True |
| 605 |
print(12 > p) |
| 606 |
assert (12 > p) == False |
| 607 |
|
| 608 |
def test_remove_dead_weakref(): |
| 609 |
import _weakref, gc |
| 610 |
class A: pass |
| 611 |
a = A() |
| 612 |
r = _weakref.ref(a) |
| 613 |
s = "abc" |
| 614 |
d = {s: r} |
| 615 |
del a |
| 616 |
gc.collect() |
| 617 |
assert r() is None |
| 618 |
_weakref._remove_dead_weakref(d, s) |