def make_sloppy_codec(encoding):
    """
    Take a codec name, and return a 'sloppy' version of that codec that can
    encode and decode the unassigned bytes in that encoding.

    Single-byte encodings in the standard library are defined using some
    boilerplate classes surrounding the functions that do the actual work,
    `codecs.charmap_decode` and `charmap_encode`. This function, given an
    encoding name, *defines* those boilerplate classes.
    """
    # Make a bytestring of all 256 possible bytes.
    all_bytes = bytes(range(256))

    # Get a list of what they would decode to in Latin-1.
    sloppy_chars = list(all_bytes.decode('latin-1'))

    # Get a list of what they decode to in the given encoding. Use the
    # replacement character for unassigned bytes.
    if PY26:
        decoded_chars = all_bytes.decode(encoding, 'replace')
    else:
        decoded_chars = all_bytes.decode(encoding, errors='replace')

    # Update the sloppy_chars list. Each byte that was successfully decoded
    # gets its decoded value in the list. The unassigned bytes are left as
    # they are, which gives their decoding in Latin-1.
    for i, char in enumerate(decoded_chars):
        if char != REPLACEMENT_CHAR:
            sloppy_chars[i] = char

    # For ftfy's own purposes, we're going to allow byte 1A, the "Substitute"
    # control code, to encode the Unicode replacement character U+FFFD.
    sloppy_chars[0x1a] = REPLACEMENT_CHAR

    # Create the data structures that tell the charmap methods how to encode
    # and decode in this sloppy encoding.
    decoding_table = ''.join(sloppy_chars)
    encoding_table = codecs.charmap_build(decoding_table)

    # Now produce all the class boilerplate. Look at the Python source for
    # `encodings.cp1252` for comparison; this is almost exactly the same,
    # except I made it follow pep8.
    class Codec(codecs.Codec):
        def encode(self, input, errors='strict'):
            return codecs.charmap_encode(input, errors, encoding_table)

        def decode(self, input, errors='strict'):
            return codecs.charmap_decode(input, errors, decoding_table)

    class IncrementalEncoder(codecs.IncrementalEncoder):
        def encode(self, input, final=False):
            return codecs.charmap_encode(input, self.errors, encoding_table)[0]

    class IncrementalDecoder(codecs.IncrementalDecoder):
        def decode(self, input, final=False):
            return codecs.charmap_decode(input, self.errors, decoding_table)[0]

    class StreamWriter(Codec, codecs.StreamWriter):
        pass

    class StreamReader(Codec, codecs.StreamReader):
        pass

    return codecs.CodecInfo(
        name='sloppy-' + encoding,
        encode=Codec().encode,
        decode=Codec().decode,
        incrementalencoder=IncrementalEncoder,
        incrementaldecoder=IncrementalDecoder,
        streamreader=StreamReader,
        streamwriter=StreamWriter,
    )
def make_sloppy_codec ( encoding ) : all_bytes = bytes ( range ( 256 ) ) sloppy_chars = list ( all_bytes . decode ( 'latin-1' ) ) if PY26 : decoded_chars = all_bytes . decode ( encoding , 'replace' ) else : decoded_chars = all_bytes . decode ( encoding , errors = 'replace' ) for i , char in enumerate ( decoded_chars ) : if char != REPLACEMENT_CHAR : sloppy_chars [ i ] = char sloppy_chars [ 0x1a ] = REPLACEMENT_CHAR decoding_table = '' . join ( sloppy_chars ) encoding_table = codecs . charmap_build ( decoding_table ) class Codec ( codecs . Codec ) : def encode ( self , input , errors = 'strict' ) : return codecs . charmap_encode ( input , errors , encoding_table ) def decode ( self , input , errors = 'strict' ) : return codecs . charmap_decode ( input , errors , decoding_table ) class IncrementalEncoder ( codecs . IncrementalEncoder ) : def encode ( self , input , final = False ) : return codecs . charmap_encode ( input , self . errors , encoding_table ) [ 0 ] class IncrementalDecoder ( codecs . IncrementalDecoder ) : def decode ( self , input , final = False ) : return codecs . charmap_decode ( input , self . errors , decoding_table ) [ 0 ] class StreamWriter ( Codec , codecs . StreamWriter ) : pass class StreamReader ( Codec , codecs . StreamReader ) : pass return codecs . CodecInfo ( name = 'sloppy-' + encoding , encode = Codec ( ) . encode , decode = Codec ( ) . decode , incrementalencoder = IncrementalEncoder , incrementaldecoder = IncrementalDecoder , streamreader = StreamReader , streamwriter = StreamWriter , )
Take a codec name, and return a 'sloppy' version of that codec that can
    encode and decode the unassigned bytes in that encoding.

    Single-byte encodings in the standard library are defined using some
    boilerplate classes surrounding the functions that do the actual work,
    `codecs.charmap_decode` and `charmap_encode`. This function, given an
    encoding name, *defines* those boilerplate classes.