# Buggy code
def cache_node(node, provider, opts):
    """
    Cache node individually

    .. versionadded:: 2014.7.0
    """
    if isinstance(opts, dict):
        __opts__.update(opts)

    if "update_cachedir" not in __opts__ or not __opts__["update_cachedir"]:
        return

    if not os.path.exists(os.path.join(__opts__["cachedir"], "active")):
        init_cachedir()

    base = os.path.join(__opts__["cachedir"], "active")
    provider, driver = provider.split(":")
    prov_dir = os.path.join(base, driver, provider)
    if not os.path.exists(prov_dir):
        os.makedirs(prov_dir)
    path = os.path.join(prov_dir, "{0}.p".format(node["name"]))
    with salt.utils.fopen(path, "w") as fh_:
        msgpack.dump(node, fh_)


# Traceback (TypeError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/home/bar/Dev/foo/build7/_venv/bin/salt-ssh", line 11, in <module>
load_entry_point('salt==2017.7.2', 'console_scripts', 'salt-ssh')()
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/scripts.py", line 416, in salt_ssh
client.run()
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/cli/ssh.py", line 20, in run
ssh = salt.client.ssh.SSH(self.config)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/client/ssh/__init__.py", line 230, in __init__
self.tgt_type)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/roster/__init__.py", line 105, in targets
targets.update(self.rosters[f_str](tgt, tgt_type))
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/roster/cloud.py", line 45, in targets
minions = __runner__['cache.cloud'](tgt)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/runners/cache.py", line 402, in cloud
cloud_cache = __utils__['cloud.list_cache_nodes_full'](opts=opts, provider=provider)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/utils/cloud.py", line 2768, in list_cache_nodes_full
minions[driver][prov][minion_id] = msgpack.load(fh_)
File "msgpack/_unpacker.pyx", line 164, in msgpack._unpacker.unpack (msgpack/_unpacker.cpp:2622)
File "msgpack/_unpacker.pyx", line 124, in msgpack._unpacker.unpackb (msgpack/_unpacker.cpp:1918)
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/home/bar/Dev/foo/build7/_venv/bin/salt-ssh", line 11, in <module>
load_entry_point('salt==2017.7.2', 'console_scripts', 'salt-ssh')()
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/scripts.py", line 416, in salt_ssh
client.run()
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/cli/ssh.py", line 20, in run
ssh = salt.client.ssh.SSH(self.config)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/client/ssh/__init__.py", line 230, in __init__
self.tgt_type)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/roster/__init__.py", line 105, in targets
targets.update(self.rosters[f_str](tgt, tgt_type))
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/roster/cloud.py", line 45, in targets
minions = __runner__['cache.cloud'](tgt)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/runners/cache.py", line 402, in cloud
cloud_cache = __utils__['cloud.list_cache_nodes_full'](opts=opts, provider=provider)
File "/home/bar/Dev/foo/build7/_venv/lib/python3.5/site-packages/salt/utils/cloud.py", line 2768, in list_cache_nodes_full
minions[driver][prov][minion_id] = msgpack.load(fh_)
File "msgpack/_unpacker.pyx", line 164, in msgpack._unpacker.unpack (msgpack/_unpacker.cpp:2622)
File "msgpack/_unpacker.pyx", line 124, in msgpack._unpacker.unpackb (msgpack/_unpacker.cpp:1918)
TypeError: a bytes-like object is required, not 'str'
EXIT 1

# Fixed code
def cache_node(node, provider, opts):
    """
    Cache node individually

    .. versionadded:: 2014.7.0
    """
    if isinstance(opts, dict):
        __opts__.update(opts)

    if "update_cachedir" not in __opts__ or not __opts__["update_cachedir"]:
        return

    if not os.path.exists(os.path.join(__opts__["cachedir"], "active")):
        init_cachedir()

    base = os.path.join(__opts__["cachedir"], "active")
    provider, driver = provider.split(":")
    prov_dir = os.path.join(base, driver, provider)
    if not os.path.exists(prov_dir):
        os.makedirs(prov_dir)
    path = os.path.join(prov_dir, "{0}.p".format(node["name"]))
    mode = "wb" if six.PY3 else "w"
    with salt.utils.fopen(path, mode) as fh_:
        msgpack.dump(node, fh_)