# Buggy code
def __str__(self):
    line = self.expression.start_line
    start = self.expression.start_column
    end = self.expression.end_column

    source = []
    if self.source is not None:
        source = self.source.split("\n")[line - 1 : self.expression.end_line]

        if line == self.expression.end_line:
            length = end - start
        else:
            length = len(source[0]) - start

    result = ""

    result += '  File "%s", line %d, column %d\n\n' % (self.filename, line, start)

    if len(source) == 1:
        result += "  %s\n" % colored.red(source[0])
        result += "  %s%s\n" % (
            " " * (start - 1),
            colored.green("^" + "-" * (length - 1) + "^"),
        )
    if len(source) > 1:
        result += "  %s\n" % colored.red(source[0])
        result += "  %s%s\n" % (" " * (start - 1), colored.green("^" + "-" * length))
        if len(source) > 2:  # write the middle lines
            for line in source[1:-1]:
                result += "  %s\n" % colored.red("".join(line))
                result += "  %s\n" % colored.green("-" * len(line))

        # write the last line
        result += "  %s\n" % colored.red("".join(source[-1]))
        result += "  %s\n" % colored.green("-" * (end - 1) + "^")

    result += colored.yellow("%s: %s\n\n" % (self.__class__.__name__, self.message))

    if not PY3:
        return result.encode("utf-8")
    else:
        return result


# Traceback (UnicodeEncodeError)
Traceback (most recent call last):
File "/usr/bin/hy", line 9, in <module>
load_entry_point('hy==0.11.0', 'console_scripts', 'hy')()
File "/usr/lib/python2.7/site-packages/hy/cmdline.py", line 347, in hy_main
sys.exit(cmdline_handler("hy", sys.argv))
File "/usr/lib/python2.7/site-packages/hy/cmdline.py", line 342, in cmdline_handler
return run_repl(spy=options.spy)
File "/usr/lib/python2.7/site-packages/hy/cmdline.py", line 240, in run_repl
os=platform.system()
File "/usr/lib64/python2.7/code.py", line 243, in interact
more = self.push(line)
File "/usr/lib64/python2.7/code.py", line 265, in push
more = self.runsource(source, self.filename)
File "/usr/lib/python2.7/site-packages/hy/cmdline.py", line 113, in runsource
sys.stderr.write(str(e))
File "/usr/lib/python2.7/site-packages/hy/errors.py", line 104, in __str__
self.message))
File "/usr/lib/python2.7/site-packages/clint/textui/colored.py", line 108, in __radd__
return str(other) + str(self.color_str)
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufdd0' in position 24: ordinal not in range(128)

# Fixed code
def __str__(self):
    line = self.expression.start_line
    start = self.expression.start_column
    end = self.expression.end_column

    source = []
    if self.source is not None:
        source = self.source.split("\n")[line - 1 : self.expression.end_line]

        if line == self.expression.end_line:
            length = end - start
        else:
            length = len(source[0]) - start

    result = ""

    result += '  File "%s", line %d, column %d\n\n' % (self.filename, line, start)

    if len(source) == 1:
        result += "  %s\n" % colored.red(source[0])
        result += "  %s%s\n" % (
            " " * (start - 1),
            colored.green("^" + "-" * (length - 1) + "^"),
        )
    if len(source) > 1:
        result += "  %s\n" % colored.red(source[0])
        result += "  %s%s\n" % (" " * (start - 1), colored.green("^" + "-" * length))
        if len(source) > 2:  # write the middle lines
            for line in source[1:-1]:
                result += "  %s\n" % colored.red("".join(line))
                result += "  %s\n" % colored.green("-" * len(line))

        # write the last line
        result += "  %s\n" % colored.red("".join(source[-1]))
        result += "  %s\n" % colored.green("-" * (end - 1) + "^")

    result += colored.yellow(
        "%s: %s\n\n" % (self.__class__.__name__, self.message.encode("utf-8"))
    )

    return result