def _indent(stream, indent, *msgs):

    """ write a message to a text stream, with indentation. Also ensures that
    the output encoding of the messages is safe for writing. """

    for x in range(0, indent):
        stream.write("  ")
    for x in msgs:
        # Any nicer way? In Py2 x can be 'str' or 'unicode'.
        stream.write(x.encode("ascii", "backslashreplace").decode("ascii"))
    stream.write("\n")
write a message to a text stream, with indentation. Also ensures that
    the output encoding of the messages is safe for writing.