blob: 235c77c945cc5becee45a00045b0d68cc61a7639 [file] [log] [blame]
Georg Brandlbab33782010-11-20 13:44:41 +00001.. _codec-registry:
2
3Codec 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
Hai Shid332e7b2020-09-29 05:41:11 +080013.. c:function:: int PyCodec_Unregister(PyObject *search_function)
14
15 Unregister a codec search function and clear the registry's cache.
16 If the search function is not registered, do nothing.
17 Return 0 on success. Raise an exception and return -1 on error.
18
19 .. versionadded:: 3.10
20
Georg Brandlbab33782010-11-20 13:44:41 +000021.. c:function:: int PyCodec_KnownEncoding(const char *encoding)
22
23 Return ``1`` or ``0`` depending on whether there is a registered codec for
Serhiy Storchaka3fcc1e02018-12-18 13:57:17 +020024 the given *encoding*. This function always succeeds.
Georg Brandlbab33782010-11-20 13:44:41 +000025
26.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
27
28 Generic codec based encoding API.
29
30 *object* is passed through the encoder function found for the given
31 *encoding* using the error handling method defined by *errors*. *errors* may
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020032 be ``NULL`` to use the default method defined for the codec. Raises a
Georg Brandlbab33782010-11-20 13:44:41 +000033 :exc:`LookupError` if no encoder can be found.
34
35.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
36
37 Generic codec based decoding API.
38
39 *object* is passed through the decoder function found for the given
40 *encoding* using the error handling method defined by *errors*. *errors* may
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020041 be ``NULL`` to use the default method defined for the codec. Raises a
Georg Brandlbab33782010-11-20 13:44:41 +000042 :exc:`LookupError` if no encoder can be found.
43
44
45Codec lookup API
46----------------
47
48In the following functions, the *encoding* string is looked up converted to all
49lower-case characters, which makes encodings looked up through this mechanism
50effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020051and ``NULL`` returned.
Georg Brandlbab33782010-11-20 13:44:41 +000052
53.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
54
55 Get an encoder function for the given *encoding*.
56
57.. c:function:: PyObject* PyCodec_Decoder(const char *encoding)
58
59 Get a decoder function for the given *encoding*.
60
61.. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
62
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030063 Get an :class:`~codecs.IncrementalEncoder` object for the given *encoding*.
Georg Brandlbab33782010-11-20 13:44:41 +000064
65.. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
66
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030067 Get an :class:`~codecs.IncrementalDecoder` object for the given *encoding*.
Georg Brandlbab33782010-11-20 13:44:41 +000068
69.. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
70
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030071 Get a :class:`~codecs.StreamReader` factory function for the given *encoding*.
Georg Brandlbab33782010-11-20 13:44:41 +000072
73.. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
74
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030075 Get a :class:`~codecs.StreamWriter` factory function for the given *encoding*.
Georg Brandlbab33782010-11-20 13:44:41 +000076
77
78Registry API for Unicode encoding error handlers
79------------------------------------------------
80
81.. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error)
82
83 Register the error handling callback function *error* under the given *name*.
84 This callback function will be called by a codec when it encounters
85 unencodable characters/undecodable bytes and *name* is specified as the error
86 parameter in the call to the encode/decode function.
87
88 The callback gets a single argument, an instance of
89 :exc:`UnicodeEncodeError`, :exc:`UnicodeDecodeError` or
90 :exc:`UnicodeTranslateError` that holds information about the problematic
Georg Brandl5a932652010-11-23 07:54:19 +000091 sequence of characters or bytes and their offset in the original string (see
92 :ref:`unicodeexceptions` for functions to extract this information). The
Georg Brandlbab33782010-11-20 13:44:41 +000093 callback must either raise the given exception, or return a two-item tuple
94 containing the replacement for the problematic sequence, and an integer
95 giving the offset in the original string at which encoding/decoding should be
96 resumed.
97
Georg Brandlbab33782010-11-20 13:44:41 +000098 Return ``0`` on success, ``-1`` on error.
99
100.. c:function:: PyObject* PyCodec_LookupError(const char *name)
101
102 Lookup the error handling callback function registered under *name*. As a
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200103 special case ``NULL`` can be passed, in which case the error handling callback
Georg Brandlbab33782010-11-20 13:44:41 +0000104 for "strict" will be returned.
105
106.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
107
108 Raise *exc* as an exception.
109
110.. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc)
111
112 Ignore the unicode error, skipping the faulty input.
113
114.. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc)
115
116 Replace the unicode encode error with ``?`` or ``U+FFFD``.
117
118.. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
119
120 Replace the unicode encode error with XML character references.
121
122.. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)
123
124 Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
125 ``\U``).
126
Serhiy Storchaka166ebc42014-11-25 13:57:17 +0200127.. c:function:: PyObject* PyCodec_NameReplaceErrors(PyObject *exc)
128
Berker Peksag87f6c222014-11-25 18:59:20 +0200129 Replace the unicode encode error with ``\N{...}`` escapes.
Serhiy Storchaka166ebc42014-11-25 13:57:17 +0200130
Berker Peksag87f6c222014-11-25 18:59:20 +0200131 .. versionadded:: 3.5