def safe_str(o):
    """
    Returns a string representation of an object, or a string containing a
    traceback, if that object's __str__ raised an exception.

    @param o: Any object.

    @rtype: C{str}
    """
    if _PY3 and isinstance(o, bytes):
        # If o is bytes and seems to holds a utf-8 encoded string,
        # convert it to str.
        try:
            return o.decode('utf-8')
        except:
            pass
    try:
        return str(o)
    except:
        return _safeFormat(str, o)

    Returns a string representation of an object, or a string containing a
    traceback, if that object's __str__ raised an exception.

    @param o: Any object.

    @rtype: C{str}
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/python/reflect.py