Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
| 3 | _codecs -- Provides access to the codec registry and the builtin |
| 4 | codecs. |
| 5 | |
| 6 | This module should never be imported directly. The standard library |
| 7 | module "codecs" wraps this builtin module for use within Python. |
| 8 | |
| 9 | The codec registry is accessible via: |
| 10 | |
| 11 | register(search_function) -> None |
| 12 | |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 13 | lookup(encoding) -> CodecInfo object |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 14 | |
| 15 | The builtin Unicode codecs use the following interface: |
| 16 | |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 17 | <encoding>_encode(Unicode_object[,errors='strict']) -> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | (string object, bytes consumed) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 19 | |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 20 | <encoding>_decode(char_buffer_obj[,errors='strict']) -> |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 21 | (Unicode object, bytes consumed) |
| 22 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 23 | <encoding>_encode() interfaces also accept non-Unicode object as |
| 24 | input. The objects are then converted to Unicode using |
| 25 | PyUnicode_FromObject() prior to applying the conversion. |
| 26 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 27 | These <encoding>s are available: utf_8, unicode_escape, |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 28 | raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit), |
| 29 | mbcs (on win32). |
| 30 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 31 | |
| 32 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 33 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 34 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 35 | |
| 36 | ------------------------------------------------------------------------ */ |
| 37 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 38 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 39 | #include "Python.h" |
| 40 | |
Victor Stinner | 87a7c82 | 2011-11-10 20:05:55 +0100 | [diff] [blame] | 41 | #ifdef MS_WINDOWS |
| 42 | #include <windows.h> |
| 43 | #endif |
| 44 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 45 | /*[clinic input] |
| 46 | module _codecs |
| 47 | [clinic start generated code]*/ |
| 48 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/ |
| 49 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 50 | #include "clinic/_codecsmodule.c.h" |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 51 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 52 | /* --- Registry ----------------------------------------------------------- */ |
| 53 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 54 | /*[clinic input] |
| 55 | _codecs.register |
| 56 | search_function: object |
| 57 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 58 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 59 | Register a codec search function. |
| 60 | |
| 61 | Search functions are expected to take one argument, the encoding name in |
| 62 | all lower case letters, and either return None, or a tuple of functions |
| 63 | (encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object). |
| 64 | [clinic start generated code]*/ |
| 65 | |
| 66 | static PyObject * |
| 67 | _codecs_register(PyModuleDef *module, PyObject *search_function) |
| 68 | /*[clinic end generated code: output=d17608b6ad380eb8 input=369578467955cae4]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 70 | if (PyCodec_Register(search_function)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 71 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 72 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 73 | Py_RETURN_NONE; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 76 | /*[clinic input] |
| 77 | _codecs.lookup |
| 78 | encoding: str |
| 79 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 80 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 81 | Looks up a codec tuple in the Python codec registry and returns a CodecInfo object. |
| 82 | [clinic start generated code]*/ |
| 83 | |
| 84 | static PyObject * |
| 85 | _codecs_lookup_impl(PyModuleDef *module, const char *encoding) |
| 86 | /*[clinic end generated code: output=798e41aff0c04ef6 input=3c572c0db3febe9c]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 88 | return _PyCodec_Lookup(encoding); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 91 | /*[clinic input] |
| 92 | _codecs.encode |
| 93 | obj: object |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 94 | encoding: str(c_default="NULL") = "utf-8" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 95 | errors: str(c_default="NULL") = "strict" |
| 96 | |
| 97 | Encodes obj using the codec registered for encoding. |
| 98 | |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 99 | The default encoding is 'utf-8'. errors may be given to set a |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 100 | different error handling scheme. Default is 'strict' meaning that encoding |
| 101 | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
| 102 | and 'backslashreplace' as well as any other name registered with |
| 103 | codecs.register_error that can handle ValueErrors. |
| 104 | [clinic start generated code]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 105 | |
| 106 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 107 | _codecs_encode_impl(PyModuleDef *module, PyObject *obj, const char *encoding, |
| 108 | const char *errors) |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 109 | /*[clinic end generated code: output=5c073f62249c8d7c input=cd5b685040ff61f0]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 110 | { |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 111 | if (encoding == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 113 | |
| 114 | /* Encode via the codec registry */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 115 | return PyCodec_Encode(obj, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 118 | /*[clinic input] |
| 119 | _codecs.decode |
| 120 | obj: object |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 121 | encoding: str(c_default="NULL") = "utf-8" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 122 | errors: str(c_default="NULL") = "strict" |
| 123 | |
| 124 | Decodes obj using the codec registered for encoding. |
| 125 | |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 126 | Default encoding is 'utf-8'. errors may be given to set a |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 127 | different error handling scheme. Default is 'strict' meaning that encoding |
| 128 | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
| 129 | and 'backslashreplace' as well as any other name registered with |
| 130 | codecs.register_error that can handle ValueErrors. |
| 131 | [clinic start generated code]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 132 | |
| 133 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 134 | _codecs_decode_impl(PyModuleDef *module, PyObject *obj, const char *encoding, |
| 135 | const char *errors) |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 136 | /*[clinic end generated code: output=c81cbf6189a7f878 input=7702c0cc2fa1add6]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 137 | { |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 138 | if (encoding == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 140 | |
| 141 | /* Decode via the codec registry */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 142 | return PyCodec_Decode(obj, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 145 | /* --- Helpers ------------------------------------------------------------ */ |
| 146 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 147 | /*[clinic input] |
| 148 | _codecs._forget_codec |
| 149 | |
| 150 | encoding: str |
| 151 | / |
| 152 | |
| 153 | Purge the named codec from the internal codec lookup cache |
| 154 | [clinic start generated code]*/ |
| 155 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 156 | static PyObject * |
| 157 | _codecs__forget_codec_impl(PyModuleDef *module, const char *encoding) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 158 | /*[clinic end generated code: output=b56a9b99d2d28080 input=18d5d92d0e386c38]*/ |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 159 | { |
| 160 | if (_PyCodec_Forget(encoding) < 0) { |
| 161 | return NULL; |
| 162 | }; |
| 163 | Py_RETURN_NONE; |
| 164 | } |
| 165 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 166 | static |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 167 | PyObject *codec_tuple(PyObject *decoded, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | Py_ssize_t len) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 169 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 170 | if (decoded == NULL) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 171 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 172 | return Py_BuildValue("Nn", decoded, len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 175 | /* --- String codecs ------------------------------------------------------ */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 176 | /*[clinic input] |
| 177 | _codecs.escape_decode |
| 178 | data: Py_buffer(accept={str, buffer}) |
| 179 | errors: str(accept={str, NoneType}) = NULL |
| 180 | / |
| 181 | [clinic start generated code]*/ |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 182 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 183 | static PyObject * |
| 184 | _codecs_escape_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 185 | const char *errors) |
| 186 | /*[clinic end generated code: output=648fa3e78d03e658 input=0018edfd99db714d]*/ |
| 187 | { |
| 188 | PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len, |
| 189 | errors, 0, NULL); |
| 190 | return codec_tuple(decoded, data->len); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 193 | /*[clinic input] |
| 194 | _codecs.escape_encode |
| 195 | data: object(subclass_of='&PyBytes_Type') |
| 196 | errors: str(accept={str, NoneType}) = NULL |
| 197 | / |
| 198 | [clinic start generated code]*/ |
| 199 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 200 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 201 | _codecs_escape_encode_impl(PyModuleDef *module, PyObject *data, |
| 202 | const char *errors) |
| 203 | /*[clinic end generated code: output=fcd6f34fe4111c50 input=da9ded00992f32f2]*/ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 204 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 205 | Py_ssize_t size; |
| 206 | Py_ssize_t newsize; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 207 | PyObject *v; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 208 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 209 | size = PyBytes_GET_SIZE(data); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 210 | if (size > PY_SSIZE_T_MAX / 4) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 211 | PyErr_SetString(PyExc_OverflowError, |
| 212 | "string is too large to encode"); |
| 213 | return NULL; |
| 214 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 215 | newsize = 4*size; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 216 | v = PyBytes_FromStringAndSize(NULL, newsize); |
| 217 | |
| 218 | if (v == NULL) { |
| 219 | return NULL; |
| 220 | } |
| 221 | else { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 222 | Py_ssize_t i; |
| 223 | char c; |
| 224 | char *p = PyBytes_AS_STRING(v); |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 225 | |
| 226 | for (i = 0; i < size; i++) { |
| 227 | /* There's at least enough room for a hex escape */ |
| 228 | assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 229 | c = PyBytes_AS_STRING(data)[i]; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 230 | if (c == '\'' || c == '\\') |
| 231 | *p++ = '\\', *p++ = c; |
| 232 | else if (c == '\t') |
| 233 | *p++ = '\\', *p++ = 't'; |
| 234 | else if (c == '\n') |
| 235 | *p++ = '\\', *p++ = 'n'; |
| 236 | else if (c == '\r') |
| 237 | *p++ = '\\', *p++ = 'r'; |
| 238 | else if (c < ' ' || c >= 0x7f) { |
| 239 | *p++ = '\\'; |
| 240 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 241 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 242 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 243 | } |
| 244 | else |
| 245 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | } |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 247 | *p = '\0'; |
| 248 | if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) { |
| 249 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | } |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 251 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 253 | return codec_tuple(v, size); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 256 | /* --- Decoder ------------------------------------------------------------ */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 257 | /*[clinic input] |
| 258 | _codecs.unicode_internal_decode |
| 259 | obj: object |
| 260 | errors: str(accept={str, NoneType}) = NULL |
| 261 | / |
| 262 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 263 | |
| 264 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 265 | _codecs_unicode_internal_decode_impl(PyModuleDef *module, PyObject *obj, |
| 266 | const char *errors) |
| 267 | /*[clinic end generated code: output=9fe47c2cd8807d92 input=8d57930aeda170c6]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 268 | { |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 269 | if (PyUnicode_Check(obj)) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 270 | if (PyUnicode_READY(obj) < 0) |
| 271 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | Py_INCREF(obj); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 273 | return codec_tuple(obj, PyUnicode_GET_LENGTH(obj)); |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 274 | } |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 275 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 276 | Py_buffer view; |
| 277 | PyObject *result; |
| 278 | if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | return NULL; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 280 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 281 | result = codec_tuple( |
| 282 | _PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors), |
| 283 | view.len); |
| 284 | PyBuffer_Release(&view); |
| 285 | return result; |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 286 | } |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 289 | /*[clinic input] |
| 290 | _codecs.utf_7_decode |
| 291 | data: Py_buffer |
| 292 | errors: str(accept={str, NoneType}) = NULL |
| 293 | final: int(c_default="0") = False |
| 294 | / |
| 295 | [clinic start generated code]*/ |
| 296 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 297 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 298 | _codecs_utf_7_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 299 | const char *errors, int final) |
| 300 | /*[clinic end generated code: output=ca945e907e72e827 input=bc4d6247ecdb01e6]*/ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 301 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 302 | Py_ssize_t consumed = data->len; |
| 303 | PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len, |
| 304 | errors, |
| 305 | final ? NULL : &consumed); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 306 | return codec_tuple(decoded, consumed); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 309 | /*[clinic input] |
| 310 | _codecs.utf_8_decode |
| 311 | data: Py_buffer |
| 312 | errors: str(accept={str, NoneType}) = NULL |
| 313 | final: int(c_default="0") = False |
| 314 | / |
| 315 | [clinic start generated code]*/ |
| 316 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 317 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 318 | _codecs_utf_8_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 319 | const char *errors, int final) |
| 320 | /*[clinic end generated code: output=7309f9ff4ef5c9b6 input=39161d71e7422ee2]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 321 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 322 | Py_ssize_t consumed = data->len; |
| 323 | PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len, |
| 324 | errors, |
| 325 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 326 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 329 | /*[clinic input] |
| 330 | _codecs.utf_16_decode |
| 331 | data: Py_buffer |
| 332 | errors: str(accept={str, NoneType}) = NULL |
| 333 | final: int(c_default="0") = False |
| 334 | / |
| 335 | [clinic start generated code]*/ |
| 336 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 337 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 338 | _codecs_utf_16_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 339 | const char *errors, int final) |
| 340 | /*[clinic end generated code: output=8d2fa0507d9bef2c input=f3cf01d1461007ce]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 341 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 342 | int byteorder = 0; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 343 | /* This is overwritten unless final is true. */ |
| 344 | Py_ssize_t consumed = data->len; |
| 345 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 346 | errors, &byteorder, |
| 347 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 348 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 351 | /*[clinic input] |
| 352 | _codecs.utf_16_le_decode |
| 353 | data: Py_buffer |
| 354 | errors: str(accept={str, NoneType}) = NULL |
| 355 | final: int(c_default="0") = False |
| 356 | / |
| 357 | [clinic start generated code]*/ |
| 358 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 359 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 360 | _codecs_utf_16_le_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 361 | const char *errors, int final) |
| 362 | /*[clinic end generated code: output=4fd621515ef4ce18 input=a77e3bf97335d94e]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 363 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 364 | int byteorder = -1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 365 | /* This is overwritten unless final is true. */ |
| 366 | Py_ssize_t consumed = data->len; |
| 367 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 368 | errors, &byteorder, |
| 369 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 370 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 373 | /*[clinic input] |
| 374 | _codecs.utf_16_be_decode |
| 375 | data: Py_buffer |
| 376 | errors: str(accept={str, NoneType}) = NULL |
| 377 | final: int(c_default="0") = False |
| 378 | / |
| 379 | [clinic start generated code]*/ |
| 380 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 381 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 382 | _codecs_utf_16_be_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 383 | const char *errors, int final) |
| 384 | /*[clinic end generated code: output=792f4eacb3e1fa05 input=606f69fae91b5563]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 385 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 386 | int byteorder = 1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 387 | /* This is overwritten unless final is true. */ |
| 388 | Py_ssize_t consumed = data->len; |
| 389 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 390 | errors, &byteorder, |
| 391 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 392 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* This non-standard version also provides access to the byteorder |
| 396 | parameter of the builtin UTF-16 codec. |
| 397 | |
| 398 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 399 | being the value in effect at the end of data. |
| 400 | |
| 401 | */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 402 | /*[clinic input] |
| 403 | _codecs.utf_16_ex_decode |
| 404 | data: Py_buffer |
| 405 | errors: str(accept={str, NoneType}) = NULL |
| 406 | byteorder: int = 0 |
| 407 | final: int(c_default="0") = False |
| 408 | / |
| 409 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 410 | |
| 411 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 412 | _codecs_utf_16_ex_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 413 | const char *errors, int byteorder, int final) |
| 414 | /*[clinic end generated code: output=f136a186dc2defa0 input=f6e7f697658c013e]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 415 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 416 | /* This is overwritten unless final is true. */ |
| 417 | Py_ssize_t consumed = data->len; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 418 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 419 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 420 | errors, &byteorder, |
| 421 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 422 | if (decoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 424 | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
| 425 | } |
| 426 | |
| 427 | /*[clinic input] |
| 428 | _codecs.utf_32_decode |
| 429 | data: Py_buffer |
| 430 | errors: str(accept={str, NoneType}) = NULL |
| 431 | final: int(c_default="0") = False |
| 432 | / |
| 433 | [clinic start generated code]*/ |
| 434 | |
| 435 | static PyObject * |
| 436 | _codecs_utf_32_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 437 | const char *errors, int final) |
| 438 | /*[clinic end generated code: output=b7635e55857e8efb input=86d4f41c6c2e763d]*/ |
| 439 | { |
| 440 | int byteorder = 0; |
| 441 | /* This is overwritten unless final is true. */ |
| 442 | Py_ssize_t consumed = data->len; |
| 443 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 444 | errors, &byteorder, |
| 445 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 446 | return codec_tuple(decoded, consumed); |
| 447 | } |
| 448 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 449 | /*[clinic input] |
| 450 | _codecs.utf_32_le_decode |
| 451 | data: Py_buffer |
| 452 | errors: str(accept={str, NoneType}) = NULL |
| 453 | final: int(c_default="0") = False |
| 454 | / |
| 455 | [clinic start generated code]*/ |
| 456 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 457 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 458 | _codecs_utf_32_le_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 459 | const char *errors, int final) |
| 460 | /*[clinic end generated code: output=a79d1787d8ddf988 input=d18b650772d188ba]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 461 | { |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 462 | int byteorder = -1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 463 | /* This is overwritten unless final is true. */ |
| 464 | Py_ssize_t consumed = data->len; |
| 465 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 466 | errors, &byteorder, |
| 467 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 468 | return codec_tuple(decoded, consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 471 | /*[clinic input] |
| 472 | _codecs.utf_32_be_decode |
| 473 | data: Py_buffer |
| 474 | errors: str(accept={str, NoneType}) = NULL |
| 475 | final: int(c_default="0") = False |
| 476 | / |
| 477 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 478 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 479 | static PyObject * |
| 480 | _codecs_utf_32_be_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 481 | const char *errors, int final) |
| 482 | /*[clinic end generated code: output=a8356b0f36779981 input=19c271b5d34926d8]*/ |
| 483 | { |
| 484 | int byteorder = 1; |
| 485 | /* This is overwritten unless final is true. */ |
| 486 | Py_ssize_t consumed = data->len; |
| 487 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 488 | errors, &byteorder, |
| 489 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 490 | return codec_tuple(decoded, consumed); |
| 491 | } |
| 492 | |
| 493 | /* This non-standard version also provides access to the byteorder |
| 494 | parameter of the builtin UTF-32 codec. |
| 495 | |
| 496 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 497 | being the value in effect at the end of data. |
| 498 | |
| 499 | */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 500 | /*[clinic input] |
| 501 | _codecs.utf_32_ex_decode |
| 502 | data: Py_buffer |
| 503 | errors: str(accept={str, NoneType}) = NULL |
| 504 | byteorder: int = 0 |
| 505 | final: int(c_default="0") = False |
| 506 | / |
| 507 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 508 | |
| 509 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 510 | _codecs_utf_32_ex_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 511 | const char *errors, int byteorder, int final) |
| 512 | /*[clinic end generated code: output=ab8c70977c1992f5 input=4af3e6ccfe34a076]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 513 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 514 | Py_ssize_t consumed = data->len; |
| 515 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 516 | errors, &byteorder, |
| 517 | final ? NULL : &consumed); |
| 518 | if (decoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 520 | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 523 | /*[clinic input] |
| 524 | _codecs.unicode_escape_decode |
| 525 | data: Py_buffer(accept={str, buffer}) |
| 526 | errors: str(accept={str, NoneType}) = NULL |
| 527 | / |
| 528 | [clinic start generated code]*/ |
| 529 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 530 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 531 | _codecs_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 532 | const char *errors) |
| 533 | /*[clinic end generated code: output=d1aa63f2620c4999 input=49fd27d06813a7f5]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 534 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 535 | PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len, |
| 536 | errors); |
| 537 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 540 | /*[clinic input] |
| 541 | _codecs.raw_unicode_escape_decode |
| 542 | data: Py_buffer(accept={str, buffer}) |
| 543 | errors: str(accept={str, NoneType}) = NULL |
| 544 | / |
| 545 | [clinic start generated code]*/ |
| 546 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 547 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 548 | _codecs_raw_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 549 | const char *errors) |
| 550 | /*[clinic end generated code: output=0bf96cc182d81379 input=770903a211434ebc]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 551 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 552 | PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len, |
| 553 | errors); |
| 554 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 557 | /*[clinic input] |
| 558 | _codecs.latin_1_decode |
| 559 | data: Py_buffer |
| 560 | errors: str(accept={str, NoneType}) = NULL |
| 561 | / |
| 562 | [clinic start generated code]*/ |
| 563 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 564 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 565 | _codecs_latin_1_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 566 | const char *errors) |
| 567 | /*[clinic end generated code: output=66b916f5055aaf13 input=5cad0f1759c618ec]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 568 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 569 | PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors); |
| 570 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 573 | /*[clinic input] |
| 574 | _codecs.ascii_decode |
| 575 | data: Py_buffer |
| 576 | errors: str(accept={str, NoneType}) = NULL |
| 577 | / |
| 578 | [clinic start generated code]*/ |
| 579 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 580 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 581 | _codecs_ascii_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 582 | const char *errors) |
| 583 | /*[clinic end generated code: output=7f213a1b5cdafc65 input=ad1106f64037bd16]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 584 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 585 | PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors); |
| 586 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 589 | /*[clinic input] |
| 590 | _codecs.charmap_decode |
| 591 | data: Py_buffer |
| 592 | errors: str(accept={str, NoneType}) = NULL |
| 593 | mapping: object = NULL |
| 594 | / |
| 595 | [clinic start generated code]*/ |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 596 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 597 | static PyObject * |
| 598 | _codecs_charmap_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 599 | const char *errors, PyObject *mapping) |
| 600 | /*[clinic end generated code: output=87d27f365098bbae input=19712ca35c5a80e2]*/ |
| 601 | { |
| 602 | PyObject *decoded; |
| 603 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 604 | if (mapping == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | mapping = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 606 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 607 | decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors); |
| 608 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 611 | #ifdef HAVE_MBCS |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 612 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 613 | /*[clinic input] |
| 614 | _codecs.mbcs_decode |
| 615 | data: Py_buffer |
| 616 | errors: str(accept={str, NoneType}) = NULL |
| 617 | final: int(c_default="0") = False |
| 618 | / |
| 619 | [clinic start generated code]*/ |
| 620 | |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 621 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 622 | _codecs_mbcs_decode_impl(PyModuleDef *module, Py_buffer *data, |
| 623 | const char *errors, int final) |
| 624 | /*[clinic end generated code: output=0ebaf3a5b20e53fa input=d492c1ca64f4fa8a]*/ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 625 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 626 | Py_ssize_t consumed = data->len; |
| 627 | PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len, |
| 628 | errors, final ? NULL : &consumed); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 629 | return codec_tuple(decoded, consumed); |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 632 | /*[clinic input] |
| 633 | _codecs.code_page_decode |
| 634 | codepage: int |
| 635 | data: Py_buffer |
| 636 | errors: str(accept={str, NoneType}) = NULL |
| 637 | final: int(c_default="0") = False |
| 638 | / |
| 639 | [clinic start generated code]*/ |
| 640 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 641 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 642 | _codecs_code_page_decode_impl(PyModuleDef *module, int codepage, |
| 643 | Py_buffer *data, const char *errors, int final) |
| 644 | /*[clinic end generated code: output=4318e3d9971e31ba input=4f3152a304e21d51]*/ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 645 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 646 | Py_ssize_t consumed = data->len; |
Serhiy Storchaka | 1aa5e1d | 2015-05-12 14:00:22 +0300 | [diff] [blame] | 647 | PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 648 | data->buf, data->len, |
| 649 | errors, |
| 650 | final ? NULL : &consumed); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 651 | return codec_tuple(decoded, consumed); |
| 652 | } |
| 653 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 654 | #endif /* HAVE_MBCS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 656 | /* --- Encoder ------------------------------------------------------------ */ |
| 657 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 658 | /*[clinic input] |
| 659 | _codecs.readbuffer_encode |
| 660 | data: Py_buffer(accept={str, buffer}) |
| 661 | errors: str(accept={str, NoneType}) = NULL |
| 662 | / |
| 663 | [clinic start generated code]*/ |
| 664 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 665 | static PyObject * |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 666 | _codecs_readbuffer_encode_impl(PyModuleDef *module, Py_buffer *data, |
| 667 | const char *errors) |
| 668 | /*[clinic end generated code: output=319cc24083299859 input=b7c322b89d4ab923]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 669 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 670 | PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len); |
| 671 | return codec_tuple(result, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 674 | /*[clinic input] |
| 675 | _codecs.unicode_internal_encode |
| 676 | obj: object |
| 677 | errors: str(accept={str, NoneType}) = NULL |
| 678 | / |
| 679 | [clinic start generated code]*/ |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 680 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 681 | static PyObject * |
| 682 | _codecs_unicode_internal_encode_impl(PyModuleDef *module, PyObject *obj, |
| 683 | const char *errors) |
| 684 | /*[clinic end generated code: output=be08457068ad503b input=8628f0280cf5ba61]*/ |
| 685 | { |
Ezio Melotti | adc417c | 2011-11-17 12:23:34 +0200 | [diff] [blame] | 686 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 687 | "unicode_internal codec has been deprecated", |
| 688 | 1)) |
| 689 | return NULL; |
| 690 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 691 | if (PyUnicode_Check(obj)) { |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 692 | Py_UNICODE *u; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 693 | Py_ssize_t len, size; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 694 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 695 | if (PyUnicode_READY(obj) < 0) |
| 696 | return NULL; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 697 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 698 | u = PyUnicode_AsUnicodeAndSize(obj, &len); |
| 699 | if (u == NULL) |
| 700 | return NULL; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 701 | if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 702 | return PyErr_NoMemory(); |
| 703 | size = len * sizeof(Py_UNICODE); |
| 704 | return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size), |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 705 | PyUnicode_GET_LENGTH(obj)); |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 706 | } |
| 707 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 708 | Py_buffer view; |
| 709 | PyObject *result; |
| 710 | if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 712 | result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len), |
| 713 | view.len); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 714 | PyBuffer_Release(&view); |
| 715 | return result; |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 719 | /*[clinic input] |
| 720 | _codecs.utf_7_encode |
| 721 | str: object |
| 722 | errors: str(accept={str, NoneType}) = NULL |
| 723 | / |
| 724 | [clinic start generated code]*/ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 725 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 726 | static PyObject * |
| 727 | _codecs_utf_7_encode_impl(PyModuleDef *module, PyObject *str, |
| 728 | const char *errors) |
| 729 | /*[clinic end generated code: output=a7accc496a32b759 input=fd91a78f103b0421]*/ |
| 730 | { |
| 731 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 732 | |
| 733 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 734 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 735 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 737 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 738 | v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors), |
| 739 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 740 | Py_DECREF(str); |
| 741 | return v; |
| 742 | } |
| 743 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 744 | /*[clinic input] |
| 745 | _codecs.utf_8_encode |
| 746 | str: object |
| 747 | errors: str(accept={str, NoneType}) = NULL |
| 748 | / |
| 749 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 750 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 751 | static PyObject * |
| 752 | _codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str, |
| 753 | const char *errors) |
| 754 | /*[clinic end generated code: output=ec831d80e7aedede input=2c22d40532f071f3]*/ |
| 755 | { |
| 756 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 757 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 758 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 759 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 760 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 762 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors), |
| 764 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 765 | Py_DECREF(str); |
| 766 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* This version provides access to the byteorder parameter of the |
| 770 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 771 | which means: use the native byte order and prepend the data with a |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 772 | BOM mark. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 773 | |
| 774 | */ |
| 775 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 776 | /*[clinic input] |
| 777 | _codecs.utf_16_encode |
| 778 | str: object |
| 779 | errors: str(accept={str, NoneType}) = NULL |
| 780 | byteorder: int = 0 |
| 781 | / |
| 782 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 783 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 784 | static PyObject * |
| 785 | _codecs_utf_16_encode_impl(PyModuleDef *module, PyObject *str, |
| 786 | const char *errors, int byteorder) |
| 787 | /*[clinic end generated code: output=93ac58e960a9ee4d input=3935a489b2d5385e]*/ |
| 788 | { |
| 789 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 790 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 791 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 792 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 793 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 795 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 796 | v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder), |
| 797 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 798 | Py_DECREF(str); |
| 799 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 802 | /*[clinic input] |
| 803 | _codecs.utf_16_le_encode |
| 804 | str: object |
| 805 | errors: str(accept={str, NoneType}) = NULL |
| 806 | / |
| 807 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 808 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 809 | static PyObject * |
| 810 | _codecs_utf_16_le_encode_impl(PyModuleDef *module, PyObject *str, |
| 811 | const char *errors) |
| 812 | /*[clinic end generated code: output=422bedb8da34fb66 input=bc27df05d1d20dfe]*/ |
| 813 | { |
| 814 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 815 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 816 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 817 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 818 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 820 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 821 | v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1), |
| 822 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 823 | Py_DECREF(str); |
| 824 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 827 | /*[clinic input] |
| 828 | _codecs.utf_16_be_encode |
| 829 | str: object |
| 830 | errors: str(accept={str, NoneType}) = NULL |
| 831 | / |
| 832 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 833 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 834 | static PyObject * |
| 835 | _codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str, |
| 836 | const char *errors) |
| 837 | /*[clinic end generated code: output=3aa7ee9502acdd77 input=5a69d4112763462b]*/ |
| 838 | { |
| 839 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 840 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 841 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 842 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 843 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 845 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 846 | v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1), |
| 847 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 848 | Py_DECREF(str); |
| 849 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 852 | /* This version provides access to the byteorder parameter of the |
| 853 | builtin UTF-32 codecs as optional third argument. It defaults to 0 |
| 854 | which means: use the native byte order and prepend the data with a |
| 855 | BOM mark. |
| 856 | |
| 857 | */ |
| 858 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 859 | /*[clinic input] |
| 860 | _codecs.utf_32_encode |
| 861 | str: object |
| 862 | errors: str(accept={str, NoneType}) = NULL |
| 863 | byteorder: int = 0 |
| 864 | / |
| 865 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 866 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 867 | static PyObject * |
| 868 | _codecs_utf_32_encode_impl(PyModuleDef *module, PyObject *str, |
| 869 | const char *errors, int byteorder) |
| 870 | /*[clinic end generated code: output=3e7d5a003b02baed input=434a1efa492b8d58]*/ |
| 871 | { |
| 872 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 873 | |
| 874 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 875 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 876 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 878 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 879 | v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder), |
| 880 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 881 | Py_DECREF(str); |
| 882 | return v; |
| 883 | } |
| 884 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 885 | /*[clinic input] |
| 886 | _codecs.utf_32_le_encode |
| 887 | str: object |
| 888 | errors: str(accept={str, NoneType}) = NULL |
| 889 | / |
| 890 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 891 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 892 | static PyObject * |
| 893 | _codecs_utf_32_le_encode_impl(PyModuleDef *module, PyObject *str, |
| 894 | const char *errors) |
| 895 | /*[clinic end generated code: output=5dda641cd33dbfc2 input=dfa2d7dc78b99422]*/ |
| 896 | { |
| 897 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 898 | |
| 899 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 900 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 901 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 903 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 904 | v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1), |
| 905 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 906 | Py_DECREF(str); |
| 907 | return v; |
| 908 | } |
| 909 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 910 | /*[clinic input] |
| 911 | _codecs.utf_32_be_encode |
| 912 | str: object |
| 913 | errors: str(accept={str, NoneType}) = NULL |
| 914 | / |
| 915 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 916 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 917 | static PyObject * |
| 918 | _codecs_utf_32_be_encode_impl(PyModuleDef *module, PyObject *str, |
| 919 | const char *errors) |
| 920 | /*[clinic end generated code: output=ccca8b44d91a7c7a input=4595617b18169002]*/ |
| 921 | { |
| 922 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 923 | |
| 924 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 925 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 926 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 928 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 929 | v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1), |
| 930 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 931 | Py_DECREF(str); |
| 932 | return v; |
| 933 | } |
| 934 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 935 | /*[clinic input] |
| 936 | _codecs.unicode_escape_encode |
| 937 | str: object |
| 938 | errors: str(accept={str, NoneType}) = NULL |
| 939 | / |
| 940 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 941 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 942 | static PyObject * |
| 943 | _codecs_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str, |
| 944 | const char *errors) |
| 945 | /*[clinic end generated code: output=389f23d2b8f8d80b input=8273506f14076912]*/ |
| 946 | { |
| 947 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 948 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 949 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 950 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 951 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 953 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 954 | v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str), |
| 955 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 956 | Py_DECREF(str); |
| 957 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 960 | /*[clinic input] |
| 961 | _codecs.raw_unicode_escape_encode |
| 962 | str: object |
| 963 | errors: str(accept={str, NoneType}) = NULL |
| 964 | / |
| 965 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 966 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 967 | static PyObject * |
| 968 | _codecs_raw_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str, |
| 969 | const char *errors) |
| 970 | /*[clinic end generated code: output=fec4e39d6ec37a62 input=181755d5dfacef3c]*/ |
| 971 | { |
| 972 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 973 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 974 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 975 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 976 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 978 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 979 | v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str), |
| 980 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 981 | Py_DECREF(str); |
| 982 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 985 | /*[clinic input] |
| 986 | _codecs.latin_1_encode |
| 987 | str: object |
| 988 | errors: str(accept={str, NoneType}) = NULL |
| 989 | / |
| 990 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 991 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 992 | static PyObject * |
| 993 | _codecs_latin_1_encode_impl(PyModuleDef *module, PyObject *str, |
| 994 | const char *errors) |
| 995 | /*[clinic end generated code: output=ecf00eb8e48c889c input=f03f6dcf1d84bee4]*/ |
| 996 | { |
| 997 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 998 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 999 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1000 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 1001 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1003 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 1004 | v = codec_tuple(_PyUnicode_AsLatin1String(str, errors), |
| 1005 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1006 | Py_DECREF(str); |
| 1007 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1010 | /*[clinic input] |
| 1011 | _codecs.ascii_encode |
| 1012 | str: object |
| 1013 | errors: str(accept={str, NoneType}) = NULL |
| 1014 | / |
| 1015 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1016 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1017 | static PyObject * |
| 1018 | _codecs_ascii_encode_impl(PyModuleDef *module, PyObject *str, |
| 1019 | const char *errors) |
| 1020 | /*[clinic end generated code: output=a9d18fc6b6b91cfb input=d87e25a10a593fee]*/ |
| 1021 | { |
| 1022 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1023 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1024 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1025 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 1026 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1028 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 1029 | v = codec_tuple(_PyUnicode_AsASCIIString(str, errors), |
| 1030 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1031 | Py_DECREF(str); |
| 1032 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1035 | /*[clinic input] |
| 1036 | _codecs.charmap_encode |
| 1037 | str: object |
| 1038 | errors: str(accept={str, NoneType}) = NULL |
| 1039 | mapping: object = NULL |
| 1040 | / |
| 1041 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1042 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1043 | static PyObject * |
| 1044 | _codecs_charmap_encode_impl(PyModuleDef *module, PyObject *str, |
| 1045 | const char *errors, PyObject *mapping) |
| 1046 | /*[clinic end generated code: output=14ca42b83853c643 input=85f4172661e8dad9]*/ |
| 1047 | { |
| 1048 | PyObject *v; |
| 1049 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1050 | if (mapping == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | mapping = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1052 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1053 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1054 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 1055 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1057 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 1058 | v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors), |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 1059 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1060 | Py_DECREF(str); |
| 1061 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1064 | /*[clinic input] |
| 1065 | _codecs.charmap_build |
| 1066 | map: unicode |
| 1067 | / |
| 1068 | [clinic start generated code]*/ |
| 1069 | |
| 1070 | static PyObject * |
| 1071 | _codecs_charmap_build_impl(PyModuleDef *module, PyObject *map) |
| 1072 | /*[clinic end generated code: output=9485b58fa44afa6a input=d91a91d1717dbc6d]*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1073 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1074 | return PyUnicode_BuildEncodingMap(map); |
| 1075 | } |
| 1076 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 1077 | #ifdef HAVE_MBCS |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1078 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1079 | /*[clinic input] |
| 1080 | _codecs.mbcs_encode |
| 1081 | str: object |
| 1082 | errors: str(accept={str, NoneType}) = NULL |
| 1083 | / |
| 1084 | [clinic start generated code]*/ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1085 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1086 | static PyObject * |
| 1087 | _codecs_mbcs_encode_impl(PyModuleDef *module, PyObject *str, |
| 1088 | const char *errors) |
| 1089 | /*[clinic end generated code: output=d1a013bc68798bd7 input=65c09ee1e4203263]*/ |
| 1090 | { |
| 1091 | PyObject *v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1092 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1093 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1094 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 1095 | Py_XDECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1097 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 1098 | v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors), |
| 1099 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1100 | Py_DECREF(str); |
| 1101 | return v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1104 | /*[clinic input] |
| 1105 | _codecs.code_page_encode |
| 1106 | code_page: int |
| 1107 | str: object |
| 1108 | errors: str(accept={str, NoneType}) = NULL |
| 1109 | / |
| 1110 | [clinic start generated code]*/ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1111 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1112 | static PyObject * |
| 1113 | _codecs_code_page_encode_impl(PyModuleDef *module, int code_page, |
| 1114 | PyObject *str, const char *errors) |
| 1115 | /*[clinic end generated code: output=3b406618dbfbce25 input=c8562ec460c2e309]*/ |
| 1116 | { |
| 1117 | PyObject *v; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1118 | |
| 1119 | str = PyUnicode_FromObject(str); |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1120 | if (str == NULL || PyUnicode_READY(str) < 0) { |
| 1121 | Py_XDECREF(str); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1122 | return NULL; |
Antoine Pitrou | f72d4ef | 2011-11-12 18:05:15 +0100 | [diff] [blame] | 1123 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 1124 | v = codec_tuple(PyUnicode_EncodeCodePage(code_page, |
| 1125 | str, |
| 1126 | errors), |
| 1127 | PyUnicode_GET_LENGTH(str)); |
| 1128 | Py_DECREF(str); |
| 1129 | return v; |
| 1130 | } |
| 1131 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 1132 | #endif /* HAVE_MBCS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1133 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1134 | /* --- Error handler registry --------------------------------------------- */ |
| 1135 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1136 | /*[clinic input] |
| 1137 | _codecs.register_error |
| 1138 | errors: str |
| 1139 | handler: object |
| 1140 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1141 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1142 | Register the specified error handler under the name errors. |
| 1143 | |
| 1144 | handler must be a callable object, that will be called with an exception |
| 1145 | instance containing information about the location of the encoding/decoding |
| 1146 | error and must return a (replacement, new position) tuple. |
| 1147 | [clinic start generated code]*/ |
| 1148 | |
| 1149 | static PyObject * |
| 1150 | _codecs_register_error_impl(PyModuleDef *module, const char *errors, |
| 1151 | PyObject *handler) |
| 1152 | /*[clinic end generated code: output=be00d3b1849ce68a input=5e6709203c2e33fe]*/ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1153 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1154 | if (PyCodec_RegisterError(errors, handler)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1155 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1156 | Py_RETURN_NONE; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1159 | /*[clinic input] |
| 1160 | _codecs.lookup_error |
| 1161 | name: str |
| 1162 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1163 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1164 | lookup_error(errors) -> handler |
| 1165 | |
| 1166 | Return the error handler for the specified error handling name or raise a |
| 1167 | LookupError, if no handler exists under this name. |
| 1168 | [clinic start generated code]*/ |
| 1169 | |
| 1170 | static PyObject * |
| 1171 | _codecs_lookup_error_impl(PyModuleDef *module, const char *name) |
| 1172 | /*[clinic end generated code: output=731e6df8c83c6158 input=4775dd65e6235aba]*/ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1173 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1174 | return PyCodec_LookupError(name); |
| 1175 | } |
| 1176 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1177 | /* --- Module API --------------------------------------------------------- */ |
| 1178 | |
| 1179 | static PyMethodDef _codecs_functions[] = { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1180 | _CODECS_REGISTER_METHODDEF |
| 1181 | _CODECS_LOOKUP_METHODDEF |
| 1182 | _CODECS_ENCODE_METHODDEF |
| 1183 | _CODECS_DECODE_METHODDEF |
| 1184 | _CODECS_ESCAPE_ENCODE_METHODDEF |
| 1185 | _CODECS_ESCAPE_DECODE_METHODDEF |
| 1186 | _CODECS_UTF_8_ENCODE_METHODDEF |
| 1187 | _CODECS_UTF_8_DECODE_METHODDEF |
| 1188 | _CODECS_UTF_7_ENCODE_METHODDEF |
| 1189 | _CODECS_UTF_7_DECODE_METHODDEF |
| 1190 | _CODECS_UTF_16_ENCODE_METHODDEF |
| 1191 | _CODECS_UTF_16_LE_ENCODE_METHODDEF |
| 1192 | _CODECS_UTF_16_BE_ENCODE_METHODDEF |
| 1193 | _CODECS_UTF_16_DECODE_METHODDEF |
| 1194 | _CODECS_UTF_16_LE_DECODE_METHODDEF |
| 1195 | _CODECS_UTF_16_BE_DECODE_METHODDEF |
| 1196 | _CODECS_UTF_16_EX_DECODE_METHODDEF |
| 1197 | _CODECS_UTF_32_ENCODE_METHODDEF |
| 1198 | _CODECS_UTF_32_LE_ENCODE_METHODDEF |
| 1199 | _CODECS_UTF_32_BE_ENCODE_METHODDEF |
| 1200 | _CODECS_UTF_32_DECODE_METHODDEF |
| 1201 | _CODECS_UTF_32_LE_DECODE_METHODDEF |
| 1202 | _CODECS_UTF_32_BE_DECODE_METHODDEF |
| 1203 | _CODECS_UTF_32_EX_DECODE_METHODDEF |
| 1204 | _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF |
| 1205 | _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF |
| 1206 | _CODECS_UNICODE_INTERNAL_ENCODE_METHODDEF |
| 1207 | _CODECS_UNICODE_INTERNAL_DECODE_METHODDEF |
| 1208 | _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF |
| 1209 | _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF |
| 1210 | _CODECS_LATIN_1_ENCODE_METHODDEF |
| 1211 | _CODECS_LATIN_1_DECODE_METHODDEF |
| 1212 | _CODECS_ASCII_ENCODE_METHODDEF |
| 1213 | _CODECS_ASCII_DECODE_METHODDEF |
| 1214 | _CODECS_CHARMAP_ENCODE_METHODDEF |
| 1215 | _CODECS_CHARMAP_DECODE_METHODDEF |
| 1216 | _CODECS_CHARMAP_BUILD_METHODDEF |
| 1217 | _CODECS_READBUFFER_ENCODE_METHODDEF |
| 1218 | _CODECS_MBCS_ENCODE_METHODDEF |
| 1219 | _CODECS_MBCS_DECODE_METHODDEF |
| 1220 | _CODECS_CODE_PAGE_ENCODE_METHODDEF |
| 1221 | _CODECS_CODE_PAGE_DECODE_METHODDEF |
| 1222 | _CODECS_REGISTER_ERROR_METHODDEF |
| 1223 | _CODECS_LOOKUP_ERROR_METHODDEF |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 1224 | _CODECS__FORGET_CODEC_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1226 | }; |
| 1227 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1228 | static struct PyModuleDef codecsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | PyModuleDef_HEAD_INIT, |
| 1230 | "_codecs", |
| 1231 | NULL, |
| 1232 | -1, |
| 1233 | _codecs_functions, |
| 1234 | NULL, |
| 1235 | NULL, |
| 1236 | NULL, |
| 1237 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1238 | }; |
| 1239 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1240 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1241 | PyInit__codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | return PyModule_Create(&codecsmodule); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1244 | } |