def string_decode(string, enc='ascii'):
    """
    safely decodes string to unicode bytestring, respecting `enc` as a hint.

    :param string: the string to decode
    :type string: str or unicode
    :param enc: a hint what encoding is used in string ('ascii', 'utf-8', ...)
    :type enc: str
    :returns: the unicode decoded input string
    :rtype: unicode

    """

    if enc is None:
        enc = 'ascii'
    try:
        string = str(string, enc, errors='replace')
    except LookupError:  # malformed enc string
        string = string.decode('ascii', errors='replace')
    except TypeError:  # already str
        pass
    return string
def string_decode ( string , enc = 'ascii' ) : if enc is None : enc = 'ascii' try : string = str ( string , enc , errors = 'replace' ) except LookupError : string = string . decode ( 'ascii' , errors = 'replace' ) except TypeError : pass return string
safely decodes string to unicode bytestring, respecting `enc` as a hint.

    :param string: the string to decode
    :type string: str or unicode
    :param enc: a hint what encoding is used in string ('ascii', 'utf-8', ...)
    :type enc: str
    :returns: the unicode decoded input string
    :rtype: unicode