# Buggy code
def parse_magnetlink(url):
    """
    Parses the magnet link provided by the given URL.

    The output of this file consists of:
        -   dn: The display name of the magnet link
        -   xt: The URI containing the file hash of the magnet link
        -   trs: The list of Tracker URLs
    :param url: the URL at which the magnet link can be found
    :return: (dn, xt, trs) tuple, which will be left (None, None, []) if the
    given URL does not lead to a magnet link
    """
    dn = None
    xt = None
    trs = []

    logger.debug("parse_magnetlink() %s", url)

    schema, netloc, path, query, fragment = urlsplit(url)
    if schema == "magnet":
        # magnet url's do not conform to regular url syntax (they
        # do not have a netloc.)  This causes path to contain the
        # query part.
        if "?" in path:
            pre, post = path.split("?", 1)
            if query:
                query = "&".join((post, query))
            else:
                query = post

        for key, value in parse_qsl(query):
            if key == "dn":
                # convert to Unicode
                dn = value.decode() if not isinstance(value, six.text_type) else value

            elif key == "xt" and value.startswith("urn:btih:"):
                # vliegendhart: Adding support for base32 in magnet links (BEP 0009)
                encoded_infohash = value[9:49]
                if len(encoded_infohash) == 32:
                    xt = b32decode(encoded_infohash.upper())
                else:
                    xt = binascii.unhexlify(encoded_infohash)

            elif key == "tr":
                trs.append(value)

        logger.debug("parse_magnetlink() NAME: %s", dn)
        logger.debug("parse_magnetlink() HASH: %s", xt)
        logger.debug("parse_magnetlink() TRACS: %s", trs)

    return dn, xt, trs


# Traceback (UnicodeDecodeError)
byte 0xe3 in position 9: ordinal not in range(128)
/home/vader/.local/lib/python2.7/site-packages/twisted/web/http.py:2070:_finishRequestBody
/home/vader/.local/lib/python2.7/site-packages/twisted/web/http.py:2145:allContentReceived
/home/vader/.local/lib/python2.7/site-packages/twisted/web/http.py:890:requestReceived
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/rest_manager.py:118:process
--- <exception caught here> ---
/home/vader/.local/lib/python2.7/site-packages/twisted/web/server.py:197:process
/home/vader/.local/lib/python2.7/site-packages/twisted/web/server.py:257:render
/home/vader/.local/lib/python2.7/site-packages/twisted/web/resource.py:250:render
<auto generated wrapper of render_PUT() function>:2:render_PUT
/home/vader/.local/lib/python2.7/site-packages/pony/orm/core.py:528:new_func
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/mychannel_endpoint.py:271:render_PUT
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/utilities.py:127:parse_magnetlink
]
Traceback (most recent call last):
File "/home/vader/.local/lib/python2.7/site-packages/twisted/web/server.py", line 197, in process
self.render(resrc)
File "/home/vader/.local/lib/python2.7/site-packages/twisted/web/server.py", line 257, in render
body = resrc.render(self)
File "/home/vader/.local/lib/python2.7/site-packages/twisted/web/resource.py", line 250, in render
return m(request)
File "<auto generated wrapper of render_PUT() function>", line 2, in render_PUT
File "/home/vader/.local/lib/python2.7/site-packages/pony/orm/core.py", line 528, in new_func
result = func(*args, **kwargs)
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/mychannel_endpoint.py", line 271, in render_PUT
_, xt, _ = parse_magnetlink(uri)
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/utilities.py", line 127, in parse_magnetlink
dn = value.decode() if not isinstance(value, six.text_type) else value
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 9: ordinal not in range(128)

# Fixed code
def parse_magnetlink(url):
    """
    Parses the magnet link provided by the given URL.

    The output of this file consists of:
        -   dn: The display name of the magnet link
        -   xt: The URI containing the file hash of the magnet link
        -   trs: The list of Tracker URLs
    :param url: the URL at which the magnet link can be found
    :return: (dn, xt, trs) tuple, which will be left (None, None, []) if the
    given URL does not lead to a magnet link
    """
    dn = None
    xt = None
    trs = []

    logger.debug("parse_magnetlink() %s", url)

    schema, netloc, path, query, fragment = urlsplit(url)
    if schema == "magnet":
        # magnet url's do not conform to regular url syntax (they
        # do not have a netloc.)  This causes path to contain the
        # query part.
        if "?" in path:
            pre, post = path.split("?", 1)
            if query:
                query = "&".join((post, query))
            else:
                query = post

        for key, value in parse_qsl(query):
            if key == "dn":
                # convert to Unicode
                dn = (
                    value.decode("utf-8")
                    if not isinstance(value, six.text_type)
                    else value
                )

            elif key == "xt" and value.startswith("urn:btih:"):
                # vliegendhart: Adding support for base32 in magnet links (BEP 0009)
                encoded_infohash = value[9:49]
                if len(encoded_infohash) == 32:
                    xt = b32decode(encoded_infohash.upper())
                else:
                    xt = binascii.unhexlify(encoded_infohash)

            elif key == "tr":
                trs.append(value)

        logger.debug("parse_magnetlink() NAME: %s", dn)
        logger.debug("parse_magnetlink() HASH: %s", xt)
        logger.debug("parse_magnetlink() TRACS: %s", trs)

    return dn, xt, trs