Codec registry and support functions¶
- int
PyCodec_Register(PyObject *search_function)¶ Register a new codec search function.
As side effect, this tries to load the
encodingspackage, if not yetdone, to make sure that it is always first in the list of search functions.
- int
PyCodec_KnownEncoding(const char *encoding)¶ Return
1or0depending on whether there is a registered codec forthe givenencoding. This function always succeeds.
- PyObject*
PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)¶ 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 a
LookupErrorif no encoder can be found.
- PyObject*
PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)¶ 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 a
LookupErrorif 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_IncrementalEncoder(const char *encoding, const char *errors)¶ Get an
IncrementalEncoderobject for the givenencoding.
- PyObject*
PyCodec_IncrementalDecoder(const char *encoding, const char *errors)¶ Get an
IncrementalDecoderobject for the givenencoding.
- PyObject*
PyCodec_StreamReader(const char *encoding,PyObject *stream, const char *errors)¶ Get a
StreamReaderfactory function for the givenencoding.
- PyObject*
PyCodec_StreamWriter(const char *encoding,PyObject *stream, const char *errors)¶ Get a
StreamWriterfactory function for the givenencoding.
Registry API for Unicode encoding error handlers¶
- int
PyCodec_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 of
UnicodeEncodeError,UnicodeDecodeErrororUnicodeTranslateErrorthat 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.Return
0on success,-1on error.
- PyObject*
PyCodec_LookupError(const char *name)¶ 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_XMLCharRefReplaceErrors(PyObject *exc)¶ Replace the unicode encode error with XML character references.
