def _format_syntax_error_message(exception):
    """Returns a nicely formatted SyntaxError message that emulates
    what the Python interpreter outputs, e.g.:

    > File "raven.py", line 3
    >   st.write('Hello world!!'))
    >                            ^
    > SyntaxError: invalid syntax

    Parameters
    ----------
    exception : SyntaxError

    Returns
    -------
    str

    """
    if exception.text:
        return (
            """File "%(filename)s", line %(lineno)d
  %(text)s
  %(caret_indent)s^
%(errname)s: %(msg)s"""
             % {'filename': exception.filename, 'lineno': exception.lineno,
            'text': exception.text.rstrip(), 'caret_indent': ' ' * max(
            exception.offset - 1, 0), 'errname': type(exception).__name__,
            'msg': exception.msg})
    return str(exception)
