# Buggy code
def get_encodings():
    """
    return a list of string encodings to try
    """
    encodings = []
    loc = locale.getdefaultlocale()[-1]
    if loc:
        encodings.append(loc)
    encodings.append(sys.getdefaultencoding())
    encodings.extend(["utf-8", "latin-1"])
    return encodings


# Traceback (ValueError)
[INFO    ] Loading fresh modules for state activity
[INFO    ] Fetching file from saltenv 'qa', ** done ** 'top.sls'
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
ValueError: unknown locale: UTF-8
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 123, in salt_call
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/__init__.py", line 422, in run
caller.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/caller.py", line 237, in run
self.opts)
File "/usr/lib/python2.7/dist-packages/salt/output/__init__.py", line 44, in display_output
display_data = get_printout(out, opts)(data).rstrip()
File "/usr/lib/python2.7/dist-packages/salt/output/highstate.py", line 74, in output
return _format_host(host, hostdata)[0]
File "/usr/lib/python2.7/dist-packages/salt/output/highstate.py", line 209, in _format_host
comment = salt.utils.sdecode(ret['comment'])
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 2512, in sdecode
encodings = get_encodings()
File "/usr/lib/python2.7/dist-packages/salt/utils/decorators/__init__.py", line 193, in _memoize
cache[args] = func(*args)
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 2496, in get_encodings
loc = locale.getdefaultlocale()[-1]
File "/usr/lib/python2.7/locale.py", line 543, in getdefaultlocale
return _parse_localename(localename)
File "/usr/lib/python2.7/locale.py", line 475, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 123, in salt_call
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/__init__.py", line 422, in run
caller.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/caller.py", line 237, in run
self.opts)
File "/usr/lib/python2.7/dist-packages/salt/output/__init__.py", line 44, in display_output
display_data = get_printout(out, opts)(data).rstrip()
File "/usr/lib/python2.7/dist-packages/salt/output/highstate.py", line 74, in output
return _format_host(host, hostdata)[0]
File "/usr/lib/python2.7/dist-packages/salt/output/highstate.py", line 209, in _format_host
comment = salt.utils.sdecode(ret['comment'])
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 2512, in sdecode
encodings = get_encodings()
File "/usr/lib/python2.7/dist-packages/salt/utils/decorators/__init__.py", line 193, in _memoize
cache[args] = func(*args)
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 2496, in get_encodings
loc = locale.getdefaultlocale()[-1]
File "/usr/lib/python2.7/locale.py", line 543, in getdefaultlocale
return _parse_localename(localename)
File "/usr/lib/python2.7/locale.py", line 475, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8
vagrant@web01:~$

# Fixed code
def get_encodings():
    """
    return a list of string encodings to try
    """
    encodings = []

    try:
        loc_enc = locale.getdefaultlocale()[-1]
    except (ValueError, IndexError):  # system locale is nonstandard or malformed
        loc_enc = None
    if loc_enc:
        encodings.append(loc_enc)

    try:
        enc = sys.getdefaultencoding()
    except ValueError:  # system encoding is nonstandard or malformed
        enc = None
    if enc:
        encodings.append(enc)

    encodings.extend(["utf-8", "latin-1"])
    return encodings