(s)
return (ok, u2)
Decode UTF-8 safely.

    Acts like str.decode('utf8', 'replace') but also fixes
    UTF16 surrogates and NUL bytes, which Python's default
    decoder does not do.
    
    @param s: utf8-encoded byte string
    @return: tuple of (was_valid_utf8, unicode_string)
Decode UTF-8 safely.
Decode UTF - 8 safely .
def safe_utf8_decode(s):
    """Decode UTF-8 safely.

    Acts like str.decode('utf8', 'replace') but also fixes
    UTF16 surrogates and NUL bytes, which Python's default
    decoder does not do.
    
    @param s: utf8-encoded byte string
    @return: tuple of (was_valid_utf8, unicode_string) 
    """

    # decode with error detection
    ok = True
    try:
        # expect no errors by default
        u = s.decode('utf8')
    except UnicodeDecodeError:
        u = s.decode('utf8', 'safe_replace')
        ok = False
    
    u2 = sanitize_unicode(u)
    if u is not u2:
        ok = False
    return (ok, u2)
def safe_utf8_decode ( s ) : # decode with error detection ok = True try : # expect no errors by default u = s . decode ( 'utf8' ) except UnicodeDecodeError : u = s . decode ( 'utf8' , 'safe_replace' ) ok = False u2 = sanitize_unicode ( u ) if u is not u2 : ok = False return ( ok , u2 )