blob: 37ecfb4ab757b416a70873220832905238116846 [file] [log] [blame]
Guido van Rossum30944842000-03-10 22:32:23 +00001#ifndef Py_CODECREGISTRY_H
2#define Py_CODECREGISTRY_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/* ------------------------------------------------------------------------
8
9 Python Codec Registry and support functions
10
11
12Written by Marc-Andre Lemburg (mal@lemburg.com).
13
Guido van Rossum16b1ad92000-08-03 16:24:25 +000014Copyright (c) Corporation for National Research Initiatives.
Guido van Rossum30944842000-03-10 22:32:23 +000015
16 ------------------------------------------------------------------------ */
17
Fred Drake3ac3edc2000-05-09 19:51:10 +000018/* Register a new codec search function.
19
20 As side effect, this tries to load the encodings package, if not
21 yet done, to make sure that it is always first in the list of
22 search functions.
23
24 The search_function's refcount is incremented by this function. */
25
Mark Hammond91a681d2002-08-12 07:21:58 +000026PyAPI_FUNC(int) PyCodec_Register(
Guido van Rossum30944842000-03-10 22:32:23 +000027 PyObject *search_function
28 );
29
Hai Shid332e7b2020-09-29 05:41:11 +080030/* Unregister a codec search function and clear the registry's cache.
31 If the search function is not registered, do nothing.
32 Return 0 on success. Raise an exception and return -1 on error. */
33
34PyAPI_FUNC(int) PyCodec_Unregister(
35 PyObject *search_function
36 );
37
Marc-André Lemburgb2750b52008-06-06 12:18:17 +000038/* Codec registry lookup API.
Fred Drake3ac3edc2000-05-09 19:51:10 +000039
Thomas Woutersa9773292006-04-21 09:43:23 +000040 Looks up the given encoding and returns a CodecInfo object with
41 function attributes which implement the different aspects of
42 processing the encoding.
Fred Drake3ac3edc2000-05-09 19:51:10 +000043
44 The encoding string is looked up converted to all lower-case
45 characters. This makes encodings looked up through this mechanism
46 effectively case-insensitive.
47
Thomas Woutersa9773292006-04-21 09:43:23 +000048 If no codec is found, a KeyError is set and NULL returned.
Fred Drake3ac3edc2000-05-09 19:51:10 +000049
50 As side effect, this tries to load the encodings package, if not
51 yet done. This is part of the lazy load strategy for the encodings
52 package.
53
54 */
55
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000056#ifndef Py_LIMITED_API
Mark Hammond91a681d2002-08-12 07:21:58 +000057PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
Guido van Rossum30944842000-03-10 22:32:23 +000058 const char *encoding
59 );
Nick Coghlan8fad1672014-09-15 23:50:44 +120060
61PyAPI_FUNC(int) _PyCodec_Forget(
62 const char *encoding
63 );
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000064#endif
Guido van Rossum30944842000-03-10 22:32:23 +000065
Marc-André Lemburgb2750b52008-06-06 12:18:17 +000066/* Codec registry encoding check API.
67
68 Returns 1/0 depending on whether there is a registered codec for
69 the given encoding.
70
71*/
72
73PyAPI_FUNC(int) PyCodec_KnownEncoding(
74 const char *encoding
75 );
76
Fred Drake3ac3edc2000-05-09 19:51:10 +000077/* Generic codec based encoding API.
Guido van Rossum30944842000-03-10 22:32:23 +000078
Fred Drake3ac3edc2000-05-09 19:51:10 +000079 object is passed through the encoder function found for the given
80 encoding using the error handling method defined by errors. errors
81 may be NULL to use the default method defined for the codec.
Serhiy Storchaka009b8112015-03-18 21:53:15 +020082
Fred Drake3ac3edc2000-05-09 19:51:10 +000083 Raises a LookupError in case no encoder can be found.
Guido van Rossum30944842000-03-10 22:32:23 +000084
Fred Drake3ac3edc2000-05-09 19:51:10 +000085 */
Guido van Rossum30944842000-03-10 22:32:23 +000086
Mark Hammond91a681d2002-08-12 07:21:58 +000087PyAPI_FUNC(PyObject *) PyCodec_Encode(
Guido van Rossum30944842000-03-10 22:32:23 +000088 PyObject *object,
89 const char *encoding,
90 const char *errors
91 );
92
Fred Drake3ac3edc2000-05-09 19:51:10 +000093/* Generic codec based decoding API.
94
95 object is passed through the decoder function found for the given
96 encoding using the error handling method defined by errors. errors
97 may be NULL to use the default method defined for the codec.
Serhiy Storchaka009b8112015-03-18 21:53:15 +020098
Fred Drake3ac3edc2000-05-09 19:51:10 +000099 Raises a LookupError in case no encoder can be found.
100
101 */
102
Mark Hammond91a681d2002-08-12 07:21:58 +0000103PyAPI_FUNC(PyObject *) PyCodec_Decode(
Guido van Rossum30944842000-03-10 22:32:23 +0000104 PyObject *object,
105 const char *encoding,
106 const char *errors
107 );
108
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100109#ifndef Py_LIMITED_API
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000110/* Text codec specific encoding and decoding API.
111
112 Checks the encoding against a list of codecs which do not
113 implement a str<->bytes encoding before attempting the
114 operation.
115
116 Please note that these APIs are internal and should not
117 be used in Python C extensions.
118
Nick Coghlana9b15242014-02-04 22:11:18 +1000119 XXX (ncoghlan): should we make these, or something like them, public
120 in Python 3.5+?
121
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000122 */
Nick Coghlana9b15242014-02-04 22:11:18 +1000123PyAPI_FUNC(PyObject *) _PyCodec_LookupTextEncoding(
124 const char *encoding,
125 const char *alternate_command
126 );
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000127
128PyAPI_FUNC(PyObject *) _PyCodec_EncodeText(
129 PyObject *object,
130 const char *encoding,
131 const char *errors
132 );
133
134PyAPI_FUNC(PyObject *) _PyCodec_DecodeText(
135 PyObject *object,
136 const char *encoding,
137 const char *errors
138 );
Nick Coghlana9b15242014-02-04 22:11:18 +1000139
140/* These two aren't actually text encoding specific, but _io.TextIOWrapper
141 * is the only current API consumer.
142 */
143PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalDecoder(
144 PyObject *codec_info,
145 const char *errors
146 );
147
148PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalEncoder(
149 PyObject *codec_info,
150 const char *errors
151 );
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000152#endif
153
154
155
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200156/* --- Codec Lookup APIs --------------------------------------------------
Fred Drake3ac3edc2000-05-09 19:51:10 +0000157
158 All APIs return a codec object with incremented refcount and are
159 based on _PyCodec_Lookup(). The same comments w/r to the encoding
160 name also apply to these APIs.
161
162*/
163
164/* Get an encoder function for the given encoding. */
165
Mark Hammond91a681d2002-08-12 07:21:58 +0000166PyAPI_FUNC(PyObject *) PyCodec_Encoder(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000167 const char *encoding
168 );
169
170/* Get a decoder function for the given encoding. */
171
Mark Hammond91a681d2002-08-12 07:21:58 +0000172PyAPI_FUNC(PyObject *) PyCodec_Decoder(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000173 const char *encoding
174 );
175
Martin Panter7462b6492015-11-02 03:37:02 +0000176/* Get an IncrementalEncoder object for the given encoding. */
Thomas Woutersa9773292006-04-21 09:43:23 +0000177
178PyAPI_FUNC(PyObject *) PyCodec_IncrementalEncoder(
179 const char *encoding,
180 const char *errors
181 );
182
Martin Panter7462b6492015-11-02 03:37:02 +0000183/* Get an IncrementalDecoder object function for the given encoding. */
Thomas Woutersa9773292006-04-21 09:43:23 +0000184
185PyAPI_FUNC(PyObject *) PyCodec_IncrementalDecoder(
186 const char *encoding,
187 const char *errors
188 );
189
Fred Drake3ac3edc2000-05-09 19:51:10 +0000190/* Get a StreamReader factory function for the given encoding. */
191
Mark Hammond91a681d2002-08-12 07:21:58 +0000192PyAPI_FUNC(PyObject *) PyCodec_StreamReader(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000193 const char *encoding,
194 PyObject *stream,
195 const char *errors
196 );
197
198/* Get a StreamWriter factory function for the given encoding. */
199
Mark Hammond91a681d2002-08-12 07:21:58 +0000200PyAPI_FUNC(PyObject *) PyCodec_StreamWriter(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000201 const char *encoding,
202 PyObject *stream,
203 const char *errors
204 );
205
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000206/* Unicode encoding error handling callback registry API */
207
Georg Brandlbab33782010-11-20 13:44:41 +0000208/* Register the error handling callback function error under the given
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000209 name. This function will be called by the codec when it encounters
210 unencodable characters/undecodable bytes and doesn't know the
211 callback name, when name is specified as the error parameter
212 in the call to the encode/decode function.
213 Return 0 on success, -1 on error */
214PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error);
215
Georg Brandlbab33782010-11-20 13:44:41 +0000216/* Lookup the error handling callback function registered under the given
217 name. As a special case NULL can be passed, in which case
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000218 the error handling callback for "strict" will be returned. */
219PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name);
220
221/* raise exc as an exception */
222PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc);
223
224/* ignore the unicode error, skipping the faulty input */
225PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc);
226
Georg Brandlbab33782010-11-20 13:44:41 +0000227/* replace the unicode encode error with ? or U+FFFD */
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000228PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc);
229
230/* replace the unicode encode error with XML character references */
231PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
232
233/* replace the unicode encode error with backslash escapes (\x, \u and \U) */
234PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc);
235
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200236#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Serhiy Storchaka166ebc42014-11-25 13:57:17 +0200237/* replace the unicode encode error with backslash escapes (\N, \x, \u and \U) */
238PyAPI_FUNC(PyObject *) PyCodec_NameReplaceErrors(PyObject *exc);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200239#endif
Serhiy Storchaka166ebc42014-11-25 13:57:17 +0200240
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200241#ifndef Py_LIMITED_API
Antoine Pitroue6069832011-10-15 16:38:20 +0200242PyAPI_DATA(const char *) Py_hexdigits;
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200243#endif
Victor Stinnerf5cff562011-10-14 02:13:11 +0200244
Guido van Rossum30944842000-03-10 22:32:23 +0000245#ifdef __cplusplus
246}
247#endif
248#endif /* !Py_CODECREGISTRY_H */