# Buggy code
def get_computer_desc():
    """
    Get PRETTY_HOSTNAME value stored in /etc/machine-info
    If this file doesn't exist or the variable doesn't exist
    return False.

    :return: Value of PRETTY_HOSTNAME if this does not exist False.
    :rtype: str

    CLI Example:

    .. code-block:: bash

        salt '*' system.get_computer_desc
    """
    desc = None
    hostname_cmd = salt.utils.path.which("hostnamectl")
    if hostname_cmd:
        desc = __salt__["cmd.run"](
            [hostname_cmd, "status", "--pretty"], python_shell=False
        )
    else:
        pattern = re.compile(r"^\s*PRETTY_HOSTNAME=(.*)$")
        try:
            with salt.utils.files.fopen("/etc/machine-info", "r") as mach_info:
                for line in mach_info.readlines():
                    line = salt.utils.stringutils.to_unicode(line)
                    match = pattern.match(line)
                    if match:
                        # get rid of whitespace then strip off quotes
                        desc = _strip_quotes(match.group(1).strip())
                        # no break so we get the last occurance
        except IOError:
            return False
    if six.PY3:
        desc = desc.replace('\\"', '"')
    else:
        desc = desc.replace('\\"', '"').decode("string_escape")
    return desc


# Traceback (UnicodeEncodeError)
ID: test1
Function: file.recurse
Name: /root/group2test
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/state.py", line 1746, in call
**cdata['kwargs'])
File "/usr/lib/python2.6/site-packages/salt/loader.py", line 1703, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/states/file.py", line 2807, in recurse
manage_file(dest, src)
File "/usr/lib/python2.6/site-packages/salt/states/file.py", line 2685, in manage_file
**pass_kwargs)
File "/usr/lib/python2.6/site-packages/salt/states/file.py", line 1814, in managed
__env__
File "/usr/lib/python2.6/site-packages/salt/modules/file.py", line 3490, in source_list
contextkey = '{0}_|-{1}_|-{2}'.format(source, source_hash, saltenv)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-34: ordinal not in range(128)
Started: 20:08:21.608398
Duration: 1467.811 ms
Changes:

# Fixed code
def get_computer_desc():
    """
    Get PRETTY_HOSTNAME value stored in /etc/machine-info
    If this file doesn't exist or the variable doesn't exist
    return False.

    :return: Value of PRETTY_HOSTNAME if this does not exist False.
    :rtype: str

    CLI Example:

    .. code-block:: bash

        salt '*' system.get_computer_desc
    """
    hostname_cmd = salt.utils.path.which("hostnamectl")
    if hostname_cmd:
        desc = __salt__["cmd.run"](
            [hostname_cmd, "status", "--pretty"], python_shell=False
        )
    else:
        desc = None
        pattern = re.compile(r"^\s*PRETTY_HOSTNAME=(.*)$")
        try:
            with salt.utils.files.fopen("/etc/machine-info", "r") as mach_info:
                for line in mach_info.readlines():
                    line = salt.utils.stringutils.to_unicode(line)
                    match = pattern.match(line)
                    if match:
                        # get rid of whitespace then strip off quotes
                        desc = _strip_quotes(match.group(1).strip())
                        # no break so we get the last occurance
        except IOError:
            pass

        if desc is None:
            return False

    return desc.replace(r"\"", r'"').replace(r"\n", "\n").replace(r"\t", "\t")