|
| 1 | +.. _codec-registry: |
| 2 | + |
| 3 | +Codec registry and support functions |
| 4 | +==================================== |
| 5 | + |
| 6 | +.. c:function:: int PyCodec_Register(PyObject *search_function) |
| 7 | +
|
| 8 | + Register a new codec search function. |
| 9 | +
|
| 10 | + As side effect, this tries to load the :mod:`encodings` package, if not yet |
| 11 | + done, to make sure that it is always first in the list of search functions. |
| 12 | +
|
| 13 | +.. c:function:: int PyCodec_KnownEncoding(const char *encoding) |
| 14 | +
|
| 15 | + Return ``1`` or ``0`` depending on whether there is a registered codec for |
| 16 | + the given *encoding*. |
| 17 | +
|
| 18 | +.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors) |
| 19 | +
|
| 20 | + Generic codec based encoding API. |
| 21 | +
|
| 22 | + *object* is passed through the encoder function found for the given |
| 23 | + *encoding* using the error handling method defined by *errors*. *errors* may |
| 24 | + be *NULL* to use the default method defined for the codec. Raises a |
| 25 | + :exc:`LookupError` if no encoder can be found. |
| 26 | +
|
| 27 | +.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors) |
| 28 | +
|
| 29 | + Generic codec based decoding API. |
| 30 | +
|
| 31 | + *object* is passed through the decoder function found for the given |
| 32 | + *encoding* using the error handling method defined by *errors*. *errors* may |
| 33 | + be *NULL* to use the default method defined for the codec. Raises a |
| 34 | + :exc:`LookupError` if no encoder can be found. |
| 35 | +
|
| 36 | +
|
| 37 | +Codec lookup API |
| 38 | +---------------- |
| 39 | +
|
| 40 | +In the following functions, the *encoding* string is looked up converted to all |
| 41 | +lower-case characters, which makes encodings looked up through this mechanism |
| 42 | +effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set |
| 43 | +and *NULL* returned. |
| 44 | +
|
| 45 | +.. c:function:: PyObject* PyCodec_Encoder(const char *encoding) |
| 46 | +
|
| 47 | + Get an encoder function for the given *encoding*. |
| 48 | +
|
| 49 | +.. c:function:: PyObject* PyCodec_Decoder(const char *encoding) |
| 50 | +
|
| 51 | + Get a decoder function for the given *encoding*. |
| 52 | +
|
| 53 | +.. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors) |
| 54 | +
|
| 55 | + Get an :class:`IncrementalEncoder` object for the given *encoding*. |
| 56 | +
|
| 57 | +.. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors) |
| 58 | +
|
| 59 | + Get an :class:`IncrementalDecoder` object for the given *encoding*. |
| 60 | +
|
| 61 | +.. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors) |
| 62 | +
|
| 63 | + Get a :class:`StreamReader` factory function for the given *encoding*. |
| 64 | +
|
| 65 | +.. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors) |
| 66 | +
|
| 67 | + Get a :class:`StreamWriter` factory function for the given *encoding*. |
| 68 | +
|
| 69 | +
|
| 70 | +Registry API for Unicode encoding error handlers |
| 71 | +------------------------------------------------ |
| 72 | +
|
| 73 | +.. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error) |
| 74 | +
|
| 75 | + Register the error handling callback function *error* under the given *name*. |
| 76 | + This callback function will be called by a codec when it encounters |
| 77 | + unencodable characters/undecodable bytes and *name* is specified as the error |
| 78 | + parameter in the call to the encode/decode function. |
| 79 | +
|
| 80 | + The callback gets a single argument, an instance of |
| 81 | + :exc:`UnicodeEncodeError`, :exc:`UnicodeDecodeError` or |
| 82 | + :exc:`UnicodeTranslateError` that holds information about the problematic |
| 83 | + sequence of characters or bytes and their offset in the original string. The |
| 84 | + callback must either raise the given exception, or return a two-item tuple |
| 85 | + containing the replacement for the problematic sequence, and an integer |
| 86 | + giving the offset in the original string at which encoding/decoding should be |
| 87 | + resumed. |
| 88 | +
|
| 89 | + .. XXX once they are documented, link to PyUnicode*Error access functions |
| 90 | + to show how to get at the exception properties |
| 91 | +
|
| 92 | + Return ``0`` on success, ``-1`` on error. |
| 93 | +
|
| 94 | +.. c:function:: PyObject* PyCodec_LookupError(const char *name) |
| 95 | +
|
| 96 | + Lookup the error handling callback function registered under *name*. As a |
| 97 | + special case *NULL* can be passed, in which case the error handling callback |
| 98 | + for "strict" will be returned. |
| 99 | +
|
| 100 | +.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc) |
| 101 | +
|
| 102 | + Raise *exc* as an exception. |
| 103 | +
|
| 104 | +.. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc) |
| 105 | +
|
| 106 | + Ignore the unicode error, skipping the faulty input. |
| 107 | +
|
| 108 | +.. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc) |
| 109 | +
|
| 110 | + Replace the unicode encode error with ``?`` or ``U+FFFD``. |
| 111 | +
|
| 112 | +.. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
| 113 | +
|
| 114 | + Replace the unicode encode error with XML character references. |
| 115 | +
|
| 116 | +.. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc) |
| 117 | +
|
| 118 | + Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and |
| 119 | + ``\U``). |
| 120 | +
|
0 commit comments