def add_teardown_for_attr(obj, attr): try: old_attr = getattr(obj, attr) reset = lambda: setattr(obj, attr, old_attr) except AttributeError: reset = lambda: delattr(obj, attr) MockTransaction.add_teardown(reset)
def add_teardown_for(obj, attr, settable = Attr): """add a hook to remove `attr` when transaction ends. Settable can be Attr or Item""" try: old_attr = settable.get(obj, attr) except settable.error: reset = lambda: settable.delete(obj, attr) else: reset = lambda: settable.set(obj, attr, old_attr) MockTransaction.add_teardown(reset)
def __init__(self, name): self._acts = [] self._name = name self.received_calls = [] MockTransaction.add_teardown(self._verify)