'A context manager that captures any AssertionError raised within it,
and, if within a ``bulk_assertions`` context, records the captured
assertion to the bulk assertion manager. If not within a ``bulk_assertions``
context, just raises the original exception.'

@contextmanager
def _capture_assertion_errors(self):

    try:
        manager = self.__manager
        self.__manager = None
        (yield)
    except AssertionError:
        if (manager is not None):
            (exc_type, exc_value, exc_tb) = sys.exc_info()
            relevant_frames = 0
            for frame_record in inspect.stack():
                frame = frame_record[0]
                if ('__unittest' in frame.f_globals):
                    break
                relevant_frames += 1
            stack_above = traceback.extract_stack()[(- relevant_frames):(-1)]
            stack_below = traceback.extract_tb(exc_tb)
            formatted_stack = traceback.format_list((stack_above + stack_below))
            formatted_exc = traceback.format_exception_only(exc_type, exc_value)
            manager.log_error(''.join((formatted_stack + formatted_exc)))
        else:
            raise
    finally:
        self.__manager = manager
