blob: 83252afbb7f78c8e29ff0a64fb83f0cfbe51d097 [file] [log] [blame]
Georg Brandlb7276502010-11-26 08:28:05 +00001.. _codec-registry:
2
3Codec registry and support functions
4====================================
5
Sandro Tosi98ed08f2012-01-14 16:42:02 +01006.. c:function:: int PyCodec_Register(PyObject *search_function)
Georg Brandlb7276502010-11-26 08:28:05 +00007
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 Tosi98ed08f2012-01-14 16:42:02 +010013.. c:function:: int PyCodec_KnownEncoding(const char *encoding)
Georg Brandlb7276502010-11-26 08:28:05 +000014
15 Return ``1`` or ``0`` depending on whether there is a registered codec for
16 the given *encoding*.
17
Sandro Tosi98ed08f2012-01-14 16:42:02 +010018.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000019
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 Tosi98ed08f2012-01-14 16:42:02 +010027.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000028
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
37Codec lookup API
38----------------
39
40In the following functions, the *encoding* string is looked up converted to all
41lower-case characters, which makes encodings looked up through this mechanism
42effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
43and *NULL* returned.
44
Sandro Tosi98ed08f2012-01-14 16:42:02 +010045.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
Georg Brandlb7276502010-11-26 08:28:05 +000046
47 Get an encoder function for the given *encoding*.
48
Sandro Tosi98ed08f2012-01-14 16:42:02 +010049.. c:function:: PyObject* PyCodec_Decoder(const char *encoding)
Georg Brandlb7276502010-11-26 08:28:05 +000050
51 Get a decoder function for the given *encoding*.
52
Sandro Tosi98ed08f2012-01-14 16:42:02 +010053.. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000054
Serhiy Storchaka99a196f2013-10-09 13:25:21 +030055 Get an :class:`~codecs.IncrementalEncoder` object for the given *encoding*.
Georg Brandlb7276502010-11-26 08:28:05 +000056
Sandro Tosi98ed08f2012-01-14 16:42:02 +010057.. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000058
Serhiy Storchaka99a196f2013-10-09 13:25:21 +030059 Get an :class:`~codecs.IncrementalDecoder` object for the given *encoding*.
Georg Brandlb7276502010-11-26 08:28:05 +000060
Sandro Tosi98ed08f2012-01-14 16:42:02 +010061.. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000062
Serhiy Storchaka99a196f2013-10-09 13:25:21 +030063 Get a :class:`~codecs.StreamReader` factory function for the given *encoding*.
Georg Brandlb7276502010-11-26 08:28:05 +000064
Sandro Tosi98ed08f2012-01-14 16:42:02 +010065.. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
Georg Brandlb7276502010-11-26 08:28:05 +000066
Serhiy Storchaka99a196f2013-10-09 13:25:21 +030067 Get a :class:`~codecs.StreamWriter` factory function for the given *encoding*.
Georg Brandlb7276502010-11-26 08:28:05 +000068
69
70Registry API for Unicode encoding error handlers
71------------------------------------------------
72
Sandro Tosi98ed08f2012-01-14 16:42:02 +010073.. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error)
Georg Brandlb7276502010-11-26 08:28:05 +000074
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 Tosi98ed08f2012-01-14 16:42:02 +010092.. c:function:: PyObject* PyCodec_LookupError(const char *name)
Georg Brandlb7276502010-11-26 08:28:05 +000093
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 Tosi98ed08f2012-01-14 16:42:02 +010098.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
Georg Brandlb7276502010-11-26 08:28:05 +000099
100 Raise *exc* as an exception.
101
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100102.. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc)
Georg Brandlb7276502010-11-26 08:28:05 +0000103
104 Ignore the unicode error, skipping the faulty input.
105
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100106.. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc)
Georg Brandlb7276502010-11-26 08:28:05 +0000107
108 Replace the unicode encode error with ``?`` or ``U+FFFD``.
109
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100110.. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
Georg Brandlb7276502010-11-26 08:28:05 +0000111
112 Replace the unicode encode error with XML character references.
113
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100114.. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)
Georg Brandlb7276502010-11-26 08:28:05 +0000115
116 Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
117 ``\U``).
118