| 1 |
import gc |
| 2 |
import sys |
| 3 |
import doctest |
| 4 |
import unittest |
| 5 |
import collections |
| 6 |
import weakref |
| 7 |
import operator |
| 8 |
import contextlib |
| 9 |
import copy |
| 10 |
import threading |
| 11 |
import time |
| 12 |
import random |
| 13 |
import textwrap |
| 14 |
|
| 15 |
from test import support |
| 16 |
from test.support import script_helper, ALWAYS_EQ |
| 17 |
from test.support import gc_collect |
| 18 |
from test.support import import_helper |
| 19 |
from test.support import threading_helper |
| 20 |
from test.support import is_wasi, Py_DEBUG |
| 21 |
|
| 22 |
# Used in ReferencesTestCase.test_ref_created_during_del() . |
| 23 |
ref_from_del = None |
| 24 |
|
| 25 |
# Used by FinalizeTestCase as a global that may be replaced by None |
| 26 |
# when the interpreter shuts down. |
| 27 |
_global_var = 'foobar' |
| 28 |
|
| 29 |
class C: |
| 30 |
def method(self): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class Callable: |
| 35 |
bar = None |
| 36 |
|
| 37 |
def __call__(self, x): |
| 38 |
self.bar = x |
| 39 |
|
| 40 |
|
| 41 |
def create_function(): |
| 42 |
def f(): pass |
| 43 |
return f |
| 44 |
|
| 45 |
def create_bound_method(): |
| 46 |
return C().method |
| 47 |
|
| 48 |
|
| 49 |
class Object: |
| 50 |
def __init__(self, arg): |
| 51 |
self.arg = arg |
| 52 |
def __repr__(self): |
| 53 |
return "<Object %r>" % self.arg |
| 54 |
def __eq__(self, other): |
| 55 |
if isinstance(other, Object): |
| 56 |
return self.arg == other.arg |
| 57 |
return NotImplemented |
| 58 |
def __lt__(self, other): |
| 59 |
if isinstance(other, Object): |
| 60 |
return self.arg < other.arg |
| 61 |
return NotImplemented |
| 62 |
def __hash__(self): |
| 63 |
return hash(self.arg) |
| 64 |
def some_method(self): |
| 65 |
return 4 |
| 66 |
def other_method(self): |
| 67 |
return 5 |
| 68 |
|
| 69 |
|
| 70 |
class RefCycle: |
| 71 |
def __init__(self): |
| 72 |
self.cycle = self |
| 73 |
|
| 74 |
|
| 75 |
class TestBase(unittest.TestCase): |
| 76 |
|
| 77 |
def setUp(self): |
| 78 |
self.cbcalled = 0 |
| 79 |
|
| 80 |
def callback(self, ref): |
| 81 |
self.cbcalled += 1 |
| 82 |
|
| 83 |
|
| 84 |
@contextlib.contextmanager |
| 85 |
def collect_in_thread(period=0.005): |
| 86 |
""" |
| 87 |
Ensure GC collections happen in a different thread, at a high frequency. |
| 88 |
""" |
| 89 |
please_stop = False |
| 90 |
|
| 91 |
def collect(): |
| 92 |
while not please_stop: |
| 93 |
time.sleep(period) |
| 94 |
gc.collect() |
| 95 |
|
| 96 |
with support.disable_gc(): |
| 97 |
t = threading.Thread(target=collect) |
| 98 |
t.start() |
| 99 |
try: |
| 100 |
yield |
| 101 |
finally: |
| 102 |
please_stop = True |
| 103 |
t.join() |
| 104 |
|
| 105 |
|
| 106 |
class ReferencesTestCase(TestBase): |
| 107 |
|
| 108 |
def test_basic_ref(self): |
| 109 |
self.check_basic_ref(C) |
| 110 |
self.check_basic_ref(create_function) |
| 111 |
self.check_basic_ref(create_bound_method) |
| 112 |
|
| 113 |
# Just make sure the tp_repr handler doesn't raise an exception. |
| 114 |
# Live reference: |
| 115 |
o = C() |
| 116 |
wr = weakref.ref(o) |
| 117 |
repr(wr) |
| 118 |
# Dead reference: |
| 119 |
del o |
| 120 |
repr(wr) |
| 121 |
|
| 122 |
@support.cpython_only |
| 123 |
def test_ref_repr(self): |
| 124 |
obj = C() |
| 125 |
ref = weakref.ref(obj) |
| 126 |
regex = ( |
| 127 |
rf"<weakref at 0x[0-9a-fA-F]+; " |
| 128 |
rf"to '{'' if __name__ == '__main__' else C.__module__ + '.'}{C.__qualname__}' " |
| 129 |
rf"at 0x[0-9a-fA-F]+>" |
| 130 |
) |
| 131 |
self.assertRegex(repr(ref), regex) |
| 132 |
|
| 133 |
obj = None |
| 134 |
gc_collect() |
| 135 |
self.assertRegex(repr(ref), |
| 136 |
rf'<weakref at 0x[0-9a-fA-F]+; dead>') |
| 137 |
|
| 138 |
# test type with __name__ |
| 139 |
class WithName: |
| 140 |
@property |
| 141 |
def __name__(self): |
| 142 |
return "custom_name" |
| 143 |
|
| 144 |
obj2 = WithName() |
| 145 |
ref2 = weakref.ref(obj2) |
| 146 |
regex = ( |
| 147 |
rf"<weakref at 0x[0-9a-fA-F]+; " |
| 148 |
rf"to '{'' if __name__ == '__main__' else WithName.__module__ + '.'}" |
| 149 |
rf"{WithName.__qualname__}' " |
| 150 |
rf"at 0x[0-9a-fA-F]+ +\(custom_name\)>" |
| 151 |
) |
| 152 |
self.assertRegex(repr(ref2), regex) |
| 153 |
|
| 154 |
def test_repr_failure_gh99184(self): |
| 155 |
class MyConfig(dict): |
| 156 |
def __getattr__(self, x): |
| 157 |
return self[x] |
| 158 |
|
| 159 |
obj = MyConfig(offset=5) |
| 160 |
obj_weakref = weakref.ref(obj) |
| 161 |
|
| 162 |
self.assertIn('MyConfig', repr(obj_weakref)) |
| 163 |
self.assertIn('MyConfig', str(obj_weakref)) |
| 164 |
|
| 165 |
def test_basic_callback(self): |
| 166 |
self.check_basic_callback(C) |
| 167 |
self.check_basic_callback(create_function) |
| 168 |
self.check_basic_callback(create_bound_method) |
| 169 |
|
| 170 |
@support.cpython_only |
| 171 |
def test_cfunction(self): |
| 172 |
_testcapi = import_helper.import_module("_testcapi") |
| 173 |
create_cfunction = _testcapi.create_cfunction |
| 174 |
f = create_cfunction() |
| 175 |
wr = weakref.ref(f) |
| 176 |
self.assertIs(wr(), f) |
| 177 |
del f |
| 178 |
self.assertIsNone(wr()) |
| 179 |
self.check_basic_ref(create_cfunction) |
| 180 |
self.check_basic_callback(create_cfunction) |
| 181 |
|
| 182 |
def test_multiple_callbacks(self): |
| 183 |
o = C() |
| 184 |
ref1 = weakref.ref(o, self.callback) |
| 185 |
ref2 = weakref.ref(o, self.callback) |
| 186 |
del o |
| 187 |
gc_collect() # For PyPy or other GCs. |
| 188 |
self.assertIsNone(ref1(), "expected reference to be invalidated") |
| 189 |
self.assertIsNone(ref2(), "expected reference to be invalidated") |
| 190 |
self.assertEqual(self.cbcalled, 2, |
| 191 |
"callback not called the right number of times") |
| 192 |
|
| 193 |
def test_multiple_selfref_callbacks(self): |
| 194 |
# Make sure all references are invalidated before callbacks are called |
| 195 |
# |
| 196 |
# What's important here is that we're using the first |
| 197 |
# reference in the callback invoked on the second reference |
| 198 |
# (the most recently created ref is cleaned up first). This |
| 199 |
# tests that all references to the object are invalidated |
| 200 |
# before any of the callbacks are invoked, so that we only |
| 201 |
# have one invocation of _weakref.c:cleanup_helper() active |
| 202 |
# for a particular object at a time. |
| 203 |
# |
| 204 |
def callback(object, self=self): |
| 205 |
self.ref() |
| 206 |
c = C() |
| 207 |
self.ref = weakref.ref(c, callback) |
| 208 |
ref1 = weakref.ref(c, callback) |
| 209 |
del c |
| 210 |
|
| 211 |
def test_constructor_kwargs(self): |
| 212 |
c = C() |
| 213 |
self.assertRaises(TypeError, weakref.ref, c, callback=None) |
| 214 |
|
| 215 |
def test_proxy_ref(self): |
| 216 |
o = C() |
| 217 |
o.bar = 1 |
| 218 |
ref1 = weakref.proxy(o, self.callback) |
| 219 |
ref2 = weakref.proxy(o, self.callback) |
| 220 |
del o |
| 221 |
gc_collect() # For PyPy or other GCs. |
| 222 |
|
| 223 |
def check(proxy): |
| 224 |
proxy.bar |
| 225 |
|
| 226 |
self.assertRaises(ReferenceError, check, ref1) |
| 227 |
self.assertRaises(ReferenceError, check, ref2) |
| 228 |
ref3 = weakref.proxy(C()) |
| 229 |
gc_collect() # For PyPy or other GCs. |
| 230 |
self.assertRaises(ReferenceError, bool, ref3) |
| 231 |
self.assertEqual(self.cbcalled, 2) |
| 232 |
|
| 233 |
@support.cpython_only |
| 234 |
def test_proxy_repr(self): |
| 235 |
obj = C() |
| 236 |
ref = weakref.proxy(obj, self.callback) |
| 237 |
regex = ( |
| 238 |
rf"<weakproxy at 0x[0-9a-fA-F]+; " |
| 239 |
rf"to '{'' if __name__ == '__main__' else C.__module__ + '.'}{C.__qualname__}' " |
| 240 |
rf"at 0x[0-9a-fA-F]+>" |
| 241 |
) |
| 242 |
self.assertRegex(repr(ref), regex) |
| 243 |
|
| 244 |
obj = None |
| 245 |
gc_collect() |
| 246 |
self.assertRegex(repr(ref), |
| 247 |
rf'<weakproxy at 0x[0-9a-fA-F]+; dead>') |
| 248 |
|
| 249 |
def check_basic_ref(self, factory): |
| 250 |
o = factory() |
| 251 |
ref = weakref.ref(o) |
| 252 |
self.assertIsNotNone(ref(), |
| 253 |
"weak reference to live object should be live") |
| 254 |
o2 = ref() |
| 255 |
self.assertIs(o, o2, |
| 256 |
"<ref>() should return original object if live") |
| 257 |
|
| 258 |
def check_basic_callback(self, factory): |
| 259 |
self.cbcalled = 0 |
| 260 |
o = factory() |
| 261 |
ref = weakref.ref(o, self.callback) |
| 262 |
del o |
| 263 |
gc_collect() # For PyPy or other GCs. |
| 264 |
self.assertEqual(self.cbcalled, 1, |
| 265 |
"callback did not properly set 'cbcalled'") |
| 266 |
self.assertIsNone(ref(), |
| 267 |
"ref2 should be dead after deleting object reference") |
| 268 |
|
| 269 |
def test_ref_reuse(self): |
| 270 |
o = C() |
| 271 |
ref1 = weakref.ref(o) |
| 272 |
# create a proxy to make sure that there's an intervening creation |
| 273 |
# between these two; it should make no difference |
| 274 |
proxy = weakref.proxy(o) |
| 275 |
ref2 = weakref.ref(o) |
| 276 |
self.assertIs(ref1, ref2, |
| 277 |
"reference object w/out callback should be re-used") |
| 278 |
|
| 279 |
o = C() |
| 280 |
proxy = weakref.proxy(o) |
| 281 |
ref1 = weakref.ref(o) |
| 282 |
ref2 = weakref.ref(o) |
| 283 |
self.assertIs(ref1, ref2, |
| 284 |
"reference object w/out callback should be re-used") |
| 285 |
self.assertEqual(weakref.getweakrefcount(o), 2, |
| 286 |
"wrong weak ref count for object") |
| 287 |
del proxy |
| 288 |
gc_collect() # For PyPy or other GCs. |
| 289 |
self.assertEqual(weakref.getweakrefcount(o), 1, |
| 290 |
"wrong weak ref count for object after deleting proxy") |
| 291 |
|
| 292 |
def test_proxy_reuse(self): |
| 293 |
o = C() |
| 294 |
proxy1 = weakref.proxy(o) |
| 295 |
ref = weakref.ref(o) |
| 296 |
proxy2 = weakref.proxy(o) |
| 297 |
self.assertIs(proxy1, proxy2, |
| 298 |
"proxy object w/out callback should have been re-used") |
| 299 |
|
| 300 |
def test_basic_proxy(self): |
| 301 |
o = C() |
| 302 |
self.check_proxy(o, weakref.proxy(o)) |
| 303 |
|
| 304 |
L = collections.UserList() |
| 305 |
p = weakref.proxy(L) |
| 306 |
self.assertFalse(p, "proxy for empty UserList should be false") |
| 307 |
p.append(12) |
| 308 |
self.assertEqual(len(L), 1) |
| 309 |
self.assertTrue(p, "proxy for non-empty UserList should be true") |
| 310 |
p[:] = [2, 3] |
| 311 |
self.assertEqual(len(L), 2) |
| 312 |
self.assertEqual(len(p), 2) |
| 313 |
self.assertIn(3, p, "proxy didn't support __contains__() properly") |
| 314 |
p[1] = 5 |
| 315 |
self.assertEqual(L[1], 5) |
| 316 |
self.assertEqual(p[1], 5) |
| 317 |
L2 = collections.UserList(L) |
| 318 |
p2 = weakref.proxy(L2) |
| 319 |
self.assertEqual(p, p2) |
| 320 |
## self.assertEqual(repr(L2), repr(p2)) |
| 321 |
L3 = collections.UserList(range(10)) |
| 322 |
p3 = weakref.proxy(L3) |
| 323 |
self.assertEqual(L3[:], p3[:]) |
| 324 |
self.assertEqual(L3[5:], p3[5:]) |
| 325 |
self.assertEqual(L3[:5], p3[:5]) |
| 326 |
self.assertEqual(L3[2:5], p3[2:5]) |
| 327 |
|
| 328 |
def test_proxy_unicode(self): |
| 329 |
# See bug 5037 |
| 330 |
class C(object): |
| 331 |
def __str__(self): |
| 332 |
return "string" |
| 333 |
def __bytes__(self): |
| 334 |
return b"bytes" |
| 335 |
instance = C() |
| 336 |
self.assertIn("__bytes__", dir(weakref.proxy(instance))) |
| 337 |
self.assertEqual(bytes(weakref.proxy(instance)), b"bytes") |
| 338 |
|
| 339 |
def test_proxy_index(self): |
| 340 |
class C: |
| 341 |
def __index__(self): |
| 342 |
return 10 |
| 343 |
o = C() |
| 344 |
p = weakref.proxy(o) |
| 345 |
self.assertEqual(operator.index(p), 10) |
| 346 |
|
| 347 |
def test_proxy_div(self): |
| 348 |
class C: |
| 349 |
def __floordiv__(self, other): |
| 350 |
return 42 |
| 351 |
def __ifloordiv__(self, other): |
| 352 |
return 21 |
| 353 |
o = C() |
| 354 |
p = weakref.proxy(o) |
| 355 |
self.assertEqual(p // 5, 42) |
| 356 |
p //= 5 |
| 357 |
self.assertEqual(p, 21) |
| 358 |
|
| 359 |
def test_proxy_matmul(self): |
| 360 |
class C: |
| 361 |
def __matmul__(self, other): |
| 362 |
return 1729 |
| 363 |
def __rmatmul__(self, other): |
| 364 |
return -163 |
| 365 |
def __imatmul__(self, other): |
| 366 |
return 561 |
| 367 |
o = C() |
| 368 |
p = weakref.proxy(o) |
| 369 |
self.assertEqual(p @ 5, 1729) |
| 370 |
self.assertEqual(5 @ p, -163) |
| 371 |
p @= 5 |
| 372 |
self.assertEqual(p, 561) |
| 373 |
|
| 374 |
# The PyWeakref_* C API is documented as allowing either NULL or |
| 375 |
# None as the value for the callback, where either means "no |
| 376 |
# callback". The "no callback" ref and proxy objects are supposed |
| 377 |
# to be shared so long as they exist by all callers so long as |
| 378 |
# they are active. In Python 2.3.3 and earlier, this guarantee |
| 379 |
# was not honored, and was broken in different ways for |
| 380 |
# PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.) |
| 381 |
|
| 382 |
def test_shared_ref_without_callback(self): |
| 383 |
self.check_shared_without_callback(weakref.ref) |
| 384 |
|
| 385 |
def test_shared_proxy_without_callback(self): |
| 386 |
self.check_shared_without_callback(weakref.proxy) |
| 387 |
|
| 388 |
def check_shared_without_callback(self, makeref): |
| 389 |
o = Object(1) |
| 390 |
p1 = makeref(o, None) |
| 391 |
p2 = makeref(o, None) |
| 392 |
self.assertIs(p1, p2, "both callbacks were None in the C API") |
| 393 |
del p1, p2 |
| 394 |
p1 = makeref(o) |
| 395 |
p2 = makeref(o, None) |
| 396 |
self.assertIs(p1, p2, "callbacks were NULL, None in the C API") |
| 397 |
del p1, p2 |
| 398 |
p1 = makeref(o) |
| 399 |
p2 = makeref(o) |
| 400 |
self.assertIs(p1, p2, "both callbacks were NULL in the C API") |
| 401 |
del p1, p2 |
| 402 |
p1 = makeref(o, None) |
| 403 |
p2 = makeref(o) |
| 404 |
self.assertIs(p1, p2, "callbacks were None, NULL in the C API") |
| 405 |
|
| 406 |
def test_callable_proxy(self): |
| 407 |
o = Callable() |
| 408 |
ref1 = weakref.proxy(o) |
| 409 |
|
| 410 |
self.check_proxy(o, ref1) |
| 411 |
|
| 412 |
self.assertIs(type(ref1), weakref.CallableProxyType, |
| 413 |
"proxy is not of callable type") |
| 414 |
ref1('twinkies!') |
| 415 |
self.assertEqual(o.bar, 'twinkies!', |
| 416 |
"call through proxy not passed through to original") |
| 417 |
ref1(x='Splat.') |
| 418 |
self.assertEqual(o.bar, 'Splat.', |
| 419 |
"call through proxy not passed through to original") |
| 420 |
|
| 421 |
# expect due to too few args |
| 422 |
self.assertRaises(TypeError, ref1) |
| 423 |
|
| 424 |
# expect due to too many args |
| 425 |
self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 426 |
|
| 427 |
def check_proxy(self, o, proxy): |
| 428 |
o.foo = 1 |
| 429 |
self.assertEqual(proxy.foo, 1, |
| 430 |
"proxy does not reflect attribute addition") |
| 431 |
o.foo = 2 |
| 432 |
self.assertEqual(proxy.foo, 2, |
| 433 |
"proxy does not reflect attribute modification") |
| 434 |
del o.foo |
| 435 |
self.assertNotHasAttr(proxy, 'foo', |
| 436 |
"proxy does not reflect attribute removal") |
| 437 |
|
| 438 |
proxy.foo = 1 |
| 439 |
self.assertEqual(o.foo, 1, |
| 440 |
"object does not reflect attribute addition via proxy") |
| 441 |
proxy.foo = 2 |
| 442 |
self.assertEqual(o.foo, 2, |
| 443 |
"object does not reflect attribute modification via proxy") |
| 444 |
del proxy.foo |
| 445 |
self.assertNotHasAttr(o, 'foo', |
| 446 |
"object does not reflect attribute removal via proxy") |
| 447 |
|
| 448 |
def test_proxy_deletion(self): |
| 449 |
# Test clearing of SF bug #762891 |
| 450 |
class Foo: |
| 451 |
result = None |
| 452 |
def __delitem__(self, accessor): |
| 453 |
self.result = accessor |
| 454 |
g = Foo() |
| 455 |
f = weakref.proxy(g) |
| 456 |
del f[0] |
| 457 |
self.assertEqual(f.result, 0) |
| 458 |
|
| 459 |
def test_proxy_bool(self): |
| 460 |
# Test clearing of SF bug #1170766 |
| 461 |
class List(list): pass |
| 462 |
lyst = List() |
| 463 |
self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst)) |
| 464 |
|
| 465 |
def test_proxy_iter(self): |
| 466 |
# Test fails with a debug build of the interpreter |
| 467 |
# (see bpo-38395). |
| 468 |
|
| 469 |
obj = None |
| 470 |
|
| 471 |
class MyObj: |
| 472 |
def __iter__(self): |
| 473 |
nonlocal obj |
| 474 |
del obj |
| 475 |
return NotImplemented |
| 476 |
|
| 477 |
obj = MyObj() |
| 478 |
p = weakref.proxy(obj) |
| 479 |
with self.assertRaises(TypeError): |
| 480 |
# "blech" in p calls MyObj.__iter__ through the proxy, |
| 481 |
# without keeping a reference to the real object, so it |
| 482 |
# can be killed in the middle of the call |
| 483 |
"blech" in p |
| 484 |
|
| 485 |
def test_proxy_next(self): |
| 486 |
arr = [4, 5, 6] |
| 487 |
def iterator_func(): |
| 488 |
yield from arr |
| 489 |
it = iterator_func() |
| 490 |
|
| 491 |
class IteratesWeakly: |
| 492 |
def __iter__(self): |
| 493 |
return weakref.proxy(it) |
| 494 |
|
| 495 |
weak_it = IteratesWeakly() |
| 496 |
|
| 497 |
# Calls proxy.__next__ |
| 498 |
self.assertEqual(list(weak_it), [4, 5, 6]) |
| 499 |
|
| 500 |
def test_proxy_bad_next(self): |
| 501 |
# bpo-44720: PyIter_Next() shouldn't be called if the reference |
| 502 |
# isn't an iterator. |
| 503 |
|
| 504 |
not_an_iterator = lambda: 0 |
| 505 |
|
| 506 |
class A: |
| 507 |
def __iter__(self): |
| 508 |
return weakref.proxy(not_an_iterator) |
| 509 |
a = A() |
| 510 |
|
| 511 |
msg = "Weakref proxy referenced a non-iterator" |
| 512 |
with self.assertRaisesRegex(TypeError, msg): |
| 513 |
list(a) |
| 514 |
|
| 515 |
def test_proxy_reversed(self): |
| 516 |
class MyObj: |
| 517 |
def __len__(self): |
| 518 |
return 3 |
| 519 |
def __reversed__(self): |
| 520 |
return iter('cba') |
| 521 |
|
| 522 |
obj = MyObj() |
| 523 |
self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba") |
| 524 |
|
| 525 |
def test_proxy_hash(self): |
| 526 |
class MyObj: |
| 527 |
def __hash__(self): |
| 528 |
return 42 |
| 529 |
|
| 530 |
obj = MyObj() |
| 531 |
with self.assertRaises(TypeError): |
| 532 |
hash(weakref.proxy(obj)) |
| 533 |
|
| 534 |
class MyObj: |
| 535 |
__hash__ = None |
| 536 |
|
| 537 |
obj = MyObj() |
| 538 |
with self.assertRaises(TypeError): |
| 539 |
hash(weakref.proxy(obj)) |
| 540 |
|
| 541 |
def test_getweakrefcount(self): |
| 542 |
o = C() |
| 543 |
ref1 = weakref.ref(o) |
| 544 |
ref2 = weakref.ref(o, self.callback) |
| 545 |
self.assertEqual(weakref.getweakrefcount(o), 2, |
| 546 |
"got wrong number of weak reference objects") |
| 547 |
|
| 548 |
proxy1 = weakref.proxy(o) |
| 549 |
proxy2 = weakref.proxy(o, self.callback) |
| 550 |
self.assertEqual(weakref.getweakrefcount(o), 4, |
| 551 |
"got wrong number of weak reference objects") |
| 552 |
|
| 553 |
del ref1, ref2, proxy1, proxy2 |
| 554 |
gc_collect() # For PyPy or other GCs. |
| 555 |
self.assertEqual(weakref.getweakrefcount(o), 0, |
| 556 |
"weak reference objects not unlinked from" |
| 557 |
" referent when discarded.") |
| 558 |
|
| 559 |
# assumes ints do not support weakrefs |
| 560 |
self.assertEqual(weakref.getweakrefcount(1), 0, |
| 561 |
"got wrong number of weak reference objects for int") |
| 562 |
|
| 563 |
def test_getweakrefs(self): |
| 564 |
o = C() |
| 565 |
ref1 = weakref.ref(o, self.callback) |
| 566 |
ref2 = weakref.ref(o, self.callback) |
| 567 |
del ref1 |
| 568 |
gc_collect() # For PyPy or other GCs. |
| 569 |
self.assertEqual(weakref.getweakrefs(o), [ref2], |
| 570 |
"list of refs does not match") |
| 571 |
|
| 572 |
o = C() |
| 573 |
ref1 = weakref.ref(o, self.callback) |
| 574 |
ref2 = weakref.ref(o, self.callback) |
| 575 |
del ref2 |
| 576 |
gc_collect() # For PyPy or other GCs. |
| 577 |
self.assertEqual(weakref.getweakrefs(o), [ref1], |
| 578 |
"list of refs does not match") |
| 579 |
|
| 580 |
del ref1 |
| 581 |
gc_collect() # For PyPy or other GCs. |
| 582 |
self.assertEqual(weakref.getweakrefs(o), [], |
| 583 |
"list of refs not cleared") |
| 584 |
|
| 585 |
# assumes ints do not support weakrefs |
| 586 |
self.assertEqual(weakref.getweakrefs(1), [], |
| 587 |
"list of refs does not match for int") |
| 588 |
|
| 589 |
def test_newstyle_number_ops(self): |
| 590 |
class F(float): |
| 591 |
pass |
| 592 |
f = F(2.0) |
| 593 |
p = weakref.proxy(f) |
| 594 |
self.assertEqual(p + 1.0, 3.0) |
| 595 |
self.assertEqual(1.0 + p, 3.0) # this used to SEGV |
| 596 |
|
| 597 |
def test_callbacks_protected(self): |
| 598 |
# Callbacks protected from already-set exceptions? |
| 599 |
# Regression test for SF bug #478534. |
| 600 |
class BogusError(Exception): |
| 601 |
pass |
| 602 |
data = {} |
| 603 |
def remove(k): |
| 604 |
del data[k] |
| 605 |
def encapsulate(): |
| 606 |
f = lambda : () |
| 607 |
data[weakref.ref(f, remove)] = None |
| 608 |
raise BogusError |
| 609 |
try: |
| 610 |
encapsulate() |
| 611 |
except BogusError: |
| 612 |
pass |
| 613 |
else: |
| 614 |
self.fail("exception not properly restored") |
| 615 |
try: |
| 616 |
encapsulate() |
| 617 |
except BogusError: |
| 618 |
pass |
| 619 |
else: |
| 620 |
self.fail("exception not properly restored") |
| 621 |
|
| 622 |
def test_sf_bug_840829(self): |
| 623 |
# "weakref callbacks and gc corrupt memory" |
| 624 |
# subtype_dealloc erroneously exposed a new-style instance |
| 625 |
# already in the process of getting deallocated to gc, |
| 626 |
# causing double-deallocation if the instance had a weakref |
| 627 |
# callback that triggered gc. |
| 628 |
# If the bug exists, there probably won't be an obvious symptom |
| 629 |
# in a release build. In a debug build, a segfault will occur |
| 630 |
# when the second attempt to remove the instance from the "list |
| 631 |
# of all objects" occurs. |
| 632 |
|
| 633 |
import gc |
| 634 |
|
| 635 |
class C(object): |
| 636 |
pass |
| 637 |
|
| 638 |
c = C() |
| 639 |
wr = weakref.ref(c, lambda ignore: gc.collect()) |
| 640 |
del c |
| 641 |
|
| 642 |
# There endeth the first part. It gets worse. |
| 643 |
del wr |
| 644 |
|
| 645 |
c1 = C() |
| 646 |
c1.i = C() |
| 647 |
wr = weakref.ref(c1.i, lambda ignore: gc.collect()) |
| 648 |
|
| 649 |
c2 = C() |
| 650 |
c2.c1 = c1 |
| 651 |
del c1 # still alive because c2 points to it |
| 652 |
|
| 653 |
# Now when subtype_dealloc gets called on c2, it's not enough just |
| 654 |
# that c2 is immune from gc while the weakref callbacks associated |
| 655 |
# with c2 execute (there are none in this 2nd half of the test, btw). |
| 656 |
# subtype_dealloc goes on to call the base classes' deallocs too, |
| 657 |
# so any gc triggered by weakref callbacks associated with anything |
| 658 |
# torn down by a base class dealloc can also trigger double |
| 659 |
# deallocation of c2. |
| 660 |
del c2 |
| 661 |
|
| 662 |
def test_callback_in_cycle(self): |
| 663 |
import gc |
| 664 |
|
| 665 |
class J(object): |
| 666 |
pass |
| 667 |
|
| 668 |
class II(object): |
| 669 |
def acallback(self, ignore): |
| 670 |
self.J |
| 671 |
|
| 672 |
I = II() |
| 673 |
I.J = J |
| 674 |
I.wr = weakref.ref(J, I.acallback) |
| 675 |
|
| 676 |
# Now J and II are each in a self-cycle (as all new-style class |
| 677 |
# objects are, since their __mro__ points back to them). I holds |
| 678 |
# both a weak reference (I.wr) and a strong reference (I.J) to class |
| 679 |
# J. I is also in a cycle (I.wr points to a weakref that references |
| 680 |
# I.acallback). When we del these three, they all become trash, but |
| 681 |
# the cycles prevent any of them from getting cleaned up immediately. |
| 682 |
# Instead they have to wait for cyclic gc to deduce that they're |
| 683 |
# trash. |
| 684 |
# |
| 685 |
# gc used to call tp_clear on all of them, and the order in which |
| 686 |
# it does that is pretty accidental. The exact order in which we |
| 687 |
# built up these things manages to provoke gc into running tp_clear |
| 688 |
# in just the right order (I last). Calling tp_clear on II leaves |
| 689 |
# behind an insane class object (its __mro__ becomes NULL). Calling |
| 690 |
# tp_clear on J breaks its self-cycle, but J doesn't get deleted |
| 691 |
# just then because of the strong reference from I.J. Calling |
| 692 |
# tp_clear on I starts to clear I's __dict__, and just happens to |
| 693 |
# clear I.J first -- I.wr is still intact. That removes the last |
| 694 |
# reference to J, which triggers the weakref callback. The callback |
| 695 |
# tries to do "self.J", and instances of new-style classes look up |
| 696 |
# attributes ("J") in the class dict first. The class (II) wants to |
| 697 |
# search II.__mro__, but that's NULL. The result was a segfault in |
| 698 |
# a release build, and an assert failure in a debug build. |
| 699 |
del I, J, II |
| 700 |
gc.collect() |
| 701 |
|
| 702 |
def test_callback_reachable_one_way(self): |
| 703 |
import gc |
| 704 |
|
| 705 |
# This one broke the first patch that fixed the previous test. In this case, |
| 706 |
# the objects reachable from the callback aren't also reachable |
| 707 |
# from the object (c1) *triggering* the callback: you can get to |
| 708 |
# c1 from c2, but not vice-versa. The result was that c2's __dict__ |
| 709 |
# got tp_clear'ed by the time the c2.cb callback got invoked. |
| 710 |
|
| 711 |
class C: |
| 712 |
def cb(self, ignore): |
| 713 |
self.me |
| 714 |
self.c1 |
| 715 |
self.wr |
| 716 |
|
| 717 |
c1, c2 = C(), C() |
| 718 |
|
| 719 |
c2.me = c2 |
| 720 |
c2.c1 = c1 |
| 721 |
c2.wr = weakref.ref(c1, c2.cb) |
| 722 |
|
| 723 |
del c1, c2 |
| 724 |
gc.collect() |
| 725 |
|
| 726 |
def test_callback_different_classes(self): |
| 727 |
import gc |
| 728 |
|
| 729 |
# Like test_callback_reachable_one_way, except c2 and c1 have different |
| 730 |
# classes. c2's class (C) isn't reachable from c1 then, so protecting |
| 731 |
# objects reachable from the dying object (c1) isn't enough to stop |
| 732 |
# c2's class (C) from getting tp_clear'ed before c2.cb is invoked. |
| 733 |
# The result was a segfault (C.__mro__ was NULL when the callback |
| 734 |
# tried to look up self.me). |
| 735 |
|
| 736 |
class C(object): |
| 737 |
def cb(self, ignore): |
| 738 |
self.me |
| 739 |
self.c1 |
| 740 |
self.wr |
| 741 |
|
| 742 |
class D: |
| 743 |
pass |
| 744 |
|
| 745 |
c1, c2 = D(), C() |
| 746 |
|
| 747 |
c2.me = c2 |
| 748 |
c2.c1 = c1 |
| 749 |
c2.wr = weakref.ref(c1, c2.cb) |
| 750 |
|
| 751 |
del c1, c2, C, D |
| 752 |
gc.collect() |
| 753 |
|
| 754 |
def test_callback_in_cycle_resurrection(self): |
| 755 |
import gc |
| 756 |
|
| 757 |
# Do something nasty in a weakref callback: resurrect objects |
| 758 |
# from dead cycles. For this to be attempted, the weakref and |
| 759 |
# its callback must also be part of the cyclic trash (else the |
| 760 |
# objects reachable via the callback couldn't be in cyclic trash |
| 761 |
# to begin with -- the callback would act like an external root). |
| 762 |
# But gc clears trash weakrefs with callbacks early now, which |
| 763 |
# disables the callbacks, so the callbacks shouldn't get called |
| 764 |
# at all (and so nothing actually gets resurrected). |
| 765 |
|
| 766 |
alist = [] |
| 767 |
class C(object): |
| 768 |
def __init__(self, value): |
| 769 |
self.attribute = value |
| 770 |
|
| 771 |
def acallback(self, ignore): |
| 772 |
alist.append(self.c) |
| 773 |
|
| 774 |
c1, c2 = C(1), C(2) |
| 775 |
c1.c = c2 |
| 776 |
c2.c = c1 |
| 777 |
c1.wr = weakref.ref(c2, c1.acallback) |
| 778 |
c2.wr = weakref.ref(c1, c2.acallback) |
| 779 |
|
| 780 |
def C_went_away(ignore): |
| 781 |
alist.append("C went away") |
| 782 |
wr = weakref.ref(C, C_went_away) |
| 783 |
|
| 784 |
del c1, c2, C # make them all trash |
| 785 |
self.assertEqual(alist, []) # del isn't enough to reclaim anything |
| 786 |
|
| 787 |
gc.collect() |
| 788 |
# c1.wr and c2.wr were part of the cyclic trash, so should have |
| 789 |
# been cleared without their callbacks executing. OTOH, the weakref |
| 790 |
# to C is bound to a function local (wr), and wasn't trash, so that |
| 791 |
# callback should have been invoked when C went away. |
| 792 |
self.assertEqual(alist, ["C went away"]) |
| 793 |
# The remaining weakref should be dead now (its callback ran). |
| 794 |
self.assertEqual(wr(), None) |
| 795 |
|
| 796 |
del alist[:] |
| 797 |
gc.collect() |
| 798 |
self.assertEqual(alist, []) |
| 799 |
|
| 800 |
def test_callbacks_on_callback(self): |
| 801 |
import gc |
| 802 |
|
| 803 |
# Set up weakref callbacks *on* weakref callbacks. |
| 804 |
alist = [] |
| 805 |
def safe_callback(ignore): |
| 806 |
alist.append("safe_callback called") |
| 807 |
|
| 808 |
class C(object): |
| 809 |
def cb(self, ignore): |
| 810 |
alist.append("cb called") |
| 811 |
|
| 812 |
c, d = C(), C() |
| 813 |
c.other = d |
| 814 |
d.other = c |
| 815 |
callback = c.cb |
| 816 |
c.wr = weakref.ref(d, callback) # this won't trigger |
| 817 |
d.wr = weakref.ref(callback, d.cb) # ditto |
| 818 |
external_wr = weakref.ref(callback, safe_callback) # but this will |
| 819 |
self.assertIs(external_wr(), callback) |
| 820 |
|
| 821 |
# The weakrefs attached to c and d should get cleared, so that |
| 822 |
# C.cb is never called. But external_wr isn't part of the cyclic |
| 823 |
# trash, and no cyclic trash is reachable from it, so safe_callback |
| 824 |
# should get invoked when the bound method object callback (c.cb) |
| 825 |
# -- which is itself a callback, and also part of the cyclic trash -- |
| 826 |
# gets reclaimed at the end of gc. |
| 827 |
|
| 828 |
del callback, c, d, C |
| 829 |
self.assertEqual(alist, []) # del isn't enough to clean up cycles |
| 830 |
gc.collect() |
| 831 |
self.assertEqual(alist, ["safe_callback called"]) |
| 832 |
self.assertEqual(external_wr(), None) |
| 833 |
|
| 834 |
del alist[:] |
| 835 |
gc.collect() |
| 836 |
self.assertEqual(alist, []) |
| 837 |
|
| 838 |
def test_gc_during_ref_creation(self): |
| 839 |
self.check_gc_during_creation(weakref.ref) |
| 840 |
|
| 841 |
def test_gc_during_proxy_creation(self): |
| 842 |
self.check_gc_during_creation(weakref.proxy) |
| 843 |
|
| 844 |
def check_gc_during_creation(self, makeref): |
| 845 |
thresholds = gc.get_threshold() |
| 846 |
gc.set_threshold(1, 1, 1) |
| 847 |
gc.collect() |
| 848 |
class A: |
| 849 |
pass |
| 850 |
|
| 851 |
def callback(*args): |
| 852 |
pass |
| 853 |
|
| 854 |
referenced = A() |
| 855 |
|
| 856 |
a = A() |
| 857 |
a.a = a |
| 858 |
a.wr = makeref(referenced) |
| 859 |
|
| 860 |
try: |
| 861 |
# now make sure the object and the ref get labeled as |
| 862 |
# cyclic trash: |
| 863 |
a = A() |
| 864 |
weakref.ref(referenced, callback) |
| 865 |
|
| 866 |
finally: |
| 867 |
gc.set_threshold(*thresholds) |
| 868 |
|
| 869 |
def test_ref_created_during_del(self): |
| 870 |
# Bug #1377858 |
| 871 |
# A weakref created in an object's __del__() would crash the |
| 872 |
# interpreter when the weakref was cleaned up since it would refer to |
| 873 |
# non-existent memory. This test should not segfault the interpreter. |
| 874 |
class Target(object): |
| 875 |
def __del__(self): |
| 876 |
global ref_from_del |
| 877 |
ref_from_del = weakref.ref(self) |
| 878 |
|
| 879 |
w = Target() |
| 880 |
|
| 881 |
def test_init(self): |
| 882 |
# Issue 3634 |
| 883 |
# <weakref to class>.__init__() doesn't check errors correctly |
| 884 |
r = weakref.ref(Exception) |
| 885 |
self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0) |
| 886 |
# No exception should be raised here |
| 887 |
gc.collect() |
| 888 |
|
| 889 |
def test_classes(self): |
| 890 |
# Check that classes are weakrefable. |
| 891 |
class A(object): |
| 892 |
pass |
| 893 |
l = [] |
| 894 |
weakref.ref(int) |
| 895 |
a = weakref.ref(A, l.append) |
| 896 |
A = None |
| 897 |
gc.collect() |
| 898 |
self.assertEqual(a(), None) |
| 899 |
self.assertEqual(l, [a]) |
| 900 |
|
| 901 |
def test_equality(self): |
| 902 |
# Alive weakrefs defer equality testing to their underlying object. |
| 903 |
x = Object(1) |
| 904 |
y = Object(1) |
| 905 |
z = Object(2) |
| 906 |
a = weakref.ref(x) |
| 907 |
b = weakref.ref(y) |
| 908 |
c = weakref.ref(z) |
| 909 |
d = weakref.ref(x) |
| 910 |
# Note how we directly test the operators here, to stress both |
| 911 |
# __eq__ and __ne__. |
| 912 |
self.assertTrue(a == b) |
| 913 |
self.assertFalse(a != b) |
| 914 |
self.assertFalse(a == c) |
| 915 |
self.assertTrue(a != c) |
| 916 |
self.assertTrue(a == d) |
| 917 |
self.assertFalse(a != d) |
| 918 |
self.assertFalse(a == x) |
| 919 |
self.assertTrue(a != x) |
| 920 |
self.assertTrue(a == ALWAYS_EQ) |
| 921 |
self.assertFalse(a != ALWAYS_EQ) |
| 922 |
del x, y, z |
| 923 |
gc.collect() |
| 924 |
for r in a, b, c: |
| 925 |
# Sanity check |
| 926 |
self.assertIs(r(), None) |
| 927 |
# Dead weakrefs compare by identity: whether `a` and `d` are the |
| 928 |
# same weakref object is an implementation detail, since they pointed |
| 929 |
# to the same original object and didn't have a callback. |
| 930 |
# (see issue #16453). |
| 931 |
self.assertFalse(a == b) |
| 932 |
self.assertTrue(a != b) |
| 933 |
self.assertFalse(a == c) |
| 934 |
self.assertTrue(a != c) |
| 935 |
self.assertEqual(a == d, a is d) |
| 936 |
self.assertEqual(a != d, a is not d) |
| 937 |
|
| 938 |
def test_ordering(self): |
| 939 |
# weakrefs cannot be ordered, even if the underlying objects can. |
| 940 |
ops = [operator.lt, operator.gt, operator.le, operator.ge] |
| 941 |
x = Object(1) |
| 942 |
y = Object(1) |
| 943 |
a = weakref.ref(x) |
| 944 |
b = weakref.ref(y) |
| 945 |
for op in ops: |
| 946 |
self.assertRaises(TypeError, op, a, b) |
| 947 |
# Same when dead. |
| 948 |
del x, y |
| 949 |
gc.collect() |
| 950 |
for op in ops: |
| 951 |
self.assertRaises(TypeError, op, a, b) |
| 952 |
|
| 953 |
def test_hashing(self): |
| 954 |
# Alive weakrefs hash the same as the underlying object |
| 955 |
x = Object(42) |
| 956 |
y = Object(42) |
| 957 |
a = weakref.ref(x) |
| 958 |
b = weakref.ref(y) |
| 959 |
self.assertEqual(hash(a), hash(42)) |
| 960 |
del x, y |
| 961 |
gc.collect() |
| 962 |
# Dead weakrefs: |
| 963 |
# - retain their hash is they were hashed when alive; |
| 964 |
# - otherwise, cannot be hashed. |
| 965 |
self.assertEqual(hash(a), hash(42)) |
| 966 |
self.assertRaises(TypeError, hash, b) |
| 967 |
|
| 968 |
@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack") |
| 969 |
def test_trashcan_16602(self): |
| 970 |
# Issue #16602: when a weakref's target was part of a long |
| 971 |
# deallocation chain, the trashcan mechanism could delay clearing |
| 972 |
# of the weakref and make the target object visible from outside |
| 973 |
# code even though its refcount had dropped to 0. A crash ensued. |
| 974 |
class C: |
| 975 |
def __init__(self, parent): |
| 976 |
if not parent: |
| 977 |
return |
| 978 |
wself = weakref.ref(self) |
| 979 |
def cb(wparent): |
| 980 |
o = wself() |
| 981 |
self.wparent = weakref.ref(parent, cb) |
| 982 |
|
| 983 |
d = weakref.WeakKeyDictionary() |
| 984 |
root = c = C(None) |
| 985 |
for n in range(100): |
| 986 |
d[c] = c = C(c) |
| 987 |
del root |
| 988 |
gc.collect() |
| 989 |
|
| 990 |
def test_callback_attribute(self): |
| 991 |
x = Object(1) |
| 992 |
callback = lambda ref: None |
| 993 |
ref1 = weakref.ref(x, callback) |
| 994 |
self.assertIs(ref1.__callback__, callback) |
| 995 |
|
| 996 |
ref2 = weakref.ref(x) |
| 997 |
self.assertIsNone(ref2.__callback__) |
| 998 |
|
| 999 |
def test_callback_attribute_after_deletion(self): |
| 1000 |
x = Object(1) |
| 1001 |
ref = weakref.ref(x, self.callback) |
| 1002 |
self.assertIsNotNone(ref.__callback__) |
| 1003 |
del x |
| 1004 |
support.gc_collect() |
| 1005 |
self.assertIsNone(ref.__callback__) |
| 1006 |
|
| 1007 |
def test_set_callback_attribute(self): |
| 1008 |
x = Object(1) |
| 1009 |
callback = lambda ref: None |
| 1010 |
ref1 = weakref.ref(x, callback) |
| 1011 |
with self.assertRaises(AttributeError): |
| 1012 |
ref1.__callback__ = lambda ref: None |
| 1013 |
|
| 1014 |
def test_callback_gcs(self): |
| 1015 |
class ObjectWithDel(Object): |
| 1016 |
def __del__(self): pass |
| 1017 |
x = ObjectWithDel(1) |
| 1018 |
ref1 = weakref.ref(x, lambda ref: support.gc_collect()) |
| 1019 |
del x |
| 1020 |
support.gc_collect() |
| 1021 |
|
| 1022 |
@support.cpython_only |
| 1023 |
def test_no_memory_when_clearing(self): |
| 1024 |
# gh-118331: Make sure we do not raise an exception from the destructor |
| 1025 |
# when clearing weakrefs if allocating the intermediate tuple fails. |
| 1026 |
code = textwrap.dedent(""" |
| 1027 |
import _testcapi |
| 1028 |
import weakref |
| 1029 |
|
| 1030 |
class TestObj: |
| 1031 |
pass |
| 1032 |
|
| 1033 |
def callback(obj): |
| 1034 |
pass |
| 1035 |
|
| 1036 |
obj = TestObj() |
| 1037 |
# The choice of 50 is arbitrary, but must be large enough to ensure |
| 1038 |
# the allocation won't be serviced by the free list. |
| 1039 |
wrs = [weakref.ref(obj, callback) for _ in range(50)] |
| 1040 |
_testcapi.set_nomemory(0) |
| 1041 |
del obj |
| 1042 |
""").strip() |
| 1043 |
res, _ = script_helper.run_python_until_end("-c", code) |
| 1044 |
stderr = res.err.decode("ascii", "backslashreplace") |
| 1045 |
self.assertNotRegex(stderr, "_Py_Dealloc: Deallocator of type 'TestObj'") |
| 1046 |
|
| 1047 |
|
| 1048 |
class SubclassableWeakrefTestCase(TestBase): |
| 1049 |
|
| 1050 |
def test_subclass_refs(self): |
| 1051 |
class MyRef(weakref.ref): |
| 1052 |
def __init__(self, ob, callback=None, value=42): |
| 1053 |
self.value = value |
| 1054 |
super().__init__(ob, callback) |
| 1055 |
def __call__(self): |
| 1056 |
self.called = True |
| 1057 |
return super().__call__() |
| 1058 |
o = Object("foo") |
| 1059 |
mr = MyRef(o, value=24) |
| 1060 |
self.assertIs(mr(), o) |
| 1061 |
self.assertTrue(mr.called) |
| 1062 |
self.assertEqual(mr.value, 24) |
| 1063 |
del o |
| 1064 |
gc_collect() # For PyPy or other GCs. |
| 1065 |
self.assertIsNone(mr()) |
| 1066 |
self.assertTrue(mr.called) |
| 1067 |
|
| 1068 |
def test_subclass_refs_dont_replace_standard_refs(self): |
| 1069 |
class MyRef(weakref.ref): |
| 1070 |
pass |
| 1071 |
o = Object(42) |
| 1072 |
r1 = MyRef(o) |
| 1073 |
r2 = weakref.ref(o) |
| 1074 |
self.assertIsNot(r1, r2) |
| 1075 |
self.assertEqual(weakref.getweakrefs(o), [r2, r1]) |
| 1076 |
self.assertEqual(weakref.getweakrefcount(o), 2) |
| 1077 |
r3 = MyRef(o) |
| 1078 |
self.assertEqual(weakref.getweakrefcount(o), 3) |
| 1079 |
refs = weakref.getweakrefs(o) |
| 1080 |
self.assertEqual(len(refs), 3) |
| 1081 |
self.assertIs(r2, refs[0]) |
| 1082 |
self.assertIn(r1, refs[1:]) |
| 1083 |
self.assertIn(r3, refs[1:]) |
| 1084 |
|
| 1085 |
def test_subclass_refs_dont_conflate_callbacks(self): |
| 1086 |
class MyRef(weakref.ref): |
| 1087 |
pass |
| 1088 |
o = Object(42) |
| 1089 |
r1 = MyRef(o, id) |
| 1090 |
r2 = MyRef(o, str) |
| 1091 |
self.assertIsNot(r1, r2) |
| 1092 |
refs = weakref.getweakrefs(o) |
| 1093 |
self.assertIn(r1, refs) |
| 1094 |
self.assertIn(r2, refs) |
| 1095 |
|
| 1096 |
def test_subclass_refs_with_slots(self): |
| 1097 |
class MyRef(weakref.ref): |
| 1098 |
__slots__ = "slot1", "slot2" |
| 1099 |
def __new__(type, ob, callback, slot1, slot2): |
| 1100 |
return weakref.ref.__new__(type, ob, callback) |
| 1101 |
def __init__(self, ob, callback, slot1, slot2): |
| 1102 |
self.slot1 = slot1 |
| 1103 |
self.slot2 = slot2 |
| 1104 |
def meth(self): |
| 1105 |
return self.slot1 + self.slot2 |
| 1106 |
o = Object(42) |
| 1107 |
r = MyRef(o, None, "abc", "def") |
| 1108 |
self.assertEqual(r.slot1, "abc") |
| 1109 |
self.assertEqual(r.slot2, "def") |
| 1110 |
self.assertEqual(r.meth(), "abcdef") |
| 1111 |
self.assertNotHasAttr(r, "__dict__") |
| 1112 |
|
| 1113 |
def test_subclass_refs_with_cycle(self): |
| 1114 |
"""Confirm https://bugs.python.org/issue3100 is fixed.""" |
| 1115 |
# An instance of a weakref subclass can have attributes. |
| 1116 |
# If such a weakref holds the only strong reference to the object, |
| 1117 |
# deleting the weakref will delete the object. In this case, |
| 1118 |
# the callback must not be called, because the ref object is |
| 1119 |
# being deleted. |
| 1120 |
class MyRef(weakref.ref): |
| 1121 |
pass |
| 1122 |
|
| 1123 |
# Use a local callback, for "regrtest -R::" |
| 1124 |
# to detect refcounting problems |
| 1125 |
def callback(w): |
| 1126 |
self.cbcalled += 1 |
| 1127 |
|
| 1128 |
o = C() |
| 1129 |
r1 = MyRef(o, callback) |
| 1130 |
r1.o = o |
| 1131 |
del o |
| 1132 |
|
| 1133 |
del r1 # Used to crash here |
| 1134 |
|
| 1135 |
self.assertEqual(self.cbcalled, 0) |
| 1136 |
|
| 1137 |
# Same test, with two weakrefs to the same object |
| 1138 |
# (since code paths are different) |
| 1139 |
o = C() |
| 1140 |
r1 = MyRef(o, callback) |
| 1141 |
r2 = MyRef(o, callback) |
| 1142 |
r1.r = r2 |
| 1143 |
r2.o = o |
| 1144 |
del o |
| 1145 |
del r2 |
| 1146 |
|
| 1147 |
del r1 # Used to crash here |
| 1148 |
|
| 1149 |
self.assertEqual(self.cbcalled, 0) |
| 1150 |
|
| 1151 |
|
| 1152 |
class WeakMethodTestCase(unittest.TestCase): |
| 1153 |
|
| 1154 |
def _subclass(self): |
| 1155 |
"""Return an Object subclass overriding `some_method`.""" |
| 1156 |
class C(Object): |
| 1157 |
def some_method(self): |
| 1158 |
return 6 |
| 1159 |
return C |
| 1160 |
|
| 1161 |
def test_alive(self): |
| 1162 |
o = Object(1) |
| 1163 |
r = weakref.WeakMethod(o.some_method) |
| 1164 |
self.assertIsInstance(r, weakref.ReferenceType) |
| 1165 |
self.assertIsInstance(r(), type(o.some_method)) |
| 1166 |
self.assertIs(r().__self__, o) |
| 1167 |
self.assertIs(r().__func__, o.some_method.__func__) |
| 1168 |
self.assertEqual(r()(), 4) |
| 1169 |
|
| 1170 |
def test_object_dead(self): |
| 1171 |
o = Object(1) |
| 1172 |
r = weakref.WeakMethod(o.some_method) |
| 1173 |
del o |
| 1174 |
gc.collect() |
| 1175 |
self.assertIs(r(), None) |
| 1176 |
|
| 1177 |
def test_method_dead(self): |
| 1178 |
C = self._subclass() |
| 1179 |
o = C(1) |
| 1180 |
r = weakref.WeakMethod(o.some_method) |
| 1181 |
del C.some_method |
| 1182 |
gc.collect() |
| 1183 |
self.assertIs(r(), None) |
| 1184 |
|
| 1185 |
def test_callback_when_object_dead(self): |
| 1186 |
# Test callback behaviour when object dies first. |
| 1187 |
C = self._subclass() |
| 1188 |
calls = [] |
| 1189 |
def cb(arg): |
| 1190 |
calls.append(arg) |
| 1191 |
o = C(1) |
| 1192 |
r = weakref.WeakMethod(o.some_method, cb) |
| 1193 |
del o |
| 1194 |
gc.collect() |
| 1195 |
self.assertEqual(calls, [r]) |
| 1196 |
# Callback is only called once. |
| 1197 |
C.some_method = Object.some_method |
| 1198 |
gc.collect() |
| 1199 |
self.assertEqual(calls, [r]) |
| 1200 |
|
| 1201 |
def test_callback_when_method_dead(self): |
| 1202 |
# Test callback behaviour when method dies first. |
| 1203 |
C = self._subclass() |
| 1204 |
calls = [] |
| 1205 |
def cb(arg): |
| 1206 |
calls.append(arg) |
| 1207 |
o = C(1) |
| 1208 |
r = weakref.WeakMethod(o.some_method, cb) |
| 1209 |
del C.some_method |
| 1210 |
gc.collect() |
| 1211 |
self.assertEqual(calls, [r]) |
| 1212 |
# Callback is only called once. |
| 1213 |
del o |
| 1214 |
gc.collect() |
| 1215 |
self.assertEqual(calls, [r]) |
| 1216 |
|
| 1217 |
@support.cpython_only |
| 1218 |
def test_no_cycles(self): |
| 1219 |
# A WeakMethod doesn't create any reference cycle to itself. |
| 1220 |
o = Object(1) |
| 1221 |
def cb(_): |
| 1222 |
pass |
| 1223 |
r = weakref.WeakMethod(o.some_method, cb) |
| 1224 |
wr = weakref.ref(r) |
| 1225 |
del r |
| 1226 |
self.assertIs(wr(), None) |
| 1227 |
|
| 1228 |
def test_equality(self): |
| 1229 |
def _eq(a, b): |
| 1230 |
self.assertTrue(a == b) |
| 1231 |
self.assertFalse(a != b) |
| 1232 |
def _ne(a, b): |
| 1233 |
self.assertTrue(a != b) |
| 1234 |
self.assertFalse(a == b) |
| 1235 |
x = Object(1) |
| 1236 |
y = Object(1) |
| 1237 |
a = weakref.WeakMethod(x.some_method) |
| 1238 |
b = weakref.WeakMethod(y.some_method) |
| 1239 |
c = weakref.WeakMethod(x.other_method) |
| 1240 |
d = weakref.WeakMethod(y.other_method) |
| 1241 |
# Objects equal, same method |
| 1242 |
_eq(a, b) |
| 1243 |
_eq(c, d) |
| 1244 |
# Objects equal, different method |
| 1245 |
_ne(a, c) |
| 1246 |
_ne(a, d) |
| 1247 |
_ne(b, c) |
| 1248 |
_ne(b, d) |
| 1249 |
# Objects unequal, same or different method |
| 1250 |
z = Object(2) |
| 1251 |
e = weakref.WeakMethod(z.some_method) |
| 1252 |
f = weakref.WeakMethod(z.other_method) |
| 1253 |
_ne(a, e) |
| 1254 |
_ne(a, f) |
| 1255 |
_ne(b, e) |
| 1256 |
_ne(b, f) |
| 1257 |
# Compare with different types |
| 1258 |
_ne(a, x.some_method) |
| 1259 |
_eq(a, ALWAYS_EQ) |
| 1260 |
del x, y, z |
| 1261 |
gc.collect() |
| 1262 |
# Dead WeakMethods compare by identity |
| 1263 |
refs = a, b, c, d, e, f |
| 1264 |
for q in refs: |
| 1265 |
for r in refs: |
| 1266 |
self.assertEqual(q == r, q is r) |
| 1267 |
self.assertEqual(q != r, q is not r) |
| 1268 |
|
| 1269 |
def test_hashing(self): |
| 1270 |
# Alive WeakMethods are hashable if the underlying object is |
| 1271 |
# hashable. |
| 1272 |
x = Object(1) |
| 1273 |
y = Object(1) |
| 1274 |
a = weakref.WeakMethod(x.some_method) |
| 1275 |
b = weakref.WeakMethod(y.some_method) |
| 1276 |
c = weakref.WeakMethod(y.other_method) |
| 1277 |
# Since WeakMethod objects are equal, the hashes should be equal. |
| 1278 |
self.assertEqual(hash(a), hash(b)) |
| 1279 |
ha = hash(a) |
| 1280 |
# Dead WeakMethods retain their old hash value |
| 1281 |
del x, y |
| 1282 |
gc.collect() |
| 1283 |
self.assertEqual(hash(a), ha) |
| 1284 |
self.assertEqual(hash(b), ha) |
| 1285 |
# If it wasn't hashed when alive, a dead WeakMethod cannot be hashed. |
| 1286 |
self.assertRaises(TypeError, hash, c) |
| 1287 |
|
| 1288 |
|
| 1289 |
class MappingTestCase(TestBase): |
| 1290 |
|
| 1291 |
COUNT = 10 |
| 1292 |
|
| 1293 |
if support.check_sanitizer(thread=True) and support.Py_GIL_DISABLED: |
| 1294 |
# Reduce iteration count to get acceptable latency |
| 1295 |
NUM_THREADED_ITERATIONS = 1000 |
| 1296 |
else: |
| 1297 |
NUM_THREADED_ITERATIONS = 100000 |
| 1298 |
|
| 1299 |
def check_len_cycles(self, dict_type, cons): |
| 1300 |
N = 20 |
| 1301 |
items = [RefCycle() for i in range(N)] |
| 1302 |
dct = dict_type(cons(o) for o in items) |
| 1303 |
# Keep an iterator alive |
| 1304 |
it = dct.items() |
| 1305 |
try: |
| 1306 |
next(it) |
| 1307 |
except StopIteration: |
| 1308 |
pass |
| 1309 |
del items |
| 1310 |
gc.collect() |
| 1311 |
n1 = len(dct) |
| 1312 |
del it |
| 1313 |
gc.collect() |
| 1314 |
n2 = len(dct) |
| 1315 |
# one item may be kept alive inside the iterator |
| 1316 |
self.assertIn(n1, (0, 1)) |
| 1317 |
self.assertEqual(n2, 0) |
| 1318 |
|
| 1319 |
def test_weak_keyed_len_cycles(self): |
| 1320 |
self.check_len_cycles(weakref.WeakKeyDictionary, lambda k: (k, 1)) |
| 1321 |
|
| 1322 |
def test_weak_valued_len_cycles(self): |
| 1323 |
self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k)) |
| 1324 |
|
| 1325 |
def check_len_race(self, dict_type, cons): |
| 1326 |
# Extended sanity checks for len() in the face of cyclic collection |
| 1327 |
self.addCleanup(gc.set_threshold, *gc.get_threshold()) |
| 1328 |
for th in range(1, 100): |
| 1329 |
N = 20 |
| 1330 |
gc.collect(0) |
| 1331 |
gc.set_threshold(th, th, th) |
| 1332 |
items = [RefCycle() for i in range(N)] |
| 1333 |
dct = dict_type(cons(o) for o in items) |
| 1334 |
del items |
| 1335 |
# All items will be collected at next garbage collection pass |
| 1336 |
it = dct.items() |
| 1337 |
try: |
| 1338 |
next(it) |
| 1339 |
except StopIteration: |
| 1340 |
pass |
| 1341 |
n1 = len(dct) |
| 1342 |
del it |
| 1343 |
n2 = len(dct) |
| 1344 |
self.assertGreaterEqual(n1, 0) |
| 1345 |
self.assertLessEqual(n1, N) |
| 1346 |
self.assertGreaterEqual(n2, 0) |
| 1347 |
self.assertLessEqual(n2, n1) |
| 1348 |
|
| 1349 |
def test_weak_keyed_len_race(self): |
| 1350 |
self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1)) |
| 1351 |
|
| 1352 |
def test_weak_valued_len_race(self): |
| 1353 |
self.check_len_race(weakref.WeakValueDictionary, lambda k: (1, k)) |
| 1354 |
|
| 1355 |
def test_weak_values(self): |
| 1356 |
# |
| 1357 |
# This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 1358 |
# |
| 1359 |
dict, objects = self.make_weak_valued_dict() |
| 1360 |
for o in objects: |
| 1361 |
self.assertEqual(weakref.getweakrefcount(o), 1) |
| 1362 |
self.assertIs(o, dict[o.arg], |
| 1363 |
"wrong object returned by weak dict!") |
| 1364 |
items1 = list(dict.items()) |
| 1365 |
items2 = list(dict.copy().items()) |
| 1366 |
items1.sort() |
| 1367 |
items2.sort() |
| 1368 |
self.assertEqual(items1, items2, |
| 1369 |
"cloning of weak-valued dictionary did not work!") |
| 1370 |
del items1, items2 |
| 1371 |
self.assertEqual(len(dict), self.COUNT) |
| 1372 |
del objects[0] |
| 1373 |
gc_collect() # For PyPy or other GCs. |
| 1374 |
self.assertEqual(len(dict), self.COUNT - 1, |
| 1375 |
"deleting object did not cause dictionary update") |
| 1376 |
del objects, o |
| 1377 |
gc_collect() # For PyPy or other GCs. |
| 1378 |
self.assertEqual(len(dict), 0, |
| 1379 |
"deleting the values did not clear the dictionary") |
| 1380 |
# regression on SF bug #447152: |
| 1381 |
dict = weakref.WeakValueDictionary() |
| 1382 |
self.assertRaises(KeyError, dict.__getitem__, 1) |
| 1383 |
dict[2] = C() |
| 1384 |
gc_collect() # For PyPy or other GCs. |
| 1385 |
self.assertRaises(KeyError, dict.__getitem__, 2) |
| 1386 |
|
| 1387 |
def test_weak_keys(self): |
| 1388 |
# |
| 1389 |
# This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
| 1390 |
# len(d), k in d. |
| 1391 |
# |
| 1392 |
dict, objects = self.make_weak_keyed_dict() |
| 1393 |
for o in objects: |
| 1394 |
self.assertEqual(weakref.getweakrefcount(o), 1, |
| 1395 |
"wrong number of weak references to %r!" % o) |
| 1396 |
self.assertIs(o.arg, dict[o], |
| 1397 |
"wrong object returned by weak dict!") |
| 1398 |
items1 = dict.items() |
| 1399 |
items2 = dict.copy().items() |
| 1400 |
self.assertEqual(set(items1), set(items2), |
| 1401 |
"cloning of weak-keyed dictionary did not work!") |
| 1402 |
del items1, items2 |
| 1403 |
self.assertEqual(len(dict), self.COUNT) |
| 1404 |
del objects[0] |
| 1405 |
gc_collect() # For PyPy or other GCs. |
| 1406 |
self.assertEqual(len(dict), (self.COUNT - 1), |
| 1407 |
"deleting object did not cause dictionary update") |
| 1408 |
del objects, o |
| 1409 |
gc_collect() # For PyPy or other GCs. |
| 1410 |
self.assertEqual(len(dict), 0, |
| 1411 |
"deleting the keys did not clear the dictionary") |
| 1412 |
o = Object(42) |
| 1413 |
dict[o] = "What is the meaning of the universe?" |
| 1414 |
self.assertIn(o, dict) |
| 1415 |
self.assertNotIn(34, dict) |
| 1416 |
|
| 1417 |
def test_weak_keyed_iters(self): |
| 1418 |
dict, objects = self.make_weak_keyed_dict() |
| 1419 |
self.check_iters(dict) |
| 1420 |
|
| 1421 |
# Test keyrefs() |
| 1422 |
refs = dict.keyrefs() |
| 1423 |
self.assertEqual(len(refs), len(objects)) |
| 1424 |
objects2 = list(objects) |
| 1425 |
for wr in refs: |
| 1426 |
ob = wr() |
| 1427 |
self.assertIn(ob, dict) |
| 1428 |
self.assertIn(ob, dict) |
| 1429 |
self.assertEqual(ob.arg, dict[ob]) |
| 1430 |
objects2.remove(ob) |
| 1431 |
self.assertEqual(len(objects2), 0) |
| 1432 |
|
| 1433 |
# Test iterkeyrefs() |
| 1434 |
objects2 = list(objects) |
| 1435 |
self.assertEqual(len(list(dict.keyrefs())), len(objects)) |
| 1436 |
for wr in dict.keyrefs(): |
| 1437 |
ob = wr() |
| 1438 |
self.assertIn(ob, dict) |
| 1439 |
self.assertIn(ob, dict) |
| 1440 |
self.assertEqual(ob.arg, dict[ob]) |
| 1441 |
objects2.remove(ob) |
| 1442 |
self.assertEqual(len(objects2), 0) |
| 1443 |
|
| 1444 |
def test_weak_valued_iters(self): |
| 1445 |
dict, objects = self.make_weak_valued_dict() |
| 1446 |
self.check_iters(dict) |
| 1447 |
|
| 1448 |
# Test valuerefs() |
| 1449 |
refs = dict.valuerefs() |
| 1450 |
self.assertEqual(len(refs), len(objects)) |
| 1451 |
objects2 = list(objects) |
| 1452 |
for wr in refs: |
| 1453 |
ob = wr() |
| 1454 |
self.assertEqual(ob, dict[ob.arg]) |
| 1455 |
self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 1456 |
objects2.remove(ob) |
| 1457 |
self.assertEqual(len(objects2), 0) |
| 1458 |
|
| 1459 |
# Test itervaluerefs() |
| 1460 |
objects2 = list(objects) |
| 1461 |
self.assertEqual(len(list(dict.itervaluerefs())), len(objects)) |
| 1462 |
for wr in dict.itervaluerefs(): |
| 1463 |
ob = wr() |
| 1464 |
self.assertEqual(ob, dict[ob.arg]) |
| 1465 |
self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 1466 |
objects2.remove(ob) |
| 1467 |
self.assertEqual(len(objects2), 0) |
| 1468 |
|
| 1469 |
def check_iters(self, dict): |
| 1470 |
# item iterator: |
| 1471 |
items = list(dict.items()) |
| 1472 |
for item in dict.items(): |
| 1473 |
items.remove(item) |
| 1474 |
self.assertFalse(items, "items() did not touch all items") |
| 1475 |
|
| 1476 |
# key iterator, via __iter__(): |
| 1477 |
keys = list(dict.keys()) |
| 1478 |
for k in dict: |
| 1479 |
keys.remove(k) |
| 1480 |
self.assertFalse(keys, "__iter__() did not touch all keys") |
| 1481 |
|
| 1482 |
# key iterator, via iterkeys(): |
| 1483 |
keys = list(dict.keys()) |
| 1484 |
for k in dict.keys(): |
| 1485 |
keys.remove(k) |
| 1486 |
self.assertFalse(keys, "iterkeys() did not touch all keys") |
| 1487 |
|
| 1488 |
# value iterator: |
| 1489 |
values = list(dict.values()) |
| 1490 |
for v in dict.values(): |
| 1491 |
values.remove(v) |
| 1492 |
self.assertFalse(values, |
| 1493 |
"itervalues() did not touch all values") |
| 1494 |
|
| 1495 |
def check_weak_destroy_while_iterating(self, dict, objects, iter_name): |
| 1496 |
n = len(dict) |
| 1497 |
it = iter(getattr(dict, iter_name)()) |
| 1498 |
next(it) # Trigger internal iteration |
| 1499 |
# Destroy an object |
| 1500 |
del objects[-1] |
| 1501 |
gc.collect() # just in case |
| 1502 |
# We have removed either the first consumed object, or another one |
| 1503 |
self.assertIn(len(list(it)), [len(objects), len(objects) - 1]) |
| 1504 |
del it |
| 1505 |
# The removal has been committed |
| 1506 |
self.assertEqual(len(dict), n - 1) |
| 1507 |
|
| 1508 |
def check_weak_destroy_and_mutate_while_iterating(self, dict, testcontext): |
| 1509 |
# Check that we can explicitly mutate the weak dict without |
| 1510 |
# interfering with delayed removal. |
| 1511 |
# `testcontext` should create an iterator, destroy one of the |
| 1512 |
# weakref'ed objects and then return a new key/value pair corresponding |
| 1513 |
# to the destroyed object. |
| 1514 |
with testcontext() as (k, v): |
| 1515 |
self.assertNotIn(k, dict) |
| 1516 |
with testcontext() as (k, v): |
| 1517 |
self.assertRaises(KeyError, dict.__delitem__, k) |
| 1518 |
self.assertNotIn(k, dict) |
| 1519 |
with testcontext() as (k, v): |
| 1520 |
self.assertRaises(KeyError, dict.pop, k) |
| 1521 |
self.assertNotIn(k, dict) |
| 1522 |
with testcontext() as (k, v): |
| 1523 |
dict[k] = v |
| 1524 |
self.assertEqual(dict[k], v) |
| 1525 |
ddict = copy.copy(dict) |
| 1526 |
with testcontext() as (k, v): |
| 1527 |
dict.update(ddict) |
| 1528 |
self.assertEqual(dict, ddict) |
| 1529 |
with testcontext() as (k, v): |
| 1530 |
dict.clear() |
| 1531 |
self.assertEqual(len(dict), 0) |
| 1532 |
|
| 1533 |
def check_weak_del_and_len_while_iterating(self, dict, testcontext): |
| 1534 |
# Check that len() works when both iterating and removing keys |
| 1535 |
# explicitly through various means (.pop(), .clear()...), while |
| 1536 |
# implicit mutation is deferred because an iterator is alive. |
| 1537 |
# (each call to testcontext() should schedule one item for removal |
| 1538 |
# for this test to work properly) |
| 1539 |
o = Object(123456) |
| 1540 |
with testcontext(): |
| 1541 |
n = len(dict) |
| 1542 |
# Since underlying dict is ordered, first item is popped |
| 1543 |
dict.pop(next(dict.keys())) |
| 1544 |
self.assertEqual(len(dict), n - 1) |
| 1545 |
dict[o] = o |
| 1546 |
self.assertEqual(len(dict), n) |
| 1547 |
# last item in objects is removed from dict in context shutdown |
| 1548 |
with testcontext(): |
| 1549 |
self.assertEqual(len(dict), n - 1) |
| 1550 |
# Then, (o, o) is popped |
| 1551 |
dict.popitem() |
| 1552 |
self.assertEqual(len(dict), n - 2) |
| 1553 |
with testcontext(): |
| 1554 |
self.assertEqual(len(dict), n - 3) |
| 1555 |
del dict[next(dict.keys())] |
| 1556 |
self.assertEqual(len(dict), n - 4) |
| 1557 |
with testcontext(): |
| 1558 |
self.assertEqual(len(dict), n - 5) |
| 1559 |
dict.popitem() |
| 1560 |
self.assertEqual(len(dict), n - 6) |
| 1561 |
with testcontext(): |
| 1562 |
dict.clear() |
| 1563 |
self.assertEqual(len(dict), 0) |
| 1564 |
self.assertEqual(len(dict), 0) |
| 1565 |
|
| 1566 |
def test_weak_keys_destroy_while_iterating(self): |
| 1567 |
# Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 1568 |
dict, objects = self.make_weak_keyed_dict() |
| 1569 |
self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 1570 |
self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 1571 |
self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 1572 |
self.check_weak_destroy_while_iterating(dict, objects, 'keyrefs') |
| 1573 |
dict, objects = self.make_weak_keyed_dict() |
| 1574 |
@contextlib.contextmanager |
| 1575 |
def testcontext(): |
| 1576 |
try: |
| 1577 |
it = iter(dict.items()) |
| 1578 |
next(it) |
| 1579 |
# Schedule a key/value for removal and recreate it |
| 1580 |
v = objects.pop().arg |
| 1581 |
gc.collect() # just in case |
| 1582 |
yield Object(v), v |
| 1583 |
finally: |
| 1584 |
it = None # should commit all removals |
| 1585 |
gc.collect() |
| 1586 |
self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
| 1587 |
# Issue #21173: len() fragile when keys are both implicitly and |
| 1588 |
# explicitly removed. |
| 1589 |
dict, objects = self.make_weak_keyed_dict() |
| 1590 |
self.check_weak_del_and_len_while_iterating(dict, testcontext) |
| 1591 |
|
| 1592 |
def test_weak_values_destroy_while_iterating(self): |
| 1593 |
# Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 1594 |
dict, objects = self.make_weak_valued_dict() |
| 1595 |
self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 1596 |
self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 1597 |
self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 1598 |
self.check_weak_destroy_while_iterating(dict, objects, 'itervaluerefs') |
| 1599 |
self.check_weak_destroy_while_iterating(dict, objects, 'valuerefs') |
| 1600 |
dict, objects = self.make_weak_valued_dict() |
| 1601 |
@contextlib.contextmanager |
| 1602 |
def testcontext(): |
| 1603 |
try: |
| 1604 |
it = iter(dict.items()) |
| 1605 |
next(it) |
| 1606 |
# Schedule a key/value for removal and recreate it |
| 1607 |
k = objects.pop().arg |
| 1608 |
gc.collect() # just in case |
| 1609 |
yield k, Object(k) |
| 1610 |
finally: |
| 1611 |
it = None # should commit all removals |
| 1612 |
gc.collect() |
| 1613 |
self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
| 1614 |
dict, objects = self.make_weak_valued_dict() |
| 1615 |
self.check_weak_del_and_len_while_iterating(dict, testcontext) |
| 1616 |
|
| 1617 |
def test_make_weak_keyed_dict_from_dict(self): |
| 1618 |
o = Object(3) |
| 1619 |
dict = weakref.WeakKeyDictionary({o:364}) |
| 1620 |
self.assertEqual(dict[o], 364) |
| 1621 |
|
| 1622 |
def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
| 1623 |
o = Object(3) |
| 1624 |
dict = weakref.WeakKeyDictionary({o:364}) |
| 1625 |
dict2 = weakref.WeakKeyDictionary(dict) |
| 1626 |
self.assertEqual(dict[o], 364) |
| 1627 |
|
| 1628 |
def make_weak_keyed_dict(self): |
| 1629 |
dict = weakref.WeakKeyDictionary() |
| 1630 |
objects = list(map(Object, range(self.COUNT))) |
| 1631 |
for o in objects: |
| 1632 |
dict[o] = o.arg |
| 1633 |
return dict, objects |
| 1634 |
|
| 1635 |
def test_make_weak_valued_dict_from_dict(self): |
| 1636 |
o = Object(3) |
| 1637 |
dict = weakref.WeakValueDictionary({364:o}) |
| 1638 |
self.assertEqual(dict[364], o) |
| 1639 |
|
| 1640 |
def test_make_weak_valued_dict_from_weak_valued_dict(self): |
| 1641 |
o = Object(3) |
| 1642 |
dict = weakref.WeakValueDictionary({364:o}) |
| 1643 |
dict2 = weakref.WeakValueDictionary(dict) |
| 1644 |
self.assertEqual(dict[364], o) |
| 1645 |
|
| 1646 |
def test_make_weak_valued_dict_misc(self): |
| 1647 |
# errors |
| 1648 |
self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__) |
| 1649 |
self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {}) |
| 1650 |
self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ()) |
| 1651 |
# special keyword arguments |
| 1652 |
o = Object(3) |
| 1653 |
for kw in 'self', 'dict', 'other', 'iterable': |
| 1654 |
d = weakref.WeakValueDictionary(**{kw: o}) |
| 1655 |
self.assertEqual(list(d.keys()), [kw]) |
| 1656 |
self.assertEqual(d[kw], o) |
| 1657 |
|
| 1658 |
def make_weak_valued_dict(self): |
| 1659 |
dict = weakref.WeakValueDictionary() |
| 1660 |
objects = list(map(Object, range(self.COUNT))) |
| 1661 |
for o in objects: |
| 1662 |
dict[o.arg] = o |
| 1663 |
return dict, objects |
| 1664 |
|
| 1665 |
def check_popitem(self, klass, key1, value1, key2, value2): |
| 1666 |
weakdict = klass() |
| 1667 |
weakdict[key1] = value1 |
| 1668 |
weakdict[key2] = value2 |
| 1669 |
self.assertEqual(len(weakdict), 2) |
| 1670 |
k, v = weakdict.popitem() |
| 1671 |
self.assertEqual(len(weakdict), 1) |
| 1672 |
if k is key1: |
| 1673 |
self.assertIs(v, value1) |
| 1674 |
else: |
| 1675 |
self.assertIs(v, value2) |
| 1676 |
k, v = weakdict.popitem() |
| 1677 |
self.assertEqual(len(weakdict), 0) |
| 1678 |
if k is key1: |
| 1679 |
self.assertIs(v, value1) |
| 1680 |
else: |
| 1681 |
self.assertIs(v, value2) |
| 1682 |
|
| 1683 |
def test_weak_valued_dict_popitem(self): |
| 1684 |
self.check_popitem(weakref.WeakValueDictionary, |
| 1685 |
"key1", C(), "key2", C()) |
| 1686 |
|
| 1687 |
def test_weak_keyed_dict_popitem(self): |
| 1688 |
self.check_popitem(weakref.WeakKeyDictionary, |
| 1689 |
C(), "value 1", C(), "value 2") |
| 1690 |
|
| 1691 |
def check_setdefault(self, klass, key, value1, value2): |
| 1692 |
self.assertIsNot(value1, value2, |
| 1693 |
"invalid test" |
| 1694 |
" -- value parameters must be distinct objects") |
| 1695 |
weakdict = klass() |
| 1696 |
o = weakdict.setdefault(key, value1) |
| 1697 |
self.assertIs(o, value1) |
| 1698 |
self.assertIn(key, weakdict) |
| 1699 |
self.assertIs(weakdict.get(key), value1) |
| 1700 |
self.assertIs(weakdict[key], value1) |
| 1701 |
|
| 1702 |
o = weakdict.setdefault(key, value2) |
| 1703 |
self.assertIs(o, value1) |
| 1704 |
self.assertIn(key, weakdict) |
| 1705 |
self.assertIs(weakdict.get(key), value1) |
| 1706 |
self.assertIs(weakdict[key], value1) |
| 1707 |
|
| 1708 |
def test_weak_valued_dict_setdefault(self): |
| 1709 |
self.check_setdefault(weakref.WeakValueDictionary, |
| 1710 |
"key", C(), C()) |
| 1711 |
|
| 1712 |
def test_weak_keyed_dict_setdefault(self): |
| 1713 |
self.check_setdefault(weakref.WeakKeyDictionary, |
| 1714 |
C(), "value 1", "value 2") |
| 1715 |
|
| 1716 |
def check_update(self, klass, dict): |
| 1717 |
# |
| 1718 |
# This exercises d.update(), len(d), d.keys(), k in d, |
| 1719 |
# d.get(), d[]. |
| 1720 |
# |
| 1721 |
weakdict = klass() |
| 1722 |
weakdict.update(dict) |
| 1723 |
self.assertEqual(len(weakdict), len(dict)) |
| 1724 |
for k in weakdict.keys(): |
| 1725 |
self.assertIn(k, dict, "mysterious new key appeared in weak dict") |
| 1726 |
v = dict.get(k) |
| 1727 |
self.assertIs(v, weakdict[k]) |
| 1728 |
self.assertIs(v, weakdict.get(k)) |
| 1729 |
for k in dict.keys(): |
| 1730 |
self.assertIn(k, weakdict, "original key disappeared in weak dict") |
| 1731 |
v = dict[k] |
| 1732 |
self.assertIs(v, weakdict[k]) |
| 1733 |
self.assertIs(v, weakdict.get(k)) |
| 1734 |
|
| 1735 |
def test_weak_valued_dict_update(self): |
| 1736 |
self.check_update(weakref.WeakValueDictionary, |
| 1737 |
{1: C(), 'a': C(), C(): C()}) |
| 1738 |
# errors |
| 1739 |
self.assertRaises(TypeError, weakref.WeakValueDictionary.update) |
| 1740 |
d = weakref.WeakValueDictionary() |
| 1741 |
self.assertRaises(TypeError, d.update, {}, {}) |
| 1742 |
self.assertRaises(TypeError, d.update, (), ()) |
| 1743 |
self.assertEqual(list(d.keys()), []) |
| 1744 |
# special keyword arguments |
| 1745 |
o = Object(3) |
| 1746 |
for kw in 'self', 'dict', 'other', 'iterable': |
| 1747 |
d = weakref.WeakValueDictionary() |
| 1748 |
d.update(**{kw: o}) |
| 1749 |
self.assertEqual(list(d.keys()), [kw]) |
| 1750 |
self.assertEqual(d[kw], o) |
| 1751 |
|
| 1752 |
def test_weak_valued_union_operators(self): |
| 1753 |
a = C() |
| 1754 |
b = C() |
| 1755 |
c = C() |
| 1756 |
wvd1 = weakref.WeakValueDictionary({1: a}) |
| 1757 |
wvd2 = weakref.WeakValueDictionary({1: b, 2: a}) |
| 1758 |
wvd3 = wvd1.copy() |
| 1759 |
d1 = {1: c, 3: b} |
| 1760 |
pairs = [(5, c), (6, b)] |
| 1761 |
|
| 1762 |
tmp1 = wvd1 | wvd2 # Between two WeakValueDictionaries |
| 1763 |
self.assertEqual(dict(tmp1), dict(wvd1) | dict(wvd2)) |
| 1764 |
self.assertIs(type(tmp1), weakref.WeakValueDictionary) |
| 1765 |
wvd1 |= wvd2 |
| 1766 |
self.assertEqual(wvd1, tmp1) |
| 1767 |
|
| 1768 |
tmp2 = wvd2 | d1 # Between WeakValueDictionary and mapping |
| 1769 |
self.assertEqual(dict(tmp2), dict(wvd2) | d1) |
| 1770 |
self.assertIs(type(tmp2), weakref.WeakValueDictionary) |
| 1771 |
wvd2 |= d1 |
| 1772 |
self.assertEqual(wvd2, tmp2) |
| 1773 |
|
| 1774 |
tmp3 = wvd3.copy() # Between WeakValueDictionary and iterable key, value |
| 1775 |
tmp3 |= pairs |
| 1776 |
self.assertEqual(dict(tmp3), dict(wvd3) | dict(pairs)) |
| 1777 |
self.assertIs(type(tmp3), weakref.WeakValueDictionary) |
| 1778 |
|
| 1779 |
tmp4 = d1 | wvd3 # Testing .__ror__ |
| 1780 |
self.assertEqual(dict(tmp4), d1 | dict(wvd3)) |
| 1781 |
self.assertIs(type(tmp4), weakref.WeakValueDictionary) |
| 1782 |
|
| 1783 |
del a |
| 1784 |
self.assertNotIn(2, tmp1) |
| 1785 |
self.assertNotIn(2, tmp2) |
| 1786 |
self.assertNotIn(1, tmp3) |
| 1787 |
self.assertNotIn(1, tmp4) |
| 1788 |
|
| 1789 |
def test_weak_keyed_dict_update(self): |
| 1790 |
self.check_update(weakref.WeakKeyDictionary, |
| 1791 |
{C(): 1, C(): 2, C(): 3}) |
| 1792 |
|
| 1793 |
def test_weak_keyed_delitem(self): |
| 1794 |
d = weakref.WeakKeyDictionary() |
| 1795 |
o1 = Object('1') |
| 1796 |
o2 = Object('2') |
| 1797 |
d[o1] = 'something' |
| 1798 |
d[o2] = 'something' |
| 1799 |
self.assertEqual(len(d), 2) |
| 1800 |
del d[o1] |
| 1801 |
self.assertEqual(len(d), 1) |
| 1802 |
self.assertEqual(list(d.keys()), [o2]) |
| 1803 |
|
| 1804 |
def test_weak_keyed_union_operators(self): |
| 1805 |
o1 = C() |
| 1806 |
o2 = C() |
| 1807 |
o3 = C() |
| 1808 |
wkd1 = weakref.WeakKeyDictionary({o1: 1, o2: 2}) |
| 1809 |
wkd2 = weakref.WeakKeyDictionary({o3: 3, o1: 4}) |
| 1810 |
wkd3 = wkd1.copy() |
| 1811 |
d1 = {o2: '5', o3: '6'} |
| 1812 |
pairs = [(o2, 7), (o3, 8)] |
| 1813 |
|
| 1814 |
tmp1 = wkd1 | wkd2 # Between two WeakKeyDictionaries |
| 1815 |
self.assertEqual(dict(tmp1), dict(wkd1) | dict(wkd2)) |
| 1816 |
self.assertIs(type(tmp1), weakref.WeakKeyDictionary) |
| 1817 |
wkd1 |= wkd2 |
| 1818 |
self.assertEqual(wkd1, tmp1) |
| 1819 |
|
| 1820 |
tmp2 = wkd2 | d1 # Between WeakKeyDictionary and mapping |
| 1821 |
self.assertEqual(dict(tmp2), dict(wkd2) | d1) |
| 1822 |
self.assertIs(type(tmp2), weakref.WeakKeyDictionary) |
| 1823 |
wkd2 |= d1 |
| 1824 |
self.assertEqual(wkd2, tmp2) |
| 1825 |
|
| 1826 |
tmp3 = wkd3.copy() # Between WeakKeyDictionary and iterable key, value |
| 1827 |
tmp3 |= pairs |
| 1828 |
self.assertEqual(dict(tmp3), dict(wkd3) | dict(pairs)) |
| 1829 |
self.assertIs(type(tmp3), weakref.WeakKeyDictionary) |
| 1830 |
|
| 1831 |
tmp4 = d1 | wkd3 # Testing .__ror__ |
| 1832 |
self.assertEqual(dict(tmp4), d1 | dict(wkd3)) |
| 1833 |
self.assertIs(type(tmp4), weakref.WeakKeyDictionary) |
| 1834 |
|
| 1835 |
del o1 |
| 1836 |
self.assertNotIn(4, tmp1.values()) |
| 1837 |
self.assertNotIn(4, tmp2.values()) |
| 1838 |
self.assertNotIn(1, tmp3.values()) |
| 1839 |
self.assertNotIn(1, tmp4.values()) |
| 1840 |
|
| 1841 |
def test_weak_valued_delitem(self): |
| 1842 |
d = weakref.WeakValueDictionary() |
| 1843 |
o1 = Object('1') |
| 1844 |
o2 = Object('2') |
| 1845 |
d['something'] = o1 |
| 1846 |
d['something else'] = o2 |
| 1847 |
self.assertEqual(len(d), 2) |
| 1848 |
del d['something'] |
| 1849 |
self.assertEqual(len(d), 1) |
| 1850 |
self.assertEqual(list(d.items()), [('something else', o2)]) |
| 1851 |
|
| 1852 |
def test_weak_keyed_bad_delitem(self): |
| 1853 |
d = weakref.WeakKeyDictionary() |
| 1854 |
o = Object('1') |
| 1855 |
# An attempt to delete an object that isn't there should raise |
| 1856 |
# KeyError. It didn't before 2.3. |
| 1857 |
self.assertRaises(KeyError, d.__delitem__, o) |
| 1858 |
self.assertRaises(KeyError, d.__getitem__, o) |
| 1859 |
|
| 1860 |
# If a key isn't of a weakly referencable type, __getitem__ and |
| 1861 |
# __setitem__ raise TypeError. __delitem__ should too. |
| 1862 |
self.assertRaises(TypeError, d.__delitem__, 13) |
| 1863 |
self.assertRaises(TypeError, d.__getitem__, 13) |
| 1864 |
self.assertRaises(TypeError, d.__setitem__, 13, 13) |
| 1865 |
|
| 1866 |
def test_weak_keyed_cascading_deletes(self): |
| 1867 |
# SF bug 742860. For some reason, before 2.3 __delitem__ iterated |
| 1868 |
# over the keys via self.data.iterkeys(). If things vanished from |
| 1869 |
# the dict during this (or got added), that caused a RuntimeError. |
| 1870 |
|
| 1871 |
d = weakref.WeakKeyDictionary() |
| 1872 |
mutate = False |
| 1873 |
|
| 1874 |
class C(object): |
| 1875 |
def __init__(self, i): |
| 1876 |
self.value = i |
| 1877 |
def __hash__(self): |
| 1878 |
return hash(self.value) |
| 1879 |
def __eq__(self, other): |
| 1880 |
if mutate: |
| 1881 |
# Side effect that mutates the dict, by removing the |
| 1882 |
# last strong reference to a key. |
| 1883 |
del objs[-1] |
| 1884 |
return self.value == other.value |
| 1885 |
|
| 1886 |
objs = [C(i) for i in range(4)] |
| 1887 |
for o in objs: |
| 1888 |
d[o] = o.value |
| 1889 |
del o # now the only strong references to keys are in objs |
| 1890 |
# Find the order in which iterkeys sees the keys. |
| 1891 |
objs = list(d.keys()) |
| 1892 |
# Reverse it, so that the iteration implementation of __delitem__ |
| 1893 |
# has to keep looping to find the first object we delete. |
| 1894 |
objs.reverse() |
| 1895 |
|
| 1896 |
# Turn on mutation in C.__eq__. The first time through the loop, |
| 1897 |
# under the iterkeys() business the first comparison will delete |
| 1898 |
# the last item iterkeys() would see, and that causes a |
| 1899 |
# RuntimeError: dictionary changed size during iteration |
| 1900 |
# when the iterkeys() loop goes around to try comparing the next |
| 1901 |
# key. After this was fixed, it just deletes the last object *our* |
| 1902 |
# "for o in obj" loop would have gotten to. |
| 1903 |
mutate = True |
| 1904 |
count = 0 |
| 1905 |
for o in objs: |
| 1906 |
count += 1 |
| 1907 |
del d[o] |
| 1908 |
gc_collect() # For PyPy or other GCs. |
| 1909 |
self.assertEqual(len(d), 0) |
| 1910 |
self.assertEqual(count, 2) |
| 1911 |
|
| 1912 |
def test_make_weak_valued_dict_repr(self): |
| 1913 |
dict = weakref.WeakValueDictionary() |
| 1914 |
self.assertRegex(repr(dict), '<WeakValueDictionary at 0x.*>') |
| 1915 |
|
| 1916 |
def test_make_weak_keyed_dict_repr(self): |
| 1917 |
dict = weakref.WeakKeyDictionary() |
| 1918 |
self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>') |
| 1919 |
|
| 1920 |
@threading_helper.requires_working_threading() |
| 1921 |
def test_threaded_weak_valued_setdefault(self): |
| 1922 |
d = weakref.WeakValueDictionary() |
| 1923 |
with collect_in_thread(): |
| 1924 |
for i in range(self.NUM_THREADED_ITERATIONS): |
| 1925 |
x = d.setdefault(10, RefCycle()) |
| 1926 |
self.assertIsNot(x, None) # we never put None in there! |
| 1927 |
del x |
| 1928 |
|
| 1929 |
@threading_helper.requires_working_threading() |
| 1930 |
def test_threaded_weak_valued_pop(self): |
| 1931 |
d = weakref.WeakValueDictionary() |
| 1932 |
with collect_in_thread(): |
| 1933 |
for i in range(self.NUM_THREADED_ITERATIONS): |
| 1934 |
d[10] = RefCycle() |
| 1935 |
x = d.pop(10, 10) |
| 1936 |
self.assertIsNot(x, None) # we never put None in there! |
| 1937 |
|
| 1938 |
@threading_helper.requires_working_threading() |
| 1939 |
def test_threaded_weak_valued_consistency(self): |
| 1940 |
# Issue #28427: old keys should not remove new values from |
| 1941 |
# WeakValueDictionary when collecting from another thread. |
| 1942 |
d = weakref.WeakValueDictionary() |
| 1943 |
with collect_in_thread(): |
| 1944 |
for i in range(2 * self.NUM_THREADED_ITERATIONS): |
| 1945 |
o = RefCycle() |
| 1946 |
d[10] = o |
| 1947 |
# o is still alive, so the dict can't be empty |
| 1948 |
self.assertEqual(len(d), 1) |
| 1949 |
o = None # lose ref |
| 1950 |
|
| 1951 |
@support.cpython_only |
| 1952 |
def test_weak_valued_consistency(self): |
| 1953 |
# A single-threaded, deterministic repro for issue #28427: old keys |
| 1954 |
# should not remove new values from WeakValueDictionary. This relies on |
| 1955 |
# an implementation detail of CPython's WeakValueDictionary (its |
| 1956 |
# underlying dictionary of KeyedRefs) to reproduce the issue. |
| 1957 |
d = weakref.WeakValueDictionary() |
| 1958 |
with support.disable_gc(): |
| 1959 |
d[10] = RefCycle() |
| 1960 |
# Keep the KeyedRef alive after it's replaced so that GC will invoke |
| 1961 |
# the callback. |
| 1962 |
wr = d.data[10] |
| 1963 |
# Replace the value with something that isn't cyclic garbage |
| 1964 |
o = RefCycle() |
| 1965 |
d[10] = o |
| 1966 |
# Trigger GC, which will invoke the callback for `wr` |
| 1967 |
gc.collect() |
| 1968 |
self.assertEqual(len(d), 1) |
| 1969 |
|
| 1970 |
def check_threaded_weak_dict_copy(self, type_, deepcopy): |
| 1971 |
# `type_` should be either WeakKeyDictionary or WeakValueDictionary. |
| 1972 |
# `deepcopy` should be either True or False. |
| 1973 |
exc = [] |
| 1974 |
|
| 1975 |
class DummyKey: |
| 1976 |
def __init__(self, ctr): |
| 1977 |
self.ctr = ctr |
| 1978 |
|
| 1979 |
class DummyValue: |
| 1980 |
def __init__(self, ctr): |
| 1981 |
self.ctr = ctr |
| 1982 |
|
| 1983 |
def dict_copy(d, exc): |
| 1984 |
try: |
| 1985 |
if deepcopy is True: |
| 1986 |
_ = copy.deepcopy(d) |
| 1987 |
else: |
| 1988 |
_ = d.copy() |
| 1989 |
except Exception as ex: |
| 1990 |
exc.append(ex) |
| 1991 |
|
| 1992 |
def pop_and_collect(lst): |
| 1993 |
gc_ctr = 0 |
| 1994 |
while lst: |
| 1995 |
i = random.randint(0, len(lst) - 1) |
| 1996 |
gc_ctr += 1 |
| 1997 |
lst.pop(i) |
| 1998 |
if gc_ctr % 10000 == 0: |
| 1999 |
gc.collect() # just in case |
| 2000 |
|
| 2001 |
self.assertIn(type_, (weakref.WeakKeyDictionary, weakref.WeakValueDictionary)) |
| 2002 |
|
| 2003 |
d = type_() |
| 2004 |
keys = [] |
| 2005 |
values = [] |
| 2006 |
# Initialize d with many entries |
| 2007 |
for i in range(70000): |
| 2008 |
k, v = DummyKey(i), DummyValue(i) |
| 2009 |
keys.append(k) |
| 2010 |
values.append(v) |
| 2011 |
d[k] = v |
| 2012 |
del k |
| 2013 |
del v |
| 2014 |
|
| 2015 |
t_copy = threading.Thread(target=dict_copy, args=(d, exc,)) |
| 2016 |
if type_ is weakref.WeakKeyDictionary: |
| 2017 |
t_collect = threading.Thread(target=pop_and_collect, args=(keys,)) |
| 2018 |
else: # weakref.WeakValueDictionary |
| 2019 |
t_collect = threading.Thread(target=pop_and_collect, args=(values,)) |
| 2020 |
|
| 2021 |
t_copy.start() |
| 2022 |
t_collect.start() |
| 2023 |
|
| 2024 |
t_copy.join() |
| 2025 |
t_collect.join() |
| 2026 |
|
| 2027 |
# Test exceptions |
| 2028 |
if exc: |
| 2029 |
raise exc[0] |
| 2030 |
|
| 2031 |
@threading_helper.requires_working_threading() |
| 2032 |
@support.requires_resource('cpu') |
| 2033 |
def test_threaded_weak_key_dict_copy(self): |
| 2034 |
# Issue #35615: Weakref keys or values getting GC'ed during dict |
| 2035 |
# copying should not result in a crash. |
| 2036 |
self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, False) |
| 2037 |
|
| 2038 |
@threading_helper.requires_working_threading() |
| 2039 |
@support.requires_resource('cpu') |
| 2040 |
def test_threaded_weak_key_dict_deepcopy(self): |
| 2041 |
# Issue #35615: Weakref keys or values getting GC'ed during dict |
| 2042 |
# copying should not result in a crash. |
| 2043 |
self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, True) |
| 2044 |
|
| 2045 |
@threading_helper.requires_working_threading() |
| 2046 |
@support.requires_resource('cpu') |
| 2047 |
def test_threaded_weak_value_dict_copy(self): |
| 2048 |
# Issue #35615: Weakref keys or values getting GC'ed during dict |
| 2049 |
# copying should not result in a crash. |
| 2050 |
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, False) |
| 2051 |
|
| 2052 |
@threading_helper.requires_working_threading() |
| 2053 |
@support.requires_resource('cpu') |
| 2054 |
def test_threaded_weak_value_dict_deepcopy(self): |
| 2055 |
# Issue #35615: Weakref keys or values getting GC'ed during dict |
| 2056 |
# copying should not result in a crash. |
| 2057 |
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True) |
| 2058 |
|
| 2059 |
@support.cpython_only |
| 2060 |
def test_remove_closure(self): |
| 2061 |
d = weakref.WeakValueDictionary() |
| 2062 |
self.assertIsNone(d._remove.__closure__) |
| 2063 |
|
| 2064 |
|
| 2065 |
from test import mapping_tests |
| 2066 |
|
| 2067 |
class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
| 2068 |
"""Check that WeakValueDictionary conforms to the mapping protocol""" |
| 2069 |
__ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
| 2070 |
type2test = weakref.WeakValueDictionary |
| 2071 |
def _reference(self): |
| 2072 |
return self.__ref.copy() |
| 2073 |
|
| 2074 |
class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
| 2075 |
"""Check that WeakKeyDictionary conforms to the mapping protocol""" |
| 2076 |
__ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
| 2077 |
type2test = weakref.WeakKeyDictionary |
| 2078 |
def _reference(self): |
| 2079 |
return self.__ref.copy() |
| 2080 |
|
| 2081 |
|
| 2082 |
class FinalizeTestCase(unittest.TestCase): |
| 2083 |
|
| 2084 |
class A: |
| 2085 |
pass |
| 2086 |
|
| 2087 |
def _collect_if_necessary(self): |
| 2088 |
# we create no ref-cycles so in CPython no gc should be needed |
| 2089 |
if sys.implementation.name != 'cpython': |
| 2090 |
support.gc_collect() |
| 2091 |
|
| 2092 |
def test_finalize(self): |
| 2093 |
def add(x,y,z): |
| 2094 |
res.append(x + y + z) |
| 2095 |
return x + y + z |
| 2096 |
|
| 2097 |
a = self.A() |
| 2098 |
|
| 2099 |
res = [] |
| 2100 |
f = weakref.finalize(a, add, 67, 43, z=89) |
| 2101 |
self.assertEqual(f.alive, True) |
| 2102 |
self.assertEqual(f.peek(), (a, add, (67,43), {'z':89})) |
| 2103 |
self.assertEqual(f(), 199) |
| 2104 |
self.assertEqual(f(), None) |
| 2105 |
self.assertEqual(f(), None) |
| 2106 |
self.assertEqual(f.peek(), None) |
| 2107 |
self.assertEqual(f.detach(), None) |
| 2108 |
self.assertEqual(f.alive, False) |
| 2109 |
self.assertEqual(res, [199]) |
| 2110 |
|
| 2111 |
res = [] |
| 2112 |
f = weakref.finalize(a, add, 67, 43, 89) |
| 2113 |
self.assertEqual(f.peek(), (a, add, (67,43,89), {})) |
| 2114 |
self.assertEqual(f.detach(), (a, add, (67,43,89), {})) |
| 2115 |
self.assertEqual(f(), None) |
| 2116 |
self.assertEqual(f(), None) |
| 2117 |
self.assertEqual(f.peek(), None) |
| 2118 |
self.assertEqual(f.detach(), None) |
| 2119 |
self.assertEqual(f.alive, False) |
| 2120 |
self.assertEqual(res, []) |
| 2121 |
|
| 2122 |
res = [] |
| 2123 |
f = weakref.finalize(a, add, x=67, y=43, z=89) |
| 2124 |
del a |
| 2125 |
self._collect_if_necessary() |
| 2126 |
self.assertEqual(f(), None) |
| 2127 |
self.assertEqual(f(), None) |
| 2128 |
self.assertEqual(f.peek(), None) |
| 2129 |
self.assertEqual(f.detach(), None) |
| 2130 |
self.assertEqual(f.alive, False) |
| 2131 |
self.assertEqual(res, [199]) |
| 2132 |
|
| 2133 |
def test_arg_errors(self): |
| 2134 |
def fin(*args, **kwargs): |
| 2135 |
res.append((args, kwargs)) |
| 2136 |
|
| 2137 |
a = self.A() |
| 2138 |
|
| 2139 |
res = [] |
| 2140 |
f = weakref.finalize(a, fin, 1, 2, func=3, obj=4) |
| 2141 |
self.assertEqual(f.peek(), (a, fin, (1, 2), {'func': 3, 'obj': 4})) |
| 2142 |
f() |
| 2143 |
self.assertEqual(res, [((1, 2), {'func': 3, 'obj': 4})]) |
| 2144 |
|
| 2145 |
with self.assertRaises(TypeError): |
| 2146 |
weakref.finalize(a, func=fin, arg=1) |
| 2147 |
with self.assertRaises(TypeError): |
| 2148 |
weakref.finalize(obj=a, func=fin, arg=1) |
| 2149 |
self.assertRaises(TypeError, weakref.finalize, a) |
| 2150 |
self.assertRaises(TypeError, weakref.finalize) |
| 2151 |
|
| 2152 |
def test_order(self): |
| 2153 |
a = self.A() |
| 2154 |
res = [] |
| 2155 |
|
| 2156 |
f1 = weakref.finalize(a, res.append, 'f1') |
| 2157 |
f2 = weakref.finalize(a, res.append, 'f2') |
| 2158 |
f3 = weakref.finalize(a, res.append, 'f3') |
| 2159 |
f4 = weakref.finalize(a, res.append, 'f4') |
| 2160 |
f5 = weakref.finalize(a, res.append, 'f5') |
| 2161 |
|
| 2162 |
# make sure finalizers can keep themselves alive |
| 2163 |
del f1, f4 |
| 2164 |
|
| 2165 |
self.assertTrue(f2.alive) |
| 2166 |
self.assertTrue(f3.alive) |
| 2167 |
self.assertTrue(f5.alive) |
| 2168 |
|
| 2169 |
self.assertTrue(f5.detach()) |
| 2170 |
self.assertFalse(f5.alive) |
| 2171 |
|
| 2172 |
f5() # nothing because previously unregistered |
| 2173 |
res.append('A') |
| 2174 |
f3() # => res.append('f3') |
| 2175 |
self.assertFalse(f3.alive) |
| 2176 |
res.append('B') |
| 2177 |
f3() # nothing because previously called |
| 2178 |
res.append('C') |
| 2179 |
del a |
| 2180 |
self._collect_if_necessary() |
| 2181 |
# => res.append('f4') |
| 2182 |
# => res.append('f2') |
| 2183 |
# => res.append('f1') |
| 2184 |
self.assertFalse(f2.alive) |
| 2185 |
res.append('D') |
| 2186 |
f2() # nothing because previously called by gc |
| 2187 |
|
| 2188 |
expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D'] |
| 2189 |
self.assertEqual(res, expected) |
| 2190 |
|
| 2191 |
def test_all_freed(self): |
| 2192 |
# we want a weakrefable subclass of weakref.finalize |
| 2193 |
class MyFinalizer(weakref.finalize): |
| 2194 |
pass |
| 2195 |
|
| 2196 |
a = self.A() |
| 2197 |
res = [] |
| 2198 |
def callback(): |
| 2199 |
res.append(123) |
| 2200 |
f = MyFinalizer(a, callback) |
| 2201 |
|
| 2202 |
wr_callback = weakref.ref(callback) |
| 2203 |
wr_f = weakref.ref(f) |
| 2204 |
del callback, f |
| 2205 |
|
| 2206 |
self.assertIsNotNone(wr_callback()) |
| 2207 |
self.assertIsNotNone(wr_f()) |
| 2208 |
|
| 2209 |
del a |
| 2210 |
self._collect_if_necessary() |
| 2211 |
|
| 2212 |
self.assertIsNone(wr_callback()) |
| 2213 |
self.assertIsNone(wr_f()) |
| 2214 |
self.assertEqual(res, [123]) |
| 2215 |
|
| 2216 |
@classmethod |
| 2217 |
def run_in_child(cls): |
| 2218 |
def error(): |
| 2219 |
# Create an atexit finalizer from inside a finalizer called |
| 2220 |
# at exit. This should be the next to be run. |
| 2221 |
g1 = weakref.finalize(cls, print, 'g1') |
| 2222 |
print('f3 error') |
| 2223 |
1/0 |
| 2224 |
|
| 2225 |
# cls should stay alive till atexit callbacks run |
| 2226 |
f1 = weakref.finalize(cls, print, 'f1', _global_var) |
| 2227 |
f2 = weakref.finalize(cls, print, 'f2', _global_var) |
| 2228 |
f3 = weakref.finalize(cls, error) |
| 2229 |
f4 = weakref.finalize(cls, print, 'f4', _global_var) |
| 2230 |
|
| 2231 |
assert f1.atexit == True |
| 2232 |
f2.atexit = False |
| 2233 |
assert f3.atexit == True |
| 2234 |
assert f4.atexit == True |
| 2235 |
|
| 2236 |
def test_atexit(self): |
| 2237 |
prog = ('from test.test_weakref import FinalizeTestCase;'+ |
| 2238 |
'FinalizeTestCase.run_in_child()') |
| 2239 |
rc, out, err = script_helper.assert_python_ok('-c', prog) |
| 2240 |
out = out.decode('ascii').splitlines() |
| 2241 |
self.assertEqual(out, ['f4 foobar', 'f3 error', 'g1', 'f1 foobar']) |
| 2242 |
self.assertTrue(b'ZeroDivisionError' in err) |
| 2243 |
|
| 2244 |
|
| 2245 |
class ModuleTestCase(unittest.TestCase): |
| 2246 |
def test_names(self): |
| 2247 |
for name in ('ReferenceType', 'ProxyType', 'CallableProxyType', |
| 2248 |
'WeakMethod', 'WeakSet', 'WeakKeyDictionary', 'WeakValueDictionary'): |
| 2249 |
obj = getattr(weakref, name) |
| 2250 |
if name != 'WeakSet': |
| 2251 |
self.assertEqual(obj.__module__, 'weakref') |
| 2252 |
self.assertEqual(obj.__name__, name) |
| 2253 |
self.assertEqual(obj.__qualname__, name) |
| 2254 |
|
| 2255 |
|
| 2256 |
libreftest = """ Doctest for examples in the library reference: weakref.rst |
| 2257 |
|
| 2258 |
>>> from test.support import gc_collect |
| 2259 |
>>> import weakref |
| 2260 |
>>> class Dict(dict): |
| 2261 |
... pass |
| 2262 |
... |
| 2263 |
>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable |
| 2264 |
>>> r = weakref.ref(obj) |
| 2265 |
>>> print(r() is obj) |
| 2266 |
True |
| 2267 |
|
| 2268 |
>>> import weakref |
| 2269 |
>>> class Object: |
| 2270 |
... pass |
| 2271 |
... |
| 2272 |
>>> o = Object() |
| 2273 |
>>> r = weakref.ref(o) |
| 2274 |
>>> o2 = r() |
| 2275 |
>>> o is o2 |
| 2276 |
True |
| 2277 |
>>> del o, o2 |
| 2278 |
>>> gc_collect() # For PyPy or other GCs. |
| 2279 |
>>> print(r()) |
| 2280 |
None |
| 2281 |
|
| 2282 |
>>> import weakref |
| 2283 |
>>> class ExtendedRef(weakref.ref): |
| 2284 |
... def __init__(self, ob, callback=None, **annotations): |
| 2285 |
... super().__init__(ob, callback) |
| 2286 |
... self.__counter = 0 |
| 2287 |
... for k, v in annotations.items(): |
| 2288 |
... setattr(self, k, v) |
| 2289 |
... def __call__(self): |
| 2290 |
... '''Return a pair containing the referent and the number of |
| 2291 |
... times the reference has been called. |
| 2292 |
... ''' |
| 2293 |
... ob = super().__call__() |
| 2294 |
... if ob is not None: |
| 2295 |
... self.__counter += 1 |
| 2296 |
... ob = (ob, self.__counter) |
| 2297 |
... return ob |
| 2298 |
... |
| 2299 |
>>> class A: # not in docs from here, just testing the ExtendedRef |
| 2300 |
... pass |
| 2301 |
... |
| 2302 |
>>> a = A() |
| 2303 |
>>> r = ExtendedRef(a, foo=1, bar="baz") |
| 2304 |
>>> r.foo |
| 2305 |
1 |
| 2306 |
>>> r.bar |
| 2307 |
'baz' |
| 2308 |
>>> r()[1] |
| 2309 |
1 |
| 2310 |
>>> r()[1] |
| 2311 |
2 |
| 2312 |
>>> r()[0] is a |
| 2313 |
True |
| 2314 |
|
| 2315 |
|
| 2316 |
>>> import weakref |
| 2317 |
>>> _id2obj_dict = weakref.WeakValueDictionary() |
| 2318 |
>>> def remember(obj): |
| 2319 |
... oid = id(obj) |
| 2320 |
... _id2obj_dict[oid] = obj |
| 2321 |
... return oid |
| 2322 |
... |
| 2323 |
>>> def id2obj(oid): |
| 2324 |
... return _id2obj_dict[oid] |
| 2325 |
... |
| 2326 |
>>> a = A() # from here, just testing |
| 2327 |
>>> a_id = remember(a) |
| 2328 |
>>> id2obj(a_id) is a |
| 2329 |
True |
| 2330 |
>>> del a |
| 2331 |
>>> gc_collect() # For PyPy or other GCs. |
| 2332 |
>>> try: |
| 2333 |
... id2obj(a_id) |
| 2334 |
... except KeyError: |
| 2335 |
... print('OK') |
| 2336 |
... else: |
| 2337 |
... print('WeakValueDictionary error') |
| 2338 |
OK |
| 2339 |
|
| 2340 |
""" |
| 2341 |
|
| 2342 |
__test__ = {'libreftest' : libreftest} |
| 2343 |
|
| 2344 |
def load_tests(loader, tests, pattern): |
| 2345 |
tests.addTest(doctest.DocTestSuite()) |
| 2346 |
return tests |
| 2347 |
|
| 2348 |
|
| 2349 |
if __name__ == "__main__": |
| 2350 |
unittest.main() |