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

    """
    return (
        'File "%(filename)s", line %(lineno)d\n'
        '  %(text)s\n'
        '  %(caret_indent)s^\n'
        '%(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,
        })