Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 1 | .. _codec-registry: |
| 2 | |
| 3 | Codec registry and support functions |
| 4 | ==================================== |
| 5 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 6 | .. c:function:: int PyCodec_Register(PyObject *search_function) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 13 | .. c:function:: int PyCodec_KnownEncoding(const char *encoding) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 14 | |
| 15 | Return ``1`` or ``0`` depending on whether there is a registered codec for |
| 16 | the given *encoding*. |
| 17 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 18 | .. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 27 | .. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 45 | .. c:function:: PyObject* PyCodec_Encoder(const char *encoding) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 46 | |
| 47 | Get an encoder function for the given *encoding*. |
| 48 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 49 | .. c:function:: PyObject* PyCodec_Decoder(const char *encoding) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 50 | |
| 51 | Get a decoder function for the given *encoding*. |
| 52 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 53 | .. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 54 | |
| 55 | Get an :class:`IncrementalEncoder` object for the given *encoding*. |
| 56 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 57 | .. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 58 | |
| 59 | Get an :class:`IncrementalDecoder` object for the given *encoding*. |
| 60 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 61 | .. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 62 | |
| 63 | Get a :class:`StreamReader` factory function for the given *encoding*. |
| 64 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 65 | .. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 66 | |
| 67 | Get a :class:`StreamWriter` factory function for the given *encoding*. |
| 68 | |
| 69 | |
| 70 | Registry API for Unicode encoding error handlers |
| 71 | ------------------------------------------------ |
| 72 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 73 | .. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 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 (see |
| 84 | :ref:`unicodeexceptions` for functions to extract this information). The |
| 85 | callback must either raise the given exception, or return a two-item tuple |
| 86 | containing the replacement for the problematic sequence, and an integer |
| 87 | giving the offset in the original string at which encoding/decoding should be |
| 88 | resumed. |
| 89 | |
| 90 | Return ``0`` on success, ``-1`` on error. |
| 91 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 92 | .. c:function:: PyObject* PyCodec_LookupError(const char *name) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 93 | |
| 94 | Lookup the error handling callback function registered under *name*. As a |
| 95 | special case *NULL* can be passed, in which case the error handling callback |
| 96 | for "strict" will be returned. |
| 97 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 98 | .. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 99 | |
| 100 | Raise *exc* as an exception. |
| 101 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 102 | .. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 103 | |
| 104 | Ignore the unicode error, skipping the faulty input. |
| 105 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 106 | .. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 107 | |
| 108 | Replace the unicode encode error with ``?`` or ``U+FFFD``. |
| 109 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 110 | .. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 111 | |
| 112 | Replace the unicode encode error with XML character references. |
| 113 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 114 | .. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc) |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 115 | |
| 116 | Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and |
| 117 | ``\U``). |
| 118 | |