blob: 611964ce4b395c1c87c7d7180891065b01b5c1aa [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
Marc-André Lemburgb2750b52008-06-06 12:18:17 +000030/* Codec registry lookup API.
Fred Drake3ac3edc2000-05-09 19:51:10 +000031
Thomas Woutersa9773292006-04-21 09:43:23 +000032 Looks up the given encoding and returns a CodecInfo object with
33 function attributes which implement the different aspects of
34 processing the encoding.
Fred Drake3ac3edc2000-05-09 19:51:10 +000035
36 The encoding string is looked up converted to all lower-case
37 characters. This makes encodings looked up through this mechanism
38 effectively case-insensitive.
39
Thomas Woutersa9773292006-04-21 09:43:23 +000040 If no codec is found, a KeyError is set and NULL returned.
Fred Drake3ac3edc2000-05-09 19:51:10 +000041
42 As side effect, this tries to load the encodings package, if not
43 yet done. This is part of the lazy load strategy for the encodings
44 package.
45
46 */
47
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000048#ifndef Py_LIMITED_API
Mark Hammond91a681d2002-08-12 07:21:58 +000049PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
Guido van Rossum30944842000-03-10 22:32:23 +000050 const char *encoding
51 );
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000052#endif
Guido van Rossum30944842000-03-10 22:32:23 +000053
Marc-André Lemburgb2750b52008-06-06 12:18:17 +000054/* Codec registry encoding check API.
55
56 Returns 1/0 depending on whether there is a registered codec for
57 the given encoding.
58
59*/
60
61PyAPI_FUNC(int) PyCodec_KnownEncoding(
62 const char *encoding
63 );
64
Fred Drake3ac3edc2000-05-09 19:51:10 +000065/* Generic codec based encoding API.
Guido van Rossum30944842000-03-10 22:32:23 +000066
Fred Drake3ac3edc2000-05-09 19:51:10 +000067 object is passed through the encoder function found for the given
68 encoding using the error handling method defined by errors. errors
69 may be NULL to use the default method defined for the codec.
70
71 Raises a LookupError in case no encoder can be found.
Guido van Rossum30944842000-03-10 22:32:23 +000072
Fred Drake3ac3edc2000-05-09 19:51:10 +000073 */
Guido van Rossum30944842000-03-10 22:32:23 +000074
Mark Hammond91a681d2002-08-12 07:21:58 +000075PyAPI_FUNC(PyObject *) PyCodec_Encode(
Guido van Rossum30944842000-03-10 22:32:23 +000076 PyObject *object,
77 const char *encoding,
78 const char *errors
79 );
80
Fred Drake3ac3edc2000-05-09 19:51:10 +000081/* Generic codec based decoding API.
82
83 object is passed through the decoder function found for the given
84 encoding using the error handling method defined by errors. errors
85 may be NULL to use the default method defined for the codec.
86
87 Raises a LookupError in case no encoder can be found.
88
89 */
90
Mark Hammond91a681d2002-08-12 07:21:58 +000091PyAPI_FUNC(PyObject *) PyCodec_Decode(
Guido van Rossum30944842000-03-10 22:32:23 +000092 PyObject *object,
93 const char *encoding,
94 const char *errors
95 );
96
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010097#ifndef Py_LIMITED_API
Nick Coghlanc72e4e62013-11-22 22:39:36 +100098/* Text codec specific encoding and decoding API.
99
100 Checks the encoding against a list of codecs which do not
101 implement a str<->bytes encoding before attempting the
102 operation.
103
104 Please note that these APIs are internal and should not
105 be used in Python C extensions.
106
Nick Coghlana9b15242014-02-04 22:11:18 +1000107 XXX (ncoghlan): should we make these, or something like them, public
108 in Python 3.5+?
109
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000110 */
Nick Coghlana9b15242014-02-04 22:11:18 +1000111PyAPI_FUNC(PyObject *) _PyCodec_LookupTextEncoding(
112 const char *encoding,
113 const char *alternate_command
114 );
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000115
116PyAPI_FUNC(PyObject *) _PyCodec_EncodeText(
117 PyObject *object,
118 const char *encoding,
119 const char *errors
120 );
121
122PyAPI_FUNC(PyObject *) _PyCodec_DecodeText(
123 PyObject *object,
124 const char *encoding,
125 const char *errors
126 );
Nick Coghlana9b15242014-02-04 22:11:18 +1000127
128/* These two aren't actually text encoding specific, but _io.TextIOWrapper
129 * is the only current API consumer.
130 */
131PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalDecoder(
132 PyObject *codec_info,
133 const char *errors
134 );
135
136PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalEncoder(
137 PyObject *codec_info,
138 const char *errors
139 );
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000140#endif
141
142
143
Fred Drake3ac3edc2000-05-09 19:51:10 +0000144/* --- Codec Lookup APIs --------------------------------------------------
145
146 All APIs return a codec object with incremented refcount and are
147 based on _PyCodec_Lookup(). The same comments w/r to the encoding
148 name also apply to these APIs.
149
150*/
151
152/* Get an encoder function for the given encoding. */
153
Mark Hammond91a681d2002-08-12 07:21:58 +0000154PyAPI_FUNC(PyObject *) PyCodec_Encoder(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000155 const char *encoding
156 );
157
158/* Get a decoder function for the given encoding. */
159
Mark Hammond91a681d2002-08-12 07:21:58 +0000160PyAPI_FUNC(PyObject *) PyCodec_Decoder(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000161 const char *encoding
162 );
163
Thomas Woutersa9773292006-04-21 09:43:23 +0000164/* Get a IncrementalEncoder object for the given encoding. */
165
166PyAPI_FUNC(PyObject *) PyCodec_IncrementalEncoder(
167 const char *encoding,
168 const char *errors
169 );
170
171/* Get a IncrementalDecoder object function for the given encoding. */
172
173PyAPI_FUNC(PyObject *) PyCodec_IncrementalDecoder(
174 const char *encoding,
175 const char *errors
176 );
177
Fred Drake3ac3edc2000-05-09 19:51:10 +0000178/* Get a StreamReader factory function for the given encoding. */
179
Mark Hammond91a681d2002-08-12 07:21:58 +0000180PyAPI_FUNC(PyObject *) PyCodec_StreamReader(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000181 const char *encoding,
182 PyObject *stream,
183 const char *errors
184 );
185
186/* Get a StreamWriter factory function for the given encoding. */
187
Mark Hammond91a681d2002-08-12 07:21:58 +0000188PyAPI_FUNC(PyObject *) PyCodec_StreamWriter(
Fred Drake3ac3edc2000-05-09 19:51:10 +0000189 const char *encoding,
190 PyObject *stream,
191 const char *errors
192 );
193
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000194/* Unicode encoding error handling callback registry API */
195
Georg Brandlbab33782010-11-20 13:44:41 +0000196/* Register the error handling callback function error under the given
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000197 name. This function will be called by the codec when it encounters
198 unencodable characters/undecodable bytes and doesn't know the
199 callback name, when name is specified as the error parameter
200 in the call to the encode/decode function.
201 Return 0 on success, -1 on error */
202PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error);
203
Georg Brandlbab33782010-11-20 13:44:41 +0000204/* Lookup the error handling callback function registered under the given
205 name. As a special case NULL can be passed, in which case
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000206 the error handling callback for "strict" will be returned. */
207PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name);
208
209/* raise exc as an exception */
210PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc);
211
212/* ignore the unicode error, skipping the faulty input */
213PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc);
214
Georg Brandlbab33782010-11-20 13:44:41 +0000215/* replace the unicode encode error with ? or U+FFFD */
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000216PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc);
217
218/* replace the unicode encode error with XML character references */
219PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
220
221/* replace the unicode encode error with backslash escapes (\x, \u and \U) */
222PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc);
223
Antoine Pitroue6069832011-10-15 16:38:20 +0200224PyAPI_DATA(const char *) Py_hexdigits;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200225
Guido van Rossum30944842000-03-10 22:32:23 +0000226#ifdef __cplusplus
227}
228#endif
229#endif /* !Py_CODECREGISTRY_H */