# Buggy code
def _safe_extended_peer_info(ext_peer_info):
    """
    Given a string describing peer info, return a json.twisted_dumps() safe representation.

    :param ext_peer_info: the string to convert to a dumpable format
    :return: the safe string
    """
    # First see if we can use this as-is
    if not ext_peer_info:
        ext_peer_info = ""
    try:
        json.twisted_dumps(ext_peer_info)
        return ext_peer_info
    except UnicodeDecodeError:
        # We might have some special unicode characters in here
        return "".join([unichr(ord(c)) for c in ext_peer_info])


# Traceback (TypeError)
ERROR   1575306209.62              rest_manager:83   (RESTRequest)  [Failure instance: Traceback: <class 'TypeError'>: Object of type bytes is not JSON serializable
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/http.py:2146:lineReceived
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/http.py:2237:allContentReceived
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/http.py:937:requestReceived
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/rest_manager.py:118:process
--- <exception caught here> ---
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/server.py:217:process
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/server.py:284:render
/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/resource.py:265:render
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py:284:render_GET
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py:42:_safe_extended_peer_info
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/json_util.py:118:twisted_dumps
/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/json_util.py:100:dumps
/usr/lib/python3.7/json/__init__.py:231:dumps
/usr/lib/python3.7/json/encoder.py:199:encode
/usr/lib/python3.7/json/encoder.py:257:iterencode
/usr/lib/python3.7/json/encoder.py:179:default
]
Traceback (most recent call last):
File "/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/server.py", line 217, in process
self.render(resrc)
File "/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/server.py", line 284, in render
body = resrc.render(self)
File "/home/vader/src/TRIBLER/py37venv/lib/python3.7/site-packages/twisted/web/resource.py", line 265, in render
return m(request)
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py", line 284, in render_GET
peer_info['extended_version'] = _safe_extended_peer_info(peer_info['extended_version'])
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py", line 42, in _safe_extended_peer_info
json.twisted_dumps(ext_peer_info)
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/json_util.py", line 118, in twisted_dumps
return dumps(obj, ensure_ascii).encode('utf-8')
File "/home/vader/src/TRIBLER/Tribler/Tribler/Core/Utilities/json_util.py", line 100, in dumps
return json.dumps(obj, ensure_ascii=ensure_ascii)
File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

# Fixed code
def _safe_extended_peer_info(ext_peer_info):
    """
    Given a string describing peer info, return a json.twisted_dumps() safe representation.

    :param ext_peer_info: the string to convert to a dumpable format
    :return: the safe string
    """
    # First see if we can use this as-is
    if not ext_peer_info:
        ext_peer_info = ""
    try:
        return ensure_unicode(ext_peer_info, "utf8")
    except UnicodeDecodeError:
        # We might have some special unicode characters in here
        return "".join([unichr(ord(c)) for c in ext_peer_info])