_lib._gcutils

Module for testing automatic garbage collection of objects

set_gc_state
gc_state
assert_deallocated

Module Contents

Classes

Functions

class ReferenceError
set_gc_state(state)

Set status of garbage collector

gc_state(state)

Context manager to set state of garbage collector to state

state : bool
True for gc enabled, False for disabled
>>> with gc_state(False):
...     assert not gc.isenabled()
>>> with gc_state(True):
...     assert gc.isenabled()
assert_deallocated(func, *args, **kwargs)

Context manager to check that object is deallocated

This is useful for checking that an object can be freed directly by reference counting, without requiring gc to break reference cycles. GC is disabled inside the context manager.

func : callable
Callable to create object to check
\*args : sequence
positional arguments to func in order to create object to check
\*\*kwargs : dict
keyword arguments to func in order to create object to check
>>> class C(object): pass
>>> with assert_deallocated(C) as c:
...     # do something
...     del c
>>> class C(object):
...     def __init__(self):
...         self._circular = self # Make circular reference
>>> with assert_deallocated(C) as c: 
...     # do something
...     del c
Traceback (most recent call last):
    ...
ReferenceError: Remaining reference(s) to object