This module defines base classes for standard Python codecs (encodersand decoders) and provides access to the internal Python codecregistry which manages the codec lookup process.
It defines the following functions:
(encoder,decoder,stream_reader,stream_writer) taking the following arguments:encoder anddecoder: These must be functions or methods which have the same interface as theencode()/decode() methods of Codec instances (see Codec Interface). The functions/methods are expected to work in a stateless mode.
stream_reader andstream_writer: These have to be factory functions providing the following interface:
factory(stream,errors='strict')
The factory functions must return objects providing the interfaces defined by the base classesStreamWriter andStreamReader, respectively. Stream codecs can maintain state.
Possible values for errors are'strict' (raise an exception in case of an encoding error),'replace' (replace malformed data with a suitable replacement marker, such as "?") and'ignore' (ignore malformed data and continue without further notice).
In case a search function cannot find a given encoding, it shouldreturnNone.
Encodings are first looked up in the registry's cache. If not found,the list of registered search functions is scanned. If no codecs tupleis found, aLookupError is raised. Otherwise, the codecstuple is stored in the cache and returned to the caller.
To simply access to the various codecs, the module provides theseadditional functions which uselookup() for the codeclookup:
Raises aLookupError in case the encoding cannot be found.
Raises aLookupError in case the encoding cannot be found.
Raises aLookupError in case the encoding cannot be found.
Raises aLookupError in case the encoding cannot be found.
To simplify working with encoded files or stream, the modulealso defines these utility functions:
Note:The wrapped version will only accept the object formatdefined by the codecs, i.e. Unicode objects for most built-incodecs. Output is also codec-dependent and will usually be Unicode aswell.
encoding specifies the encoding which is to be used for thethe file.
errors may be given to define the error handling. It defaultsto'strict' which causes aValueError to be raisedin case an encoding error occurs.
buffering has the same meaning as for the built-inopen() function. It defaults to line buffered.
Strings written to the wrapped file are interpreted according to thegiveninput encoding and then written to the original file asstrings using theoutput encoding. The intermediate encoding willusually be Unicode but depends on the specified codecs.
Ifoutput is not given, it defaults toinput.
errors may be given to define the error handling. It defaults to'strict', which causesValueError to be raised in casean encoding error occurs.
The module also provides the following constants which are usefulfor reading and writing to platform dependent files:
See Also:
| Python Library Reference |