# Buggy code
def __init__(self, opts, context=None):
    # 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, context=context or {})

    # 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)
/e/a/sources.list.d # ❯❯❯ salt-call test.version
[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/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000', 'console_scripts', 'salt-call')()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python3/dist-packages/salt/cli/call.py", line 47, in run
caller = salt.cli.caller.Caller.factory(self.config)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 63, in factory
return ZeroMQCaller(opts, **kwargs)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 312, in __init__
super(ZeroMQCaller, self).__init__(opts)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 87, in __init__
self.minion = salt.minion.SMinion(opts)
File "/usr/lib/python3/dist-packages/salt/minion.py", line 868, in __init__
salt.utils.yaml.safe_dump(cache_top, fp_)
File "/usr/lib/python3/dist-packages/salt/utils/yamldumper.py", line 124, in safe_dump
return yaml.dump(data, stream, Dumper=SafeOrderedDumper, **kwargs)
File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 200, in dump
return dump_all([data], stream, Dumper=Dumper, **kwds)
File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 188, in dump_all
dumper.represent(data)
File "/usr/lib/python3/dist-packages/yaml/representer.py", line 27, in represent
self.serialize(node)
File "ext/_yaml.pyx", line 1350, in _yaml.CEmitter.serialize (ext/_yaml.c:19880)
File "ext/_yaml.pyx", line 1525, in _yaml.output_handler (ext/_yaml.c:22762)
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000', 'console_scripts', 'salt-call')()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python3/dist-packages/salt/cli/call.py", line 47, in run
caller = salt.cli.caller.Caller.factory(self.config)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 63, in factory
return ZeroMQCaller(opts, **kwargs)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 312, in __init__
super(ZeroMQCaller, self).__init__(opts)
File "/usr/lib/python3/dist-packages/salt/cli/caller.py", line 87, in __init__
self.minion = salt.minion.SMinion(opts)
File "/usr/lib/python3/dist-packages/salt/minion.py", line 868, in __init__
salt.utils.yaml.safe_dump(cache_top, fp_)
File "/usr/lib/python3/dist-packages/salt/utils/yamldumper.py", line 124, in safe_dump
return yaml.dump(data, stream, Dumper=SafeOrderedDumper, **kwargs)
File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 200, in dump
return dump_all([data], stream, Dumper=Dumper, **kwds)
File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 188, in dump_all
dumper.represent(data)
File "/usr/lib/python3/dist-packages/yaml/representer.py", line 27, in represent
self.serialize(node)
File "ext/_yaml.pyx", line 1350, in _yaml.CEmitter.serialize (ext/_yaml.c:19880)
File "ext/_yaml.pyx", line 1525, in _yaml.output_handler (ext/_yaml.c:22762)
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def __init__(self, opts, context=None):
    # 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, context=context or {})

    # 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)