Codec registry and support functions¶
- intPyCodec_Register(PyObject*search_function)¶
- Part of theStable ABI.
Register a new codec search function.
As a 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.
- intPyCodec_Unregister(PyObject*search_function)¶
- Part of theStable ABI since version 3.10.
Unregister a codec search function and clear the registry’s cache.If the search function is not registered, do nothing.Return 0 on success. Raise an exception and return -1 on error.
Added in version 3.10.
- intPyCodec_KnownEncoding(constchar*encoding)¶
- Part of theStable ABI.
Return
1or0depending on whether there is a registered codec forthe givenencoding. This function always succeeds.
- PyObject*PyCodec_Encode(PyObject*object,constchar*encoding,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Generic codec based encoding API.
object is passed through the encoder function found for the givenencoding using the error handling method defined byerrors.errors maybe
NULLto use the default method defined for the codec. Raises aLookupErrorif no encoder can be found.
- PyObject*PyCodec_Decode(PyObject*object,constchar*encoding,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Generic codec based decoding API.
object is passed through the decoder function found for the givenencoding using the error handling method defined byerrors.errors maybe
NULLto use the default method defined for the codec. Raises aLookupErrorif no decoder 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(constchar*encoding)¶
- Return value: New reference. Part of theStable ABI.
Get an encoder function for the givenencoding.
- PyObject*PyCodec_Decoder(constchar*encoding)¶
- Return value: New reference. Part of theStable ABI.
Get a decoder function for the givenencoding.
- PyObject*PyCodec_IncrementalEncoder(constchar*encoding,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Get an
IncrementalEncoderobject for the givenencoding.
- PyObject*PyCodec_IncrementalDecoder(constchar*encoding,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Get an
IncrementalDecoderobject for the givenencoding.
- PyObject*PyCodec_StreamReader(constchar*encoding,PyObject*stream,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Get a
StreamReaderfactory function for the givenencoding.
- PyObject*PyCodec_StreamWriter(constchar*encoding,PyObject*stream,constchar*errors)¶
- Return value: New reference. Part of theStable ABI.
Get a
StreamWriterfactory function for the givenencoding.
Registry API for Unicode encoding error handlers¶
- intPyCodec_RegisterError(constchar*name,PyObject*error)¶
- Part of theStable ABI.
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(constchar*name)¶
- Return value: New reference. Part of theStable ABI.
Lookup the error handling callback function registered undername. As aspecial case
NULLcan be passed, in which case the error handling callbackfor “strict” will be returned.
- PyObject*PyCodec_StrictErrors(PyObject*exc)¶
- Return value: Always NULL. Part of theStable ABI.
Raiseexc as an exception.
- PyObject*PyCodec_IgnoreErrors(PyObject*exc)¶
- Return value: New reference. Part of theStable ABI.
Ignore the unicode error, skipping the faulty input.
- PyObject*PyCodec_ReplaceErrors(PyObject*exc)¶
- Return value: New reference. Part of theStable ABI.
Replace the unicode encode error with
?orU+FFFD.
- PyObject*PyCodec_XMLCharRefReplaceErrors(PyObject*exc)¶
- Return value: New reference. Part of theStable ABI.
Replace the unicode encode error with XML character references.
- PyObject*PyCodec_BackslashReplaceErrors(PyObject*exc)¶
- Return value: New reference. Part of theStable ABI.
Replace the unicode encode error with backslash escapes (
\x,\uand\U).
- PyObject*PyCodec_NameReplaceErrors(PyObject*exc)¶
- Return value: New reference. Part of theStable ABI since version 3.7.
Replace the unicode encode error with
\N{...}escapes.Added in version 3.5.
Codec utility variables¶
- constchar*Py_hexdigits¶
A string constant containing the lowercase hexadecimal digits:
"0123456789abcdef".Added in version 3.3.