# Buggy code
def exec_code(code, filename, ns_globals, ns_locals=None):
    """Execute code and display any exception."""
    global SHOW_INVALID_SYNTAX_MSG

    if PY2:
        filename = encode(filename)
        code = encode(code)

    ipython_shell = get_ipython()
    is_ipython = os.path.splitext(filename)[1] == ".ipy"
    try:
        if not is_ipython:
            # TODO: remove the try-except and let the SyntaxError raise
            # Because there should not be ipython code in a python file
            try:
                compiled = compile(code, filename, "exec")
            except SyntaxError as e:
                try:
                    compiled = compile(transform_cell(code), filename, "exec")
                except SyntaxError:
                    if PY2:
                        raise e
                    else:
                        # Need to call exec to avoid Syntax Error in Python 2.
                        # TODO: remove exec when dropping Python 2 support.
                        exec("raise e from None")
                else:
                    if SHOW_INVALID_SYNTAX_MSG:
                        _print(
                            "\nWARNING: This is not valid Python code. "
                            "If you want to use IPython magics, "
                            "flexible indentation, and prompt removal, "
                            "please save this file with the .ipy extension. "
                            "This will be an error in a future version of "
                            "Spyder.\n"
                        )
                        SHOW_INVALID_SYNTAX_MSG = False
        else:
            compiled = compile(transform_cell(code), filename, "exec")

        exec(compiled, ns_globals, ns_locals)
    except SystemExit as status:
        # ignore exit(0)
        if status.code:
            ipython_shell.showtraceback(exception_only=True)
    except BaseException as error:
        if isinstance(error, bdb.BdbQuit) and ipython_shell.kernel._pdb_obj:
            # Ignore BdbQuit if we are debugging, as it is expected.
            ipython_shell.kernel._pdb_obj = None
        else:
            # We ignore the call to exec
            ipython_shell.showtraceback(tb_offset=1)


# Traceback (RuntimeError)
In [1]: runfile('/home/user/.config/spyder-py3/temp.py', wdir='/home/user/.config/spyder-py3', post_mortem=True)
Traceback (most recent call last):

File "<ipython-input-1-527ce6822f9c>", line 1, in <module>
runfile('/home/user/.config/spyder-py3/temp.py', wdir='/home/user/.config/spyder-py3', post_mortem=True)

File "/home/user/miniconda3/envs/test3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "/home/user/miniconda3/envs/test3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/home/user/.config/spyder-py3/temp.py", line 1, in <module>
raise RuntimeError

RuntimeError

****************************************
Entering post mortem debugging...
****************************************
/home/user/.config/spyder-py3/temp.py(1)<module>()
----> 1 raise RuntimeError


ipdb>

# Fixed code
def exec_code(code, filename, ns_globals, ns_locals=None, post_mortem=False):
    """Execute code and display any exception."""
    global SHOW_INVALID_SYNTAX_MSG

    if PY2:
        filename = encode(filename)
        code = encode(code)

    ipython_shell = get_ipython()
    is_ipython = os.path.splitext(filename)[1] == ".ipy"
    try:
        if not is_ipython:
            # TODO: remove the try-except and let the SyntaxError raise
            # Because there should not be ipython code in a python file
            try:
                compiled = compile(code, filename, "exec")
            except SyntaxError as e:
                try:
                    compiled = compile(transform_cell(code), filename, "exec")
                except SyntaxError:
                    if PY2:
                        raise e
                    else:
                        # Need to call exec to avoid Syntax Error in Python 2.
                        # TODO: remove exec when dropping Python 2 support.
                        exec("raise e from None")
                else:
                    if SHOW_INVALID_SYNTAX_MSG:
                        _print(
                            "\nWARNING: This is not valid Python code. "
                            "If you want to use IPython magics, "
                            "flexible indentation, and prompt removal, "
                            "please save this file with the .ipy extension. "
                            "This will be an error in a future version of "
                            "Spyder.\n"
                        )
                        SHOW_INVALID_SYNTAX_MSG = False
        else:
            compiled = compile(transform_cell(code), filename, "exec")

        exec(compiled, ns_globals, ns_locals)
    except SystemExit as status:
        # ignore exit(0)
        if status.code:
            ipython_shell.showtraceback(exception_only=True)
    except BaseException as error:
        if isinstance(error, bdb.BdbQuit) and ipython_shell.kernel._pdb_obj:
            # Ignore BdbQuit if we are debugging, as it is expected.
            ipython_shell.kernel._pdb_obj = None
        elif post_mortem and isinstance(error, Exception):
            error_type, error, tb = sys.exc_info()
            post_mortem_excepthook(error_type, error, tb.tb_next)
        else:
            # We ignore the call to exec
            ipython_shell.showtraceback(tb_offset=1)