'Converts a sys.exc_info()-style tuple of values into a string.'

def _exc_info_to_string(self, err, test):

    (exctype, value, tb) = err
    while (tb and self._is_relevant_tb_level(tb)):
        tb = tb.tb_next
    if (exctype is test.failureException):
        length = self._count_relevant_tb_levels(tb)
    else:
        length = None
    tb_e = traceback.TracebackException(exctype, value, tb, limit=length, capture_locals=self.tb_locals)
    msgLines = list(tb_e.format())
    if self.buffer:
        output = sys.stdout.getvalue()
        error = sys.stderr.getvalue()
        if output:
            if (not output.endswith('\n')):
                output += '\n'
            msgLines.append((STDOUT_LINE % output))
        if error:
            if (not error.endswith('\n')):
                error += '\n'
            msgLines.append((STDERR_LINE % error))
    return ''.join(msgLines)
