@contextmanager
def assert_deallocated(func, *args, **kwargs):
 with gc_state(False):
  obj = func(*args, **kwargs)
  ref = weakref.ref(obj)
  (yield obj)
  del obj
  if (ref() is not None):
   raise ReferenceError('Remaining reference(s) to object')

@ contextmanager def assert_deallocated ( func , * args , ** kwargs ) : with gc_state ( False ) : obj = func ( * args , ** kwargs ) ref = weakref . ref ( obj ) ( yield obj ) del obj if ( ref ( ) is not None ) : raise ReferenceError ( 'Remaining reference(s) to object' )
context manager to check that object is deallocated this is useful for checking that an object can be freed directly by reference counting .