Codec registry and support functions

intPyCodec_Register(PyObject *search_function)

Register a new codec search function.

As side effect, this tries to load theencodings package, if not yetdone, to make sure that it is always first in the list of search functions.

intPyCodec_KnownEncoding(const char *encoding)

Return1 or0 depending on whether there is a registered codec forthe givenencoding. This function always succeeds.

PyObject*PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
Return value: New reference.

Generic codec based encoding API.

object is passed through the encoder function found for the givenencoding using the error handling method defined byerrors.errors maybeNULL to use the default method defined for the codec. Raises aLookupError if no encoder can be found.

PyObject*PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
Return value: New reference.

Generic codec based decoding API.

object is passed through the decoder function found for the givenencoding using the error handling method defined byerrors.errors maybeNULL to use the default method defined for the codec. Raises aLookupError if no encoder can be found.

Codec lookup API

In the following functions, theencoding string is looked up converted to alllower-case characters, which makes encodings looked up through this mechanismeffectively case-insensitive. If no codec is found, aKeyError is setandNULL returned.

PyObject*PyCodec_Encoder(const char *encoding)
Return value: New reference.

Get an encoder function for the givenencoding.

PyObject*PyCodec_Decoder(const char *encoding)
Return value: New reference.

Get a decoder function for the givenencoding.

PyObject*PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
Return value: New reference.

Get anIncrementalEncoder object for the givenencoding.

PyObject*PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
Return value: New reference.

Get anIncrementalDecoder object for the givenencoding.

PyObject*PyCodec_StreamReader(const char *encoding,PyObject *stream, const char *errors)
Return value: New reference.

Get aStreamReader factory function for the givenencoding.

PyObject*PyCodec_StreamWriter(const char *encoding,PyObject *stream, const char *errors)
Return value: New reference.

Get aStreamWriter factory function for the givenencoding.

Registry API for Unicode encoding error handlers

intPyCodec_RegisterError(const char *name,PyObject *error)

Register the error handling callback functionerror under the givenname.This callback function will be called by a codec when it encountersunencodable characters/undecodable bytes andname is specified as the errorparameter in the call to the encode/decode function.

The callback gets a single argument, an instance ofUnicodeEncodeError,UnicodeDecodeError orUnicodeTranslateError that holds information about the problematicsequence of characters or bytes and their offset in the original string (seeUnicode Exception Objects for functions to extract this information). Thecallback must either raise the given exception, or return a two-item tuplecontaining the replacement for the problematic sequence, and an integergiving the offset in the original string at which encoding/decoding should beresumed.

Return0 on success,-1 on error.

PyObject*PyCodec_LookupError(const char *name)
Return value: New reference.

Lookup the error handling callback function registered undername. As aspecial caseNULL can be passed, in which case the error handling callbackfor “strict” will be returned.

PyObject*PyCodec_StrictErrors(PyObject *exc)
Return value: Always NULL.

Raiseexc as an exception.

PyObject*PyCodec_IgnoreErrors(PyObject *exc)
Return value: New reference.

Ignore the unicode error, skipping the faulty input.

PyObject*PyCodec_ReplaceErrors(PyObject *exc)
Return value: New reference.

Replace the unicode encode error with? orU+FFFD.

PyObject*PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
Return value: New reference.

Replace the unicode encode error with XML character references.

PyObject*PyCodec_BackslashReplaceErrors(PyObject *exc)
Return value: New reference.

Replace the unicode encode error with backslash escapes (\x,\u and\U).

PyObject*PyCodec_NameReplaceErrors(PyObject *exc)
Return value: New reference.

Replace the unicode encode error with\N{...} escapes.

New in version 3.5.