# Buggy code
def initialize():
    """Replace original functions if the fs encoding is not utf-8."""
    if hasattr(sys, "_called_from_test"):
        return

    affected_functions = {
        certifi: ["where", "old_where"],
        glob: ["glob"],
        io: ["open"],
        os: [
            "access",
            "chdir",
            "listdir",
            "makedirs",
            "mkdir",
            "remove",
            "rename",
            "renames",
            "rmdir",
            "stat",
            "unlink",
            "utime",
            "walk",
        ],
        os.path: [
            "abspath",
            "basename",
            "dirname",
            "exists",
            "getctime",
            "getmtime",
            "getsize",
            "isabs",
            "isdir",
            "isfile",
            "islink",
            "join",
            "normcase",
            "normpath",
            "realpath",
            "relpath",
            "split",
            "splitext",
        ],
        shutil: ["copyfile", "copymode", "move", "rmtree"],
        tarfile: ["is_tarfile"],
    }

    # pyOpenSSL 0.14-1 bug: it can't handle unicode input.
    # pyOpenSSL fix -> https://github.com/pyca/pyopenssl/pull/209
    # Our bug: https://github.com/pymedusa/Medusa/issues/1422
    handle_output_map = {certifi: _handle_output_b}

    if os.name != "nt":
        affected_functions[os].extend(["chmod", "chown", "link", "statvfs", "symlink"])

    handle_arg = (
        _handle_input
        if not fs_encoding or fs_encoding.lower() != "utf-8"
        else lambda x: x
    )

    for k, v in affected_functions.items():
        handle_output = handle_output_map.get(k, _handle_output_u)
        for f in v:
            setattr(k, f, make_closure(getattr(k, f), handle_arg, handle_output))


# Traceback (TypeError)
python SickBeard.py --help
Traceback (most recent call last):
File "SickBeard.py", line 4, in <module>
from medusa.__main__ import main
File "/opt/share/medusa/Medusa/medusa/__main__.py", line 67, in <module>
from . import (
File "/opt/share/medusa/Medusa/medusa/providers/__init__.py", line 10, in <module>
from medusa.providers.nzb import (
File "/opt/share/medusa/Medusa/medusa/providers/nzb/anizb.py", line 14, in <module>
from medusa.providers.nzb.nzb_provider import NZBProvider
File "/opt/share/medusa/Medusa/medusa/providers/nzb/nzb_provider.py", line 8, in <module>
from medusa.providers.generic_provider import GenericProvider
File "/opt/share/medusa/Medusa/medusa/providers/generic_provider.py", line 18, in <module>
from medusa import (
File "/opt/share/medusa/Medusa/medusa/config.py", line 30, in <module>
from medusa.version_checker import CheckVersion
File "/opt/share/medusa/Medusa/medusa/version_checker.py", line 31, in <module>
from medusa.github_client import get_github_repo
File "/opt/share/medusa/Medusa/medusa/github_client.py", line 6, in <module>
import github
File "/opt/share/medusa/Medusa/ext/github/__init__.py", line 37, in <module>
from MainClass import Github, GithubIntegration
File "/opt/share/medusa/Medusa/ext/github/MainClass.py", line 34, in <module>
import jwt
File "/opt/share/medusa/Medusa/ext/jwt/__init__.py", line 19, in <module>
from .api_jwt import (
File "/opt/share/medusa/Medusa/ext/jwt/api_jwt.py", line 8, in <module>
from .api_jws import PyJWS
File "/opt/share/medusa/Medusa/ext/jwt/api_jws.py", line 7, in <module>
from .algorithms import (
File "/opt/share/medusa/Medusa/ext/jwt/algorithms.py", line 8, in <module>
from .utils import (
File "/opt/share/medusa/Medusa/ext/jwt/utils.py", line 8, in <module>
from cryptography.hazmat.primitives.asymmetric.utils import (
File "/opt/lib/python2.7/site-packages/cryptography/hazmat/primitives/asymmetric/utils.py", line 9, in <module>
File "/opt/lib/python2.7/site-packages/asn1crypto/algos.py", line 23, in <module>
from ._int import fill_width
File "/opt/lib/python2.7/site-packages/asn1crypto/_int.py", line 56, in <module>
from ._perf._big_num_ctypes import libcrypto
File "/opt/lib/python2.7/site-packages/asn1crypto/_perf/_big_num_ctypes.py", line 35, in <module>
libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto')
File "/opt/lib/python2.7/ctypes/util.py", line 274, in find_library
File "/opt/lib/python2.7/ctypes/util.py", line 107, in _findLib_gcc
File "/opt/lib/python2.7/tempfile.py", line 431, in close
File "/opt/share/medusa/Medusa/medusa/init/filesystem.py", line 85, in <lambda>
return lambda *args, **kwargs: handle_output(f(*[handle_arg(arg) for arg in args], **{k: handle_arg(arg) for k, arg in kwargs.items()}))
TypeError: remove() takes exactly 1 argument (2 given)

# Fixed code
def initialize():
    """Replace original functions if the fs encoding is not utf-8."""
    if hasattr(sys, "_called_from_test"):
        return

    affected_functions = {
        certifi: ["where", "old_where"],
        glob: ["glob"],
        io: ["open"],
        os: [
            "access",
            "chdir",
            "listdir",
            "makedirs",
            "mkdir",
            "remove",
            "rename",
            "renames",
            "rmdir",
            "stat",
            "unlink",
            "utime",
            "walk",
        ],
        os.path: [
            "abspath",
            "basename",
            "dirname",
            "exists",
            "getctime",
            "getmtime",
            "getsize",
            "isabs",
            "isdir",
            "isfile",
            "islink",
            "join",
            "normcase",
            "normpath",
            "realpath",
            "relpath",
            "split",
            "splitext",
        ],
        shutil: ["copyfile", "copymode", "move", "rmtree"],
        tarfile: ["is_tarfile"],
    }

    # pyOpenSSL 0.14-1 bug: it can't handle unicode input.
    # pyOpenSSL fix -> https://github.com/pyca/pyopenssl/pull/209
    # Our bug: https://github.com/pymedusa/Medusa/issues/1422
    handle_output_map = {certifi: _handle_output_b}

    if os.name != "nt":
        affected_functions[os].extend(["chmod", "chown", "link", "statvfs", "symlink"])

    if not fs_encoding or fs_encoding.lower() != "utf-8":
        handle_input = _handle_input
    else:
        handle_input = None

    for k, v in affected_functions.items():
        handle_output = handle_output_map.get(k, _handle_output_u)
        for f in v:
            setattr(k, f, make_closure(getattr(k, f), handle_input, handle_output))