def test_getweakrefs(self): import _weakref, gc class A(object): pass a = A() assert _weakref.getweakrefs(a) == [] assert _weakref.getweakrefs(None) == [] ref1 = _weakref.ref(a) assert _weakref.getweakrefs(a) == [ref1]
def test_weakref_subclass_with_del(self): import _weakref, gc class Ref(_weakref.ref): def __del__(self): b.a = 42 class A(object): pass a = A() b = A() b.a = 1 w = Ref(a) del w gc.collect() assert b.a == 42 if _weakref.getweakrefcount(a) > 0: # the following can crash if the presence of the applevel __del__ # leads to the fact that the __del__ of _weakref.ref is not called. assert _weakref.getweakrefs(a)[0]() is a
def test(): assert (_weakref.ReferenceType is _weakref.ref) for _ in range(99): time.sleep(5) # Throttle to avoid a MemoryError try: _weakref._remove_dead_weakref(dct, 'obj') except NameError: pass obj = fclass() _weakref.getweakrefcount dct = {'obj': _weakref.ref(obj)} _weakref.getweakrefs(obj) _weakref.getweakrefs(dct['obj']) dct['prox'] = _weakref.proxy(ffunc, ffunc) dct['oprox'] = _weakref.proxy(obj, ffunc) q = _queue.SimpleQueue() lock = rnd.choice([_thread.allocate_lock, _thread.allocate])() def lock_thread(*a, **k): try: rnd.choice([lock.acquire_lock, lock.acquire])(blocking=fbool(), timeout=rnd.randint(-10, 10)) except ValueError: pass rnd.choice([lock.locked, lock.locked_lock])() ffunc(q) try: rnd.choice([lock.release_lock, lock.release])() except RuntimeError: pass try: with lock: ffunc(q) except RuntimeError: pass rnd.choice([lock.locked, lock.locked_lock])() l = _thread._set_sentinel() if fbool(): rnd.choice([_thread.exit_thread, _thread.exit])() rlock = _thread.RLock() def rlock_thread(*a, **k): rlock.acquire(fbool()) ffunc(q) rlock._is_owned() if fbool(): try: rlock._release_save() except RuntimeError: pass if fbool(): rlock._acquire_restore( (rnd.randint(-9999, 9999), rnd.randint(-9999, 9999))) try: rlock.release() except RuntimeError: pass try: with rlock: ffunc(q) except RuntimeError: pass rlock._is_owned() if fbool(): rnd.choice([_thread.exit_thread, _thread.exit])() for _ in range(99): repr(lock) repr(rlock) try: rnd.choice([_thread.start_new, _thread.start_new_thread ])(rnd.choice([lock_thread, rlock_thread]), (q, ) + ftup(), fdict()) except RuntimeError: pass try: _thread.stack_size(rnd.randint(-99999999, 99999999)) except (ValueError, OverflowError): pass ffunc(q) try: _thread.interrupt_main() except KeyboardInterrupt: pass