# Buggy code
def __init__(self, opts):
    # Late setup of the opts grains, so we can log from the grains module
    import salt.loader

    opts["grains"] = salt.loader.grains(opts)
    super(SMinion, self).__init__(opts)

    # Clean out the proc directory (default /var/cache/salt/minion/proc)
    if self.opts.get("file_client", "remote") == "remote" or self.opts.get(
        "use_master_when_local", False
    ):
        install_zmq()
        io_loop = ZMQDefaultLoop.current()
        io_loop.run_sync(lambda: self.eval_master(self.opts, failed=True))
    self.gen_modules(initial_load=True)

    # If configured, cache pillar data on the minion
    if self.opts["file_client"] == "remote" and self.opts.get(
        "minion_pillar_cache", False
    ):
        import salt.utils.yaml

        pdir = os.path.join(self.opts["cachedir"], "pillar")
        if not os.path.isdir(pdir):
            os.makedirs(pdir, 0o700)
        ptop = os.path.join(pdir, "top.sls")
        if self.opts["saltenv"] is not None:
            penv = self.opts["saltenv"]
        else:
            penv = "base"
        cache_top = {penv: {self.opts["id"]: ["cache"]}}
        with salt.utils.files.fopen(ptop, "wb") as fp_:
            salt.utils.yaml.safe_dump(cache_top, fp_)
            os.chmod(ptop, 0o600)
        cache_sls = os.path.join(pdir, "cache.sls")
        with salt.utils.files.fopen(cache_sls, "wb") as fp_:
            salt.utils.yaml.safe_dump(self.opts["pillar"], fp_)
            os.chmod(cache_sls, 0o600)


# 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 "/usr/local/bin/salt-call", line 10, in <module>
    sys.exit(salt_call())
  File "/usr/local/lib/python3.6/site-packages/salt/scripts.py", line 431, in salt_call
    client.run()
  File "/usr/local/lib/python3.6/site-packages/salt/cli/call.py", line 47, in run
    caller = salt.cli.caller.Caller.factory(self.config)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 80, in factory
    return ZeroMQCaller(opts, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 332, in __init__
    super(ZeroMQCaller, self).__init__(opts)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 106, in __init__
    self.minion = salt.minion.SMinion(opts)
  File "/usr/local/lib/python3.6/site-packages/salt/minion.py", line 822, in __init__
    salt.utils.yaml.safe_dump(cache_top, fp_)
  File "/usr/local/lib/python3.6/site-packages/salt/utils/yamldumper.py", line 129, in safe_dump
    return yaml.dump(data, stream, Dumper=SafeOrderedDumper, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/yaml/__init__.py", line 200, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/usr/local/lib64/python3.6/site-packages/yaml/__init__.py", line 188, in dump_all
    dumper.represent(data)
  File "/usr/local/lib64/python3.6/site-packages/yaml/representer.py", line 27, in represent
    self.serialize(node)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 54, in serialize
    self.serialize_node(node, None, None)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 108, in serialize_node
    self.serialize_node(value, node, key)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 107, in serialize_node
    self.serialize_node(key, node, None)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 90, in serialize_node
    style=node.style))
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 115, in emit
    self.state()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 393, in expect_first_block_mapping_key
    return self.expect_block_mapping_key(first=True)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 403, in expect_block_mapping_key
    self.expect_node(mapping=True, simple_key=True)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 244, in expect_node
    self.expect_scalar()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 268, in expect_scalar
    self.process_scalar()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 533, in process_scalar
    self.write_plain(self.analysis.scalar, split)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 1131, in write_plain
    self.stream.write(data)
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "/usr/local/bin/salt-call", line 10, in <module>
    sys.exit(salt_call())
  File "/usr/local/lib/python3.6/site-packages/salt/scripts.py", line 431, in salt_call
    client.run()
  File "/usr/local/lib/python3.6/site-packages/salt/cli/call.py", line 47, in run
    caller = salt.cli.caller.Caller.factory(self.config)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 80, in factory
    return ZeroMQCaller(opts, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 332, in __init__
    super(ZeroMQCaller, self).__init__(opts)
  File "/usr/local/lib/python3.6/site-packages/salt/cli/caller.py", line 106, in __init__
    self.minion = salt.minion.SMinion(opts)
  File "/usr/local/lib/python3.6/site-packages/salt/minion.py", line 822, in __init__
    salt.utils.yaml.safe_dump(cache_top, fp_)
  File "/usr/local/lib/python3.6/site-packages/salt/utils/yamldumper.py", line 129, in safe_dump
    return yaml.dump(data, stream, Dumper=SafeOrderedDumper, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/yaml/__init__.py", line 200, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/usr/local/lib64/python3.6/site-packages/yaml/__init__.py", line 188, in dump_all
    dumper.represent(data)
  File "/usr/local/lib64/python3.6/site-packages/yaml/representer.py", line 27, in represent
    self.serialize(node)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 54, in serialize
    self.serialize_node(node, None, None)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 108, in serialize_node
    self.serialize_node(value, node, key)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 107, in serialize_node
    self.serialize_node(key, node, None)
  File "/usr/local/lib64/python3.6/site-packages/yaml/serializer.py", line 90, in serialize_node
    style=node.style))
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 115, in emit
    self.state()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 393, in expect_first_block_mapping_key
    return self.expect_block_mapping_key(first=True)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 403, in expect_block_mapping_key
    self.expect_node(mapping=True, simple_key=True)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 244, in expect_node
    self.expect_scalar()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 268, in expect_scalar
    self.process_scalar()
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 533, in process_scalar
    self.write_plain(self.analysis.scalar, split)
  File "/usr/local/lib64/python3.6/site-packages/yaml/emitter.py", line 1131, in write_plain
    self.stream.write(data)
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def __init__(self, opts):
    # Late setup of the opts grains, so we can log from the grains module
    import salt.loader

    opts["grains"] = salt.loader.grains(opts)
    super(SMinion, self).__init__(opts)

    # Clean out the proc directory (default /var/cache/salt/minion/proc)
    if self.opts.get("file_client", "remote") == "remote" or self.opts.get(
        "use_master_when_local", False
    ):
        install_zmq()
        io_loop = ZMQDefaultLoop.current()
        io_loop.run_sync(lambda: self.eval_master(self.opts, failed=True))
    self.gen_modules(initial_load=True)

    # If configured, cache pillar data on the minion
    if self.opts["file_client"] == "remote" and self.opts.get(
        "minion_pillar_cache", False
    ):
        import salt.utils.yaml

        pdir = os.path.join(self.opts["cachedir"], "pillar")
        if not os.path.isdir(pdir):
            os.makedirs(pdir, 0o700)
        ptop = os.path.join(pdir, "top.sls")
        if self.opts["saltenv"] is not None:
            penv = self.opts["saltenv"]
        else:
            penv = "base"
        cache_top = {penv: {self.opts["id"]: ["cache"]}}
        with salt.utils.files.fopen(ptop, "wb") as fp_:
            salt.utils.yaml.safe_dump(cache_top, fp_, encoding=SLS_ENCODING)
            os.chmod(ptop, 0o600)
        cache_sls = os.path.join(pdir, "cache.sls")
        with salt.utils.files.fopen(cache_sls, "wb") as fp_:
            salt.utils.yaml.safe_dump(self.opts["pillar"], fp_, encoding=SLS_ENCODING)
            os.chmod(cache_sls, 0o600)