# Buggy code
def runcell(cellname, filename=None):
    """
    Run a code cell from an editor as a file.

    Currently looks for code in an `ipython` property called `cell_code`.
    This property must be set by the editor prior to calling this function.
    This function deletes the contents of `cell_code` upon completion.

    Parameters
    ----------
    cellname : str or int
        Cell name or index.
    filename : str
        Needed to allow for proper traceback links.
    """
    if filename is None:
        filename = get_current_file_name()
        if filename is None:
            return
    else:
        # get_debugger replaces \\ by / so we must undo that here
        # Otherwise code caching doesn't work
        if os.name == "nt":
            filename = filename.replace("/", "\\")
    try:
        filename = filename.decode("utf-8")
    except (UnicodeError, TypeError, AttributeError):
        # UnicodeError, TypeError --> eventually raised in Python 2
        # AttributeError --> systematically raised in Python 3
        pass
    ipython_shell = get_ipython()
    try:
        # Get code from spyder
        cell_code = frontend_request().run_cell(cellname, filename)
    except Exception:
        _print(
            "This command failed to be executed because an error occurred"
            " while trying to get the cell code from Spyder's"
            " editor. The error was:\n\n"
        )
        get_ipython().showtraceback(exception_only=True)
        return

    if not cell_code or cell_code.strip() == "":
        _print("Nothing to execute, this cell is empty.\n")
        return

    # Trigger `post_execute` to exit the additional pre-execution.
    # See Spyder PR #7310.
    ipython_shell.events.trigger("post_execute")
    try:
        file_code = get_file_code(filename)
    except Exception:
        file_code = None
    with NamespaceManager(filename, current_namespace=True, file_code=file_code) as (
        ns_globals,
        ns_locals,
    ):
        exec_code(cell_code, filename, ns_globals, ns_locals)


# 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 runcell(cellname, filename=None, post_mortem=False):
    """
    Run a code cell from an editor as a file.

    Currently looks for code in an `ipython` property called `cell_code`.
    This property must be set by the editor prior to calling this function.
    This function deletes the contents of `cell_code` upon completion.

    Parameters
    ----------
    cellname : str or int
        Cell name or index.
    filename : str
        Needed to allow for proper traceback links.
    """
    if filename is None:
        filename = get_current_file_name()
        if filename is None:
            return
    else:
        # get_debugger replaces \\ by / so we must undo that here
        # Otherwise code caching doesn't work
        if os.name == "nt":
            filename = filename.replace("/", "\\")
    try:
        filename = filename.decode("utf-8")
    except (UnicodeError, TypeError, AttributeError):
        # UnicodeError, TypeError --> eventually raised in Python 2
        # AttributeError --> systematically raised in Python 3
        pass
    ipython_shell = get_ipython()
    try:
        # Get code from spyder
        cell_code = frontend_request().run_cell(cellname, filename)
    except Exception:
        _print(
            "This command failed to be executed because an error occurred"
            " while trying to get the cell code from Spyder's"
            " editor. The error was:\n\n"
        )
        get_ipython().showtraceback(exception_only=True)
        return

    if not cell_code or cell_code.strip() == "":
        _print("Nothing to execute, this cell is empty.\n")
        return

    # Trigger `post_execute` to exit the additional pre-execution.
    # See Spyder PR #7310.
    ipython_shell.events.trigger("post_execute")
    try:
        file_code = get_file_code(filename)
    except Exception:
        file_code = None
    with NamespaceManager(filename, current_namespace=True, file_code=file_code) as (
        ns_globals,
        ns_locals,
    ):
        exec_code(cell_code, filename, ns_globals, ns_locals, post_mortem=post_mortem)