# Buggy code
def query_from_mpd_list_format(field, mpd_query):
    """
    Converts an MPD ``list`` query to a Mopidy query.
    """
    # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5
    # See https://github.com/mopidy/mopidy/issues/302 for details
    if mpd_query is None:
        return {}
    try:
        # shlex does not seem to be friends with unicode objects
        tokens = shlex.split(mpd_query.encode("utf-8"))
    except ValueError as error:
        if str(error) == "No closing quotation":
            raise MpdArgError("Invalid unquoted character", command="list")
        else:
            raise
    tokens = [t.decode("utf-8") for t in tokens]
    if len(tokens) == 1:
        if field == "album":
            if not tokens[0]:
                raise ValueError
            return {b"artist": [tokens[0]]}  # See above NOTE
        else:
            raise MpdArgError('should be "Album" for 3 arguments', command="list")
    elif len(tokens) % 2 == 0:
        query = {}
        while tokens:
            key = str(tokens[0].lower())  # See above NOTE
            value = tokens[1]
            tokens = tokens[2:]
            if key not in ("artist", "album", "date", "genre"):
                raise MpdArgError("not able to parse args", command="list")
            if not value:
                raise ValueError
            if key in query:
                query[key].append(value)
            else:
                query[key] = [value]
        return query
    else:
        raise MpdArgError("not able to parse args", command="list")


# Traceback (TypeError)
erik@faust:~$  mopidy
Traceback (most recent call last):
File "/usr/local/bin/mopidy", line 5, in <module>
main()
File "/usr/local/lib64/python2.6/site-packages/mopidy/__main__.py", line 48, in main
options = parse_options()
File "/usr/local/lib64/python2.6/site-packages/mopidy/__main__.py", line 85, in parse_options
help='show GStreamer help options')
File "/usr/lib64/python2.6/optparse.py", line 1012, in add_option
raise TypeError, "invalid arguments"
TypeError: invalid arguments
erik@faust:~$  python --version
Python 2.6
erik@faust:~$  uname -a
Linux faust 3.0.13-0.27-default #1 SMP Wed Feb 15 13:33:49 UTC 2012 (d73692b) x86_64 x86_64 x86_64 GNU/Linux
erik@faust:~$  cat /etc/SuSE-release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
erik@faust:~$

# Fixed code
def query_from_mpd_list_format(field, mpd_query):
    """
    Converts an MPD ``list`` query to a Mopidy query.
    """
    if mpd_query is None:
        return {}
    try:
        # shlex does not seem to be friends with unicode objects
        tokens = shlex.split(mpd_query.encode("utf-8"))
    except ValueError as error:
        if str(error) == "No closing quotation":
            raise MpdArgError("Invalid unquoted character", command="list")
        else:
            raise
    tokens = [t.decode("utf-8") for t in tokens]
    if len(tokens) == 1:
        if field == "album":
            if not tokens[0]:
                raise ValueError
            return {"artist": [tokens[0]]}  # See above NOTE
        else:
            raise MpdArgError('should be "Album" for 3 arguments', command="list")
    elif len(tokens) % 2 == 0:
        query = {}
        while tokens:
            key = str(tokens[0].lower())  # See above NOTE
            value = tokens[1]
            tokens = tokens[2:]
            if key not in ("artist", "album", "date", "genre"):
                raise MpdArgError("not able to parse args", command="list")
            if not value:
                raise ValueError
            if key in query:
                query[key].append(value)
            else:
                query[key] = [value]
        return query
    else:
        raise MpdArgError("not able to parse args", command="list")