# Buggy code
def output(grains):
    """
    Output the grains in a clean way
    """
    colors = salt.utils.get_colors(__opts__.get("color"))
    ret = ""
    for id_, minion in grains.items():
        ret += "{0}{1}{2}:\n".format(colors["GREEN"], id_, colors["ENDC"])
        for key in sorted(minion):
            ret += "  {0}{1}{2}:".format(colors["CYAN"], key, colors["ENDC"])
            if key == "cpu_flags":
                ret += colors["LIGHT_GREEN"]
                for val in minion[key]:
                    ret += " {0}".format(val)
                ret += "{0}\n".format(colors["ENDC"])
            elif key == "pythonversion":
                ret += " {0}".format(colors["LIGHT_GREEN"])
                for val in minion[key]:
                    ret += "{0}.".format(str(val))
                ret = ret[:-1]
                ret += "{0}\n".format(colors["ENDC"])
            elif isinstance(minion[key], list):
                for val in minion[key]:
                    ret += "\n      {0}{1}{2}".format(
                        colors["LIGHT_GREEN"], val, colors["ENDC"]
                    )
                ret += "\n"
            else:
                ret += " {0}{1}{2}\n".format(
                    colors["LIGHT_GREEN"], minion[key], colors["ENDC"]
                )
    return ret


# Traceback (UnicodeEncodeError)
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 9: ordinal not in range(128)
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 726, in <module>
File "__main__.py", line 332, in bootstrap
File "__main__.py", line 359, in chainload
File "__main__.py", line 715, in _chainload
File "__main__.py", line 128, in <module>
File "__main__salt-call__.py", line 14, in <module>
File "salt/scripts.py", line 82, in salt_call
File "salt/cli/__init__.py", line 319, in run
File "salt/cli/caller.py", line 148, in run
File "salt/output/__init__.py", line 35, in display_output
File "salt/output/grains.py", line 36, in output
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 9: ordinal not in range(128)
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 726, in <module>
File "__main__.py", line 332, in bootstrap
File "__main__.py", line 359, in chainload
File "__main__.py", line 715, in _chainload
File "__main__.py", line 128, in <module>
File "__main__salt-call__.py", line 14, in <module>
File "salt/scripts.py", line 82, in salt_call
File "salt/cli/__init__.py", line 319, in run
File "salt/cli/caller.py", line 148, in run
File "salt/output/__init__.py", line 35, in display_output
File "salt/output/grains.py", line 36, in output
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 9: ordinal not in range(128)

# Fixed code
def output(grains):
    """
    Output the grains in a clean way
    """
    colors = salt.utils.get_colors(__opts__.get("color"))
    encoding = grains["locale_info"]["defaultencoding"]
    if encoding == "unknown":
        encoding = "utf-8"  # let's hope for the best

    ret = ""
    for id_, minion in grains.items():
        ret += "{0}{1}{2}:\n".format(
            colors["GREEN"], id_.decode(encoding), colors["ENDC"]
        )
        for key in sorted(minion):
            ret += "  {0}{1}{2}:".format(
                colors["CYAN"], key.decode(encoding), colors["ENDC"]
            )
            if key == "cpu_flags":
                ret += colors["LIGHT_GREEN"]
                for val in minion[key]:
                    ret += " {0}".format(val.decode(encoding))
                ret += "{0}\n".format(colors["ENDC"])
            elif key == "pythonversion":
                ret += " {0}".format(colors["LIGHT_GREEN"])
                for val in minion[key]:
                    ret += "{0}.".format(unicode(val))
                ret = ret[:-1]
                ret += "{0}\n".format(colors["ENDC"])
            elif isinstance(minion[key], list):
                for val in minion[key]:
                    ret += "\n      {0}{1}{2}".format(
                        colors["LIGHT_GREEN"], val.decode(encoding), colors["ENDC"]
                    )
                ret += "\n"
            else:
                ret += " {0}{1}{2}\n".format(
                    colors["LIGHT_GREEN"], minion[key].decode(encoding), colors["ENDC"]
                )
    return ret