Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 1 | #ifndef Py_CODECREGISTRY_H |
| 2 | #define Py_CODECREGISTRY_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
| 7 | /* ------------------------------------------------------------------------ |
| 8 | |
| 9 | Python Codec Registry and support functions |
| 10 | |
| 11 | |
| 12 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 13 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 14 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 15 | |
| 16 | ------------------------------------------------------------------------ */ |
| 17 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 18 | /* 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 Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 26 | PyAPI_FUNC(int) PyCodec_Register( |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 27 | PyObject *search_function |
| 28 | ); |
| 29 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 30 | /* Codec registry lookup API. |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 32 | 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 Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 35 | |
| 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 Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 40 | If no codec is found, a KeyError is set and NULL returned. |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 41 | |
| 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öwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 48 | #ifndef Py_LIMITED_API |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 49 | PyAPI_FUNC(PyObject *) _PyCodec_Lookup( |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 50 | const char *encoding |
| 51 | ); |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 52 | |
| 53 | PyAPI_FUNC(int) _PyCodec_Forget( |
| 54 | const char *encoding |
| 55 | ); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 56 | #endif |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 57 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 58 | /* Codec registry encoding check API. |
| 59 | |
| 60 | Returns 1/0 depending on whether there is a registered codec for |
| 61 | the given encoding. |
| 62 | |
| 63 | */ |
| 64 | |
| 65 | PyAPI_FUNC(int) PyCodec_KnownEncoding( |
| 66 | const char *encoding |
| 67 | ); |
| 68 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 69 | /* Generic codec based encoding API. |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 70 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 71 | object is passed through the encoder function found for the given |
| 72 | encoding using the error handling method defined by errors. errors |
| 73 | may be NULL to use the default method defined for the codec. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 74 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 75 | Raises a LookupError in case no encoder can be found. |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 76 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 77 | */ |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 78 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 79 | PyAPI_FUNC(PyObject *) PyCodec_Encode( |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 80 | PyObject *object, |
| 81 | const char *encoding, |
| 82 | const char *errors |
| 83 | ); |
| 84 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 85 | /* Generic codec based decoding API. |
| 86 | |
| 87 | object is passed through the decoder function found for the given |
| 88 | encoding using the error handling method defined by errors. errors |
| 89 | may be NULL to use the default method defined for the codec. |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 90 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 91 | Raises a LookupError in case no encoder can be found. |
| 92 | |
| 93 | */ |
| 94 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 95 | PyAPI_FUNC(PyObject *) PyCodec_Decode( |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 96 | PyObject *object, |
| 97 | const char *encoding, |
| 98 | const char *errors |
| 99 | ); |
| 100 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 101 | #ifndef Py_LIMITED_API |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 102 | /* Text codec specific encoding and decoding API. |
| 103 | |
| 104 | Checks the encoding against a list of codecs which do not |
| 105 | implement a str<->bytes encoding before attempting the |
| 106 | operation. |
| 107 | |
| 108 | Please note that these APIs are internal and should not |
| 109 | be used in Python C extensions. |
| 110 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 111 | XXX (ncoghlan): should we make these, or something like them, public |
| 112 | in Python 3.5+? |
| 113 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 114 | */ |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 115 | PyAPI_FUNC(PyObject *) _PyCodec_LookupTextEncoding( |
| 116 | const char *encoding, |
| 117 | const char *alternate_command |
| 118 | ); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 119 | |
| 120 | PyAPI_FUNC(PyObject *) _PyCodec_EncodeText( |
| 121 | PyObject *object, |
| 122 | const char *encoding, |
| 123 | const char *errors |
| 124 | ); |
| 125 | |
| 126 | PyAPI_FUNC(PyObject *) _PyCodec_DecodeText( |
| 127 | PyObject *object, |
| 128 | const char *encoding, |
| 129 | const char *errors |
| 130 | ); |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 131 | |
| 132 | /* These two aren't actually text encoding specific, but _io.TextIOWrapper |
| 133 | * is the only current API consumer. |
| 134 | */ |
| 135 | PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalDecoder( |
| 136 | PyObject *codec_info, |
| 137 | const char *errors |
| 138 | ); |
| 139 | |
| 140 | PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalEncoder( |
| 141 | PyObject *codec_info, |
| 142 | const char *errors |
| 143 | ); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | |
| 147 | |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 148 | /* --- Codec Lookup APIs -------------------------------------------------- |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 149 | |
| 150 | All APIs return a codec object with incremented refcount and are |
| 151 | based on _PyCodec_Lookup(). The same comments w/r to the encoding |
| 152 | name also apply to these APIs. |
| 153 | |
| 154 | */ |
| 155 | |
| 156 | /* Get an encoder function for the given encoding. */ |
| 157 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 158 | PyAPI_FUNC(PyObject *) PyCodec_Encoder( |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 159 | const char *encoding |
| 160 | ); |
| 161 | |
| 162 | /* Get a decoder function for the given encoding. */ |
| 163 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 164 | PyAPI_FUNC(PyObject *) PyCodec_Decoder( |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 165 | const char *encoding |
| 166 | ); |
| 167 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 168 | /* Get an IncrementalEncoder object for the given encoding. */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 169 | |
| 170 | PyAPI_FUNC(PyObject *) PyCodec_IncrementalEncoder( |
| 171 | const char *encoding, |
| 172 | const char *errors |
| 173 | ); |
| 174 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 175 | /* Get an IncrementalDecoder object function for the given encoding. */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 176 | |
| 177 | PyAPI_FUNC(PyObject *) PyCodec_IncrementalDecoder( |
| 178 | const char *encoding, |
| 179 | const char *errors |
| 180 | ); |
| 181 | |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 182 | /* Get a StreamReader factory function for the given encoding. */ |
| 183 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 184 | PyAPI_FUNC(PyObject *) PyCodec_StreamReader( |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 185 | const char *encoding, |
| 186 | PyObject *stream, |
| 187 | const char *errors |
| 188 | ); |
| 189 | |
| 190 | /* Get a StreamWriter factory function for the given encoding. */ |
| 191 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 192 | PyAPI_FUNC(PyObject *) PyCodec_StreamWriter( |
Fred Drake | 3ac3edc | 2000-05-09 19:51:10 +0000 | [diff] [blame] | 193 | const char *encoding, |
| 194 | PyObject *stream, |
| 195 | const char *errors |
| 196 | ); |
| 197 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 198 | /* Unicode encoding error handling callback registry API */ |
| 199 | |
Georg Brandl | bab3378 | 2010-11-20 13:44:41 +0000 | [diff] [blame] | 200 | /* Register the error handling callback function error under the given |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 201 | name. This function will be called by the codec when it encounters |
| 202 | unencodable characters/undecodable bytes and doesn't know the |
| 203 | callback name, when name is specified as the error parameter |
| 204 | in the call to the encode/decode function. |
| 205 | Return 0 on success, -1 on error */ |
| 206 | PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error); |
| 207 | |
Georg Brandl | bab3378 | 2010-11-20 13:44:41 +0000 | [diff] [blame] | 208 | /* Lookup the error handling callback function registered under the given |
| 209 | name. As a special case NULL can be passed, in which case |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 210 | the error handling callback for "strict" will be returned. */ |
| 211 | PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name); |
| 212 | |
| 213 | /* raise exc as an exception */ |
| 214 | PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc); |
| 215 | |
| 216 | /* ignore the unicode error, skipping the faulty input */ |
| 217 | PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc); |
| 218 | |
Georg Brandl | bab3378 | 2010-11-20 13:44:41 +0000 | [diff] [blame] | 219 | /* replace the unicode encode error with ? or U+FFFD */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 220 | PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc); |
| 221 | |
| 222 | /* replace the unicode encode error with XML character references */ |
| 223 | PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc); |
| 224 | |
| 225 | /* replace the unicode encode error with backslash escapes (\x, \u and \U) */ |
| 226 | PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc); |
| 227 | |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 228 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 229 | /* replace the unicode encode error with backslash escapes (\N, \x, \u and \U) */ |
| 230 | PyAPI_FUNC(PyObject *) PyCodec_NameReplaceErrors(PyObject *exc); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 231 | #endif |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 232 | |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 233 | #ifndef Py_LIMITED_API |
Antoine Pitrou | e606983 | 2011-10-15 16:38:20 +0200 | [diff] [blame] | 234 | PyAPI_DATA(const char *) Py_hexdigits; |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 235 | #endif |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 236 | |
Guido van Rossum | 3094484 | 2000-03-10 22:32:23 +0000 | [diff] [blame] | 237 | #ifdef __cplusplus |
| 238 | } |
| 239 | #endif |
| 240 | #endif /* !Py_CODECREGISTRY_H */ |