def _stack_format (stack):
    """Format a stack trace to a message.

    @return: formatted stack message
    @rtype: string
    """
    s = StringIO()
    s.write('Traceback:')
    s.write(os.linesep)
    for frame, fname, lineno, method, lines, dummy in reversed(stack):
        s.write('  File %r, line %d, in %s' % (fname, lineno, method))
        s.write(os.linesep)
        s.write('    %s' % lines[0].lstrip())
        if PRINT_LOCALVARS:
            for key, value in frame.f_locals.items():
                s.write("      %s = " % key)
                # be careful not to cause a new error in the error output
                try:
                    s.write(repr(value))
                    s.write(os.linesep)
                except Exception:
                    s.write("error in repr() call%s" % os.linesep)
    return s.getvalue()
Format a stack trace to a message.

    @return: formatted stack message
    @rtype: string