# Buggy code
def runfile(
    filename=None,
    args=None,
    wdir=None,
    namespace=None,
    post_mortem=False,
    current_namespace=False,
):
    """
    Run filename
    args: command line arguments (string)
    wdir: working directory
    namespace: namespace for execution
    post_mortem: boolean, whether to enter post-mortem mode on error
    current_namespace: if true, run the file in the current namespace
    """
    ipython_shell = get_ipython()
    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
    if PY2:
        filename = encode(filename)
    if __umr__.enabled:
        __umr__.run()
    if args is not None and not isinstance(args, basestring):
        raise TypeError("expected a character buffer object")
    try:
        file_code = get_file_code(filename)
    except Exception:
        _print(
            "This command failed to be executed because an error occurred"
            " while trying to get the file code from Spyder's"
            " editor. The error was:\n\n"
        )
        get_ipython().showtraceback(exception_only=True)
        return
    if file_code is None:
        _print("Could not get code from editor.\n")
        return

    with NamespaceManager(
        filename, namespace, current_namespace, file_code=file_code
    ) as (ns_globals, ns_locals):
        sys.argv = [filename]
        if args is not None:
            for arg in shlex.split(args):
                sys.argv.append(arg)
        if wdir is not None:
            try:
                wdir = wdir.decode("utf-8")
            except (UnicodeError, TypeError, AttributeError):
                # UnicodeError, TypeError --> eventually raised in Python 2
                # AttributeError --> systematically raised in Python 3
                pass
            if os.path.isdir(wdir):
                os.chdir(wdir)
            else:
                _print("Working directory {} doesn't exist.\n".format(wdir))

        if __umr__.has_cython:
            # Cython files
            with io.open(filename, encoding="utf-8") as f:
                ipython_shell.run_cell_magic("cython", "", f.read())
        else:
            exec_code(
                file_code, filename, ns_globals, ns_locals, post_mortem=post_mortem
            )

        sys.argv = [""]


# Traceback (TypeError)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'
ERROR:traitlets:Exception in comm_msg for c64971a0e78c11ea814f83083ff53143
Traceback (most recent call last):
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'
ERROR:traitlets:Exception in comm_msg for c64971a0e78c11ea814f83083ff53143
Traceback (most recent call last):
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 177, in comm_msg
comm.handle_msg(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/qtconsole/comms.py", line 265, in handle_msg
return self._msg_callback(msg)
File "/home/dave/envCAML_py385/lib/python3.8/site-packages/spyder_kernels/comms/commbase.py", line 330, in _comm_message
self._message_handlers[spyder_msg_type](
TypeError: _handle_remote_call_reply() missing 1 required positional argument: 'load_exception'

# Fixed code
def runfile(
    filename=None,
    args=None,
    wdir=None,
    namespace=None,
    post_mortem=False,
    current_namespace=False,
):
    """
    Run filename
    args: command line arguments (string)
    wdir: working directory
    namespace: namespace for execution
    post_mortem: boolean, whether to enter post-mortem mode on error
    current_namespace: if true, run the file in the current namespace
    """
    ipython_shell = get_ipython()
    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
    if PY2:
        filename = encode(filename)
    if __umr__.enabled:
        __umr__.run()
    if args is not None and not isinstance(args, basestring):
        raise TypeError("expected a character buffer object")
    try:
        file_code = get_file_code(filename)
    except Exception:
        _print(
            "This command failed to be executed because an error occurred"
            " while trying to get the file code from Spyder's"
            " editor. The error was:\n\n"
        )
        get_ipython().showtraceback(exception_only=True)
        return
    if file_code is None:
        _print("Could not get code from editor.\n")
        return

    with NamespaceManager(
        filename, namespace, current_namespace, file_code=file_code
    ) as (ns_globals, ns_locals):
        sys.argv = [filename]
        if args is not None:
            for arg in shlex.split(args):
                sys.argv.append(arg)
        if wdir is not None:
            if PY2:
                try:
                    wdir = wdir.decode("utf-8")
                except (UnicodeError, TypeError):
                    # UnicodeError, TypeError --> eventually raised in Python 2
                    pass
            if os.path.isdir(wdir):
                os.chdir(wdir)
                # See https://github.com/spyder-ide/spyder/issues/13632
                if "multiprocessing.process" in sys.modules:
                    try:
                        import multiprocessing.process

                        multiprocessing.process.ORIGINAL_DIR = os.path.abspath(wdir)
                    except Exception:
                        pass
            else:
                _print("Working directory {} doesn't exist.\n".format(wdir))

        if __umr__.has_cython:
            # Cython files
            with io.open(filename, encoding="utf-8") as f:
                ipython_shell.run_cell_magic("cython", "", f.read())
        else:
            exec_code(
                file_code, filename, ns_globals, ns_locals, post_mortem=post_mortem
            )

        sys.argv = [""]