# Buggy code
def to_str(s, encoding=None):
    """
    Given str, bytes, bytearray, or unicode (py2), return str
    """
    if isinstance(s, str):
        return s
    if six.PY3:
        if isinstance(s, (bytes, bytearray)):
            return s.decode(encoding or __salt_system_encoding__)
        raise TypeError("expected str, bytes, or bytearray")
    else:
        if isinstance(s, bytearray):
            return str(s)
        if isinstance(s, unicode):  # pylint: disable=incompatible-py3-code
            return s.encode(encoding or __salt_system_encoding__)
        raise TypeError("expected str, bytearray, or unicode")


# Traceback (UnicodeDecodeError)
[ERROR   ] There was a profile error: 'utf-8' codec can't decode byte 0xd0 in position 1023: unexpected end of data
Traceback (most recent call last):
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/cloud/cli.py", line 284, in run
self.config.get('names')
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/cloud/__init__.py", line 1457, in run_profile
ret[name] = self.create(vm_)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/cloud/__init__.py", line 1287, in create
output = self.clouds[func](vm_)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/cloud/clouds/saltify.py", line 64, in create
ret = __utils__['cloud.bootstrap'](vm_, __opts__)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/cloud.py", line 532, in bootstrap
deployed = deploy_script(**deploy_kwargs)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/cloud.py", line 1491, in deploy_script
if script_args:
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/cloud.py", line 2225, in root_cmd
retcode = _exec_ssh_cmd(cmd, allow_failure=allow_failure, **kwargs)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/cloud.py", line 1777, in _exec_ssh_cmd
stdout, stderr = proc.recv()
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/vt.py", line 306, in recv
return self._recv(maxsize)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/vt.py", line 674, in _recv
os.read(self.child_fd, maxsize)
File "/Users/aaksenov/.virtualenvs/puppets/lib/python3.5/site-packages/salt/utils/__init__.py", line 2980, in to_str
return s.decode(encoding or __salt_system_encoding__)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 1023: unexpected end of data

# Fixed code
def to_str(s, encoding=None):
    """
    Given str, bytes, bytearray, or unicode (py2), return str
    """
    if isinstance(s, str):
        return s
    if six.PY3:
        if isinstance(s, (bytes, bytearray)):
            # https://docs.python.org/3/howto/unicode.html#the-unicode-type
            # replace error with U+FFFD, REPLACEMENT CHARACTER
            return s.decode(encoding or __salt_system_encoding__, "replace")
        raise TypeError("expected str, bytes, or bytearray")
    else:
        if isinstance(s, bytearray):
            return str(s)
        if isinstance(s, unicode):  # pylint: disable=incompatible-py3-code
            return s.encode(encoding or __salt_system_encoding__)
        raise TypeError("expected str, bytearray, or unicode")