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 | |
| 23 | These <encoding>s are available: utf_8, unicode_escape, |
Inada Naoki | 6a16b18 | 2019-03-18 15:44:11 +0900 | [diff] [blame] | 24 | raw_unicode_escape, latin_1, ascii (7-bit), mbcs (on win32). |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 26 | |
| 27 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 28 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 29 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 30 | |
| 31 | ------------------------------------------------------------------------ */ |
| 32 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 33 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 34 | #include "Python.h" |
| 35 | |
Victor Stinner | 87a7c82 | 2011-11-10 20:05:55 +0100 | [diff] [blame] | 36 | #ifdef MS_WINDOWS |
| 37 | #include <windows.h> |
| 38 | #endif |
| 39 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 40 | /*[clinic input] |
| 41 | module _codecs |
| 42 | [clinic start generated code]*/ |
| 43 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/ |
| 44 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 45 | #include "clinic/_codecsmodule.c.h" |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 46 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 47 | /* --- Registry ----------------------------------------------------------- */ |
| 48 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 49 | /*[clinic input] |
| 50 | _codecs.register |
| 51 | search_function: object |
| 52 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 53 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 54 | Register a codec search function. |
| 55 | |
| 56 | Search functions are expected to take one argument, the encoding name in |
| 57 | all lower case letters, and either return None, or a tuple of functions |
| 58 | (encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object). |
| 59 | [clinic start generated code]*/ |
| 60 | |
| 61 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 62 | _codecs_register(PyObject *module, PyObject *search_function) |
| 63 | /*[clinic end generated code: output=d1bf21e99db7d6d3 input=369578467955cae4]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 64 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 65 | if (PyCodec_Register(search_function)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 66 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 68 | Py_RETURN_NONE; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 71 | /*[clinic input] |
Hai Shi | d332e7b | 2020-09-29 05:41:11 +0800 | [diff] [blame] | 72 | _codecs.unregister |
| 73 | search_function: object |
| 74 | / |
| 75 | |
| 76 | Unregister a codec search function and clear the registry's cache. |
| 77 | |
| 78 | If the search function is not registered, do nothing. |
| 79 | [clinic start generated code]*/ |
| 80 | |
| 81 | static PyObject * |
| 82 | _codecs_unregister(PyObject *module, PyObject *search_function) |
| 83 | /*[clinic end generated code: output=1f0edee9cf246399 input=dd7c004c652d345e]*/ |
| 84 | { |
| 85 | if (PyCodec_Unregister(search_function) < 0) { |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | Py_RETURN_NONE; |
| 90 | } |
| 91 | |
| 92 | /*[clinic input] |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 93 | _codecs.lookup |
| 94 | encoding: str |
| 95 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 96 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 97 | Looks up a codec tuple in the Python codec registry and returns a CodecInfo object. |
| 98 | [clinic start generated code]*/ |
| 99 | |
| 100 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 101 | _codecs_lookup_impl(PyObject *module, const char *encoding) |
| 102 | /*[clinic end generated code: output=9f0afa572080c36d input=3c572c0db3febe9c]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 104 | return _PyCodec_Lookup(encoding); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 107 | /*[clinic input] |
| 108 | _codecs.encode |
| 109 | obj: object |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 110 | encoding: str(c_default="NULL") = "utf-8" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 111 | errors: str(c_default="NULL") = "strict" |
| 112 | |
| 113 | Encodes obj using the codec registered for encoding. |
| 114 | |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 115 | 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] | 116 | different error handling scheme. Default is 'strict' meaning that encoding |
| 117 | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
| 118 | and 'backslashreplace' as well as any other name registered with |
| 119 | codecs.register_error that can handle ValueErrors. |
| 120 | [clinic start generated code]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 121 | |
| 122 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 123 | _codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 124 | const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 125 | /*[clinic end generated code: output=385148eb9a067c86 input=cd5b685040ff61f0]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 126 | { |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 127 | if (encoding == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 129 | |
| 130 | /* Encode via the codec registry */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 131 | return PyCodec_Encode(obj, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 134 | /*[clinic input] |
| 135 | _codecs.decode |
| 136 | obj: object |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 137 | encoding: str(c_default="NULL") = "utf-8" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 138 | errors: str(c_default="NULL") = "strict" |
| 139 | |
| 140 | Decodes obj using the codec registered for encoding. |
| 141 | |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 142 | Default encoding is 'utf-8'. errors may be given to set a |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 143 | different error handling scheme. Default is 'strict' meaning that encoding |
| 144 | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
| 145 | and 'backslashreplace' as well as any other name registered with |
| 146 | codecs.register_error that can handle ValueErrors. |
| 147 | [clinic start generated code]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 148 | |
| 149 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 150 | _codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 151 | const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 152 | /*[clinic end generated code: output=679882417dc3a0bd input=7702c0cc2fa1add6]*/ |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 153 | { |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 154 | if (encoding == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 156 | |
| 157 | /* Decode via the codec registry */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 158 | return PyCodec_Decode(obj, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 161 | /* --- Helpers ------------------------------------------------------------ */ |
| 162 | |
| 163 | static |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 164 | PyObject *codec_tuple(PyObject *decoded, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | Py_ssize_t len) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 166 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 167 | if (decoded == NULL) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 168 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 169 | return Py_BuildValue("Nn", decoded, len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 172 | /* --- String codecs ------------------------------------------------------ */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 173 | /*[clinic input] |
| 174 | _codecs.escape_decode |
| 175 | data: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 176 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 177 | / |
| 178 | [clinic start generated code]*/ |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 179 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 180 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 181 | _codecs_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 182 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 183 | /*[clinic end generated code: output=505200ba8056979a input=77298a561c90bd82]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 184 | { |
| 185 | PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len, |
| 186 | errors, 0, NULL); |
| 187 | return codec_tuple(decoded, data->len); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 190 | /*[clinic input] |
| 191 | _codecs.escape_encode |
| 192 | data: object(subclass_of='&PyBytes_Type') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 193 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 194 | / |
| 195 | [clinic start generated code]*/ |
| 196 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 197 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 198 | _codecs_escape_encode_impl(PyObject *module, PyObject *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 199 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 200 | /*[clinic end generated code: output=4af1d477834bab34 input=8f4b144799a94245]*/ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 202 | Py_ssize_t size; |
| 203 | Py_ssize_t newsize; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 204 | PyObject *v; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 205 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 206 | size = PyBytes_GET_SIZE(data); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 207 | if (size > PY_SSIZE_T_MAX / 4) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 208 | PyErr_SetString(PyExc_OverflowError, |
| 209 | "string is too large to encode"); |
| 210 | return NULL; |
| 211 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 212 | newsize = 4*size; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 213 | v = PyBytes_FromStringAndSize(NULL, newsize); |
| 214 | |
| 215 | if (v == NULL) { |
| 216 | return NULL; |
| 217 | } |
| 218 | else { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 219 | Py_ssize_t i; |
| 220 | char c; |
| 221 | char *p = PyBytes_AS_STRING(v); |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 222 | |
| 223 | for (i = 0; i < size; i++) { |
| 224 | /* There's at least enough room for a hex escape */ |
| 225 | assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 226 | c = PyBytes_AS_STRING(data)[i]; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 227 | if (c == '\'' || c == '\\') |
| 228 | *p++ = '\\', *p++ = c; |
| 229 | else if (c == '\t') |
| 230 | *p++ = '\\', *p++ = 't'; |
| 231 | else if (c == '\n') |
| 232 | *p++ = '\\', *p++ = 'n'; |
| 233 | else if (c == '\r') |
| 234 | *p++ = '\\', *p++ = 'r'; |
| 235 | else if (c < ' ' || c >= 0x7f) { |
| 236 | *p++ = '\\'; |
| 237 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 238 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 239 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | } |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 244 | *p = '\0'; |
| 245 | if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) { |
| 246 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | } |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 248 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 249 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 250 | return codec_tuple(v, size); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 253 | /* --- Decoder ------------------------------------------------------------ */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 254 | /*[clinic input] |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 255 | _codecs.utf_7_decode |
| 256 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 257 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 258 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 259 | / |
| 260 | [clinic start generated code]*/ |
| 261 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 262 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 263 | _codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 264 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 265 | /*[clinic end generated code: output=0cd3a944a32a4089 input=22c395d357815d26]*/ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 266 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 267 | Py_ssize_t consumed = data->len; |
| 268 | PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len, |
| 269 | errors, |
| 270 | final ? NULL : &consumed); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 271 | return codec_tuple(decoded, consumed); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 274 | /*[clinic input] |
| 275 | _codecs.utf_8_decode |
| 276 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 277 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 278 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 279 | / |
| 280 | [clinic start generated code]*/ |
| 281 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 282 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 283 | _codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 284 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 285 | /*[clinic end generated code: output=10f74dec8d9bb8bf input=f611b3867352ba59]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 286 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 287 | Py_ssize_t consumed = data->len; |
| 288 | PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len, |
| 289 | errors, |
| 290 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 291 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 294 | /*[clinic input] |
| 295 | _codecs.utf_16_decode |
| 296 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 297 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 298 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 299 | / |
| 300 | [clinic start generated code]*/ |
| 301 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 302 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 303 | _codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 304 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 305 | /*[clinic end generated code: output=783b442abcbcc2d0 input=191d360bd7309180]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 306 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 307 | int byteorder = 0; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 308 | /* This is overwritten unless final is true. */ |
| 309 | Py_ssize_t consumed = data->len; |
| 310 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 311 | errors, &byteorder, |
| 312 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 313 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 316 | /*[clinic input] |
| 317 | _codecs.utf_16_le_decode |
| 318 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 319 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 320 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 321 | / |
| 322 | [clinic start generated code]*/ |
| 323 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 324 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 325 | _codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 326 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 327 | /*[clinic end generated code: output=899b9e6364379dcd input=c6904fdc27fb4724]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 328 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 329 | int byteorder = -1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 330 | /* This is overwritten unless final is true. */ |
| 331 | Py_ssize_t consumed = data->len; |
| 332 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 333 | errors, &byteorder, |
| 334 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 335 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 338 | /*[clinic input] |
| 339 | _codecs.utf_16_be_decode |
| 340 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 341 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 342 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 343 | / |
| 344 | [clinic start generated code]*/ |
| 345 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 346 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 347 | _codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 348 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 349 | /*[clinic end generated code: output=49f6465ea07669c8 input=e49012400974649b]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 350 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 351 | int byteorder = 1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 352 | /* This is overwritten unless final is true. */ |
| 353 | Py_ssize_t consumed = data->len; |
| 354 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 355 | errors, &byteorder, |
| 356 | final ? NULL : &consumed); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 357 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* This non-standard version also provides access to the byteorder |
| 361 | parameter of the builtin UTF-16 codec. |
| 362 | |
| 363 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 364 | being the value in effect at the end of data. |
| 365 | |
| 366 | */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 367 | /*[clinic input] |
| 368 | _codecs.utf_16_ex_decode |
| 369 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 370 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 371 | byteorder: int = 0 |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 372 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 373 | / |
| 374 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 375 | |
| 376 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 377 | _codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 378 | const char *errors, int byteorder, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 379 | /*[clinic end generated code: output=0f385f251ecc1988 input=5a9c19f2e6b6cf0e]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 380 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 381 | /* This is overwritten unless final is true. */ |
| 382 | Py_ssize_t consumed = data->len; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 383 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 384 | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
| 385 | errors, &byteorder, |
| 386 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 387 | if (decoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 389 | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
| 390 | } |
| 391 | |
| 392 | /*[clinic input] |
| 393 | _codecs.utf_32_decode |
| 394 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 395 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 396 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 397 | / |
| 398 | [clinic start generated code]*/ |
| 399 | |
| 400 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 401 | _codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 402 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 403 | /*[clinic end generated code: output=2fc961807f7b145f input=fd7193965627eb58]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 404 | { |
| 405 | int byteorder = 0; |
| 406 | /* This is overwritten unless final is true. */ |
| 407 | Py_ssize_t consumed = data->len; |
| 408 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 409 | errors, &byteorder, |
| 410 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 411 | return codec_tuple(decoded, consumed); |
| 412 | } |
| 413 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 414 | /*[clinic input] |
| 415 | _codecs.utf_32_le_decode |
| 416 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 417 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 418 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 419 | / |
| 420 | [clinic start generated code]*/ |
| 421 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 422 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 423 | _codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 424 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 425 | /*[clinic end generated code: output=ec8f46b67a94f3e6 input=9078ec70acfe7613]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 426 | { |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 427 | int byteorder = -1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 428 | /* This is overwritten unless final is true. */ |
| 429 | Py_ssize_t consumed = data->len; |
| 430 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 431 | errors, &byteorder, |
| 432 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 433 | return codec_tuple(decoded, consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 436 | /*[clinic input] |
| 437 | _codecs.utf_32_be_decode |
| 438 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 439 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 440 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 441 | / |
| 442 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 443 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 444 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 445 | _codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 446 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 447 | /*[clinic end generated code: output=ff82bae862c92c4e input=f1ae1bbbb86648ff]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 448 | { |
| 449 | int byteorder = 1; |
| 450 | /* This is overwritten unless final is true. */ |
| 451 | Py_ssize_t consumed = data->len; |
| 452 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 453 | errors, &byteorder, |
| 454 | final ? NULL : &consumed); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 455 | return codec_tuple(decoded, consumed); |
| 456 | } |
| 457 | |
| 458 | /* This non-standard version also provides access to the byteorder |
| 459 | parameter of the builtin UTF-32 codec. |
| 460 | |
| 461 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 462 | being the value in effect at the end of data. |
| 463 | |
| 464 | */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 465 | /*[clinic input] |
| 466 | _codecs.utf_32_ex_decode |
| 467 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 468 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 469 | byteorder: int = 0 |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 470 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 471 | / |
| 472 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 473 | |
| 474 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 475 | _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 476 | const char *errors, int byteorder, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 477 | /*[clinic end generated code: output=6bfb177dceaf4848 input=e46a73bc859d0bd0]*/ |
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 | Py_ssize_t consumed = data->len; |
| 480 | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
| 481 | errors, &byteorder, |
| 482 | final ? NULL : &consumed); |
| 483 | if (decoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | return NULL; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 485 | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 488 | /*[clinic input] |
| 489 | _codecs.unicode_escape_decode |
| 490 | data: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 491 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 492 | / |
| 493 | [clinic start generated code]*/ |
| 494 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 495 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 496 | _codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 497 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 498 | /*[clinic end generated code: output=3ca3c917176b82ab input=8328081a3a569bd6]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 499 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 500 | PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len, |
| 501 | errors); |
| 502 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 505 | /*[clinic input] |
| 506 | _codecs.raw_unicode_escape_decode |
| 507 | data: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 508 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 509 | / |
| 510 | [clinic start generated code]*/ |
| 511 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 512 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 513 | _codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 514 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 515 | /*[clinic end generated code: output=c98eeb56028070a6 input=d2f5159ce3b3392f]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 516 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 517 | PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len, |
| 518 | errors); |
| 519 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 522 | /*[clinic input] |
| 523 | _codecs.latin_1_decode |
| 524 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 525 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 526 | / |
| 527 | [clinic start generated code]*/ |
| 528 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 529 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 530 | _codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 531 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 532 | /*[clinic end generated code: output=07f3dfa3f72c7d8f input=76ca58fd6dcd08c7]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 533 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 534 | PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors); |
| 535 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 538 | /*[clinic input] |
| 539 | _codecs.ascii_decode |
| 540 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 541 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 542 | / |
| 543 | [clinic start generated code]*/ |
| 544 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 545 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 546 | _codecs_ascii_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 547 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 548 | /*[clinic end generated code: output=2627d72058d42429 input=e428a267a04b4481]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 549 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 550 | PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors); |
| 551 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 554 | /*[clinic input] |
| 555 | _codecs.charmap_decode |
| 556 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 557 | errors: str(accept={str, NoneType}) = None |
| 558 | mapping: object = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 559 | / |
| 560 | [clinic start generated code]*/ |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 561 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 562 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 563 | _codecs_charmap_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 564 | const char *errors, PyObject *mapping) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 565 | /*[clinic end generated code: output=2c335b09778cf895 input=15b69df43458eb40]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 566 | { |
| 567 | PyObject *decoded; |
| 568 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 569 | if (mapping == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | mapping = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 571 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 572 | decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors); |
| 573 | return codec_tuple(decoded, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 576 | #ifdef MS_WINDOWS |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 577 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 578 | /*[clinic input] |
| 579 | _codecs.mbcs_decode |
| 580 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 581 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 582 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 583 | / |
| 584 | [clinic start generated code]*/ |
| 585 | |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 586 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 587 | _codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 588 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 589 | /*[clinic end generated code: output=39b65b8598938c4b input=1c1d50f08fa53789]*/ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 590 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 591 | Py_ssize_t consumed = data->len; |
| 592 | PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len, |
| 593 | errors, final ? NULL : &consumed); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 594 | return codec_tuple(decoded, consumed); |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 597 | /*[clinic input] |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 598 | _codecs.oem_decode |
| 599 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 600 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 601 | final: bool(accept={int}) = False |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 602 | / |
| 603 | [clinic start generated code]*/ |
| 604 | |
| 605 | static PyObject * |
| 606 | _codecs_oem_decode_impl(PyObject *module, Py_buffer *data, |
| 607 | const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 608 | /*[clinic end generated code: output=da1617612f3fcad8 input=81b67cba811022e5]*/ |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 609 | { |
| 610 | Py_ssize_t consumed = data->len; |
| 611 | PyObject *decoded = PyUnicode_DecodeCodePageStateful(CP_OEMCP, |
| 612 | data->buf, data->len, errors, final ? NULL : &consumed); |
| 613 | return codec_tuple(decoded, consumed); |
| 614 | } |
| 615 | |
| 616 | /*[clinic input] |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 617 | _codecs.code_page_decode |
| 618 | codepage: int |
| 619 | data: Py_buffer |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 620 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 621 | final: bool(accept={int}) = False |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 622 | / |
| 623 | [clinic start generated code]*/ |
| 624 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 625 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 626 | _codecs_code_page_decode_impl(PyObject *module, int codepage, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 627 | Py_buffer *data, const char *errors, int final) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 628 | /*[clinic end generated code: output=53008ea967da3fff input=c5f58d036cb63575]*/ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 629 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 630 | Py_ssize_t consumed = data->len; |
Serhiy Storchaka | 1aa5e1d | 2015-05-12 14:00:22 +0300 | [diff] [blame] | 631 | PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 632 | data->buf, data->len, |
| 633 | errors, |
| 634 | final ? NULL : &consumed); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 635 | return codec_tuple(decoded, consumed); |
| 636 | } |
| 637 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 638 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 639 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 640 | /* --- Encoder ------------------------------------------------------------ */ |
| 641 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 642 | /*[clinic input] |
| 643 | _codecs.readbuffer_encode |
| 644 | data: Py_buffer(accept={str, buffer}) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 645 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 646 | / |
| 647 | [clinic start generated code]*/ |
| 648 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 649 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 650 | _codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 651 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 652 | /*[clinic end generated code: output=c645ea7cdb3d6e86 input=aa10cfdf252455c5]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 653 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 654 | PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len); |
| 655 | return codec_tuple(result, data->len); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 658 | /*[clinic input] |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 659 | _codecs.utf_7_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 660 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 661 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 662 | / |
| 663 | [clinic start generated code]*/ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 664 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 665 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 666 | _codecs_utf_7_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 667 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 668 | /*[clinic end generated code: output=0feda21ffc921bc8 input=2546dbbb3fa53114]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 669 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 670 | return codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors), |
| 671 | PyUnicode_GET_LENGTH(str)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 674 | /*[clinic input] |
| 675 | _codecs.utf_8_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 676 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 677 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 678 | / |
| 679 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 680 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 681 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 682 | _codecs_utf_8_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 683 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 684 | /*[clinic end generated code: output=02bf47332b9c796c input=a3e71ae01c3f93f3]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 685 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 686 | return codec_tuple(_PyUnicode_AsUTF8String(str, errors), |
| 687 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* This version provides access to the byteorder parameter of the |
| 691 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 692 | 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] | 693 | BOM mark. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 694 | |
| 695 | */ |
| 696 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 697 | /*[clinic input] |
| 698 | _codecs.utf_16_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 699 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 700 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 701 | byteorder: int = 0 |
| 702 | / |
| 703 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 704 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 705 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 706 | _codecs_utf_16_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 707 | const char *errors, int byteorder) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 708 | /*[clinic end generated code: output=c654e13efa2e64e4 input=68cdc2eb8338555d]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 709 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 710 | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder), |
| 711 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 714 | /*[clinic input] |
| 715 | _codecs.utf_16_le_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 716 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 717 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 718 | / |
| 719 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 720 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 721 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 722 | _codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 723 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 724 | /*[clinic end generated code: output=431b01e55f2d4995 input=83d042706eed6798]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 725 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 726 | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1), |
| 727 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 730 | /*[clinic input] |
| 731 | _codecs.utf_16_be_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 732 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 733 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 734 | / |
| 735 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 736 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 737 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 738 | _codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 739 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 740 | /*[clinic end generated code: output=96886a6fd54dcae3 input=6f1e9e623b03071b]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 741 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 742 | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1), |
| 743 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 746 | /* This version provides access to the byteorder parameter of the |
| 747 | builtin UTF-32 codecs as optional third argument. It defaults to 0 |
| 748 | which means: use the native byte order and prepend the data with a |
| 749 | BOM mark. |
| 750 | |
| 751 | */ |
| 752 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 753 | /*[clinic input] |
| 754 | _codecs.utf_32_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 755 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 756 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 757 | byteorder: int = 0 |
| 758 | / |
| 759 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 760 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 761 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 762 | _codecs_utf_32_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 763 | const char *errors, int byteorder) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 764 | /*[clinic end generated code: output=5c760da0c09a8b83 input=8ec4c64d983bc52b]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 765 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 766 | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder), |
| 767 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 770 | /*[clinic input] |
| 771 | _codecs.utf_32_le_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 772 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 773 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 774 | / |
| 775 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 776 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 777 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 778 | _codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 779 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 780 | /*[clinic end generated code: output=b65cd176de8e36d6 input=f0918d41de3eb1b1]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 781 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 782 | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1), |
| 783 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 786 | /*[clinic input] |
| 787 | _codecs.utf_32_be_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 788 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 789 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 790 | / |
| 791 | [clinic start generated code]*/ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 792 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 793 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 794 | _codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 795 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 796 | /*[clinic end generated code: output=1d9e71a9358709e9 input=967a99a95748b557]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 797 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 798 | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1), |
| 799 | PyUnicode_GET_LENGTH(str)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 802 | /*[clinic input] |
| 803 | _codecs.unicode_escape_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 804 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 805 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 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 * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 810 | _codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 811 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 812 | /*[clinic end generated code: output=66271b30bc4f7a3c input=8c4de07597054e33]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 813 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 814 | return codec_tuple(PyUnicode_AsUnicodeEscapeString(str), |
| 815 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 818 | /*[clinic input] |
| 819 | _codecs.raw_unicode_escape_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 820 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 821 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 822 | / |
| 823 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 824 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 825 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 826 | _codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 827 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 828 | /*[clinic end generated code: output=a66a806ed01c830a input=4aa6f280d78e4574]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 829 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 830 | return codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str), |
| 831 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 834 | /*[clinic input] |
| 835 | _codecs.latin_1_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 836 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 837 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 838 | / |
| 839 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 840 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 841 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 842 | _codecs_latin_1_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 843 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 844 | /*[clinic end generated code: output=2c28c83a27884e08 input=ec3ef74bf85c5c5d]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 845 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 846 | return codec_tuple(_PyUnicode_AsLatin1String(str, errors), |
| 847 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 850 | /*[clinic input] |
| 851 | _codecs.ascii_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 852 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 853 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 854 | / |
| 855 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 856 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 857 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 858 | _codecs_ascii_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 859 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 860 | /*[clinic end generated code: output=b5e035182d33befc input=93e6e602838bd3de]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 861 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 862 | return codec_tuple(_PyUnicode_AsASCIIString(str, errors), |
| 863 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 866 | /*[clinic input] |
| 867 | _codecs.charmap_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 868 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 869 | errors: str(accept={str, NoneType}) = None |
| 870 | mapping: object = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 871 | / |
| 872 | [clinic start generated code]*/ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 873 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 874 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 875 | _codecs_charmap_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 876 | const char *errors, PyObject *mapping) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 877 | /*[clinic end generated code: output=047476f48495a9e9 input=2a98feae73dadce8]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 878 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 879 | if (mapping == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | mapping = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 881 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 882 | return codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors), |
| 883 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 886 | /*[clinic input] |
| 887 | _codecs.charmap_build |
| 888 | map: unicode |
| 889 | / |
| 890 | [clinic start generated code]*/ |
| 891 | |
| 892 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 893 | _codecs_charmap_build_impl(PyObject *module, PyObject *map) |
| 894 | /*[clinic end generated code: output=bb073c27031db9ac input=d91a91d1717dbc6d]*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 895 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 896 | return PyUnicode_BuildEncodingMap(map); |
| 897 | } |
| 898 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 899 | #ifdef MS_WINDOWS |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 900 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 901 | /*[clinic input] |
| 902 | _codecs.mbcs_encode |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 903 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 904 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 905 | / |
| 906 | [clinic start generated code]*/ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 907 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 908 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 909 | _codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 910 | /*[clinic end generated code: output=76e2e170c966c080 input=2e932fc289ea5a5b]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 911 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 912 | return codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors), |
| 913 | PyUnicode_GET_LENGTH(str)); |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 916 | /*[clinic input] |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 917 | _codecs.oem_encode |
| 918 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 919 | errors: str(accept={str, NoneType}) = None |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 920 | / |
| 921 | [clinic start generated code]*/ |
| 922 | |
| 923 | static PyObject * |
| 924 | _codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 925 | /*[clinic end generated code: output=65d5982c737de649 input=9eac86dc21eb14f2]*/ |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 926 | { |
| 927 | return codec_tuple(PyUnicode_EncodeCodePage(CP_OEMCP, str, errors), |
| 928 | PyUnicode_GET_LENGTH(str)); |
| 929 | } |
| 930 | |
| 931 | /*[clinic input] |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 932 | _codecs.code_page_encode |
| 933 | code_page: int |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 934 | str: unicode |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 935 | errors: str(accept={str, NoneType}) = None |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 936 | / |
| 937 | [clinic start generated code]*/ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 938 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 939 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 940 | _codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str, |
| 941 | const char *errors) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 942 | /*[clinic end generated code: output=45673f6085657a9e input=7d18a33bc8cd0f94]*/ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 943 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 944 | return codec_tuple(PyUnicode_EncodeCodePage(code_page, str, errors), |
| 945 | PyUnicode_GET_LENGTH(str)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 948 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 949 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 950 | /* --- Error handler registry --------------------------------------------- */ |
| 951 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 952 | /*[clinic input] |
| 953 | _codecs.register_error |
| 954 | errors: str |
| 955 | handler: object |
| 956 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 957 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 958 | Register the specified error handler under the name errors. |
| 959 | |
| 960 | handler must be a callable object, that will be called with an exception |
| 961 | instance containing information about the location of the encoding/decoding |
| 962 | error and must return a (replacement, new position) tuple. |
| 963 | [clinic start generated code]*/ |
| 964 | |
| 965 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 966 | _codecs_register_error_impl(PyObject *module, const char *errors, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 967 | PyObject *handler) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 968 | /*[clinic end generated code: output=fa2f7d1879b3067d input=5e6709203c2e33fe]*/ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 969 | { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 970 | if (PyCodec_RegisterError(errors, handler)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 971 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 972 | Py_RETURN_NONE; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 975 | /*[clinic input] |
| 976 | _codecs.lookup_error |
| 977 | name: str |
| 978 | / |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 979 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 980 | lookup_error(errors) -> handler |
| 981 | |
| 982 | Return the error handler for the specified error handling name or raise a |
| 983 | LookupError, if no handler exists under this name. |
| 984 | [clinic start generated code]*/ |
| 985 | |
| 986 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 987 | _codecs_lookup_error_impl(PyObject *module, const char *name) |
| 988 | /*[clinic end generated code: output=087f05dc0c9a98cc input=4775dd65e6235aba]*/ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 989 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 990 | return PyCodec_LookupError(name); |
| 991 | } |
| 992 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 993 | /* --- Module API --------------------------------------------------------- */ |
| 994 | |
| 995 | static PyMethodDef _codecs_functions[] = { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 996 | _CODECS_REGISTER_METHODDEF |
Hai Shi | d332e7b | 2020-09-29 05:41:11 +0800 | [diff] [blame] | 997 | _CODECS_UNREGISTER_METHODDEF |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 998 | _CODECS_LOOKUP_METHODDEF |
| 999 | _CODECS_ENCODE_METHODDEF |
| 1000 | _CODECS_DECODE_METHODDEF |
| 1001 | _CODECS_ESCAPE_ENCODE_METHODDEF |
| 1002 | _CODECS_ESCAPE_DECODE_METHODDEF |
| 1003 | _CODECS_UTF_8_ENCODE_METHODDEF |
| 1004 | _CODECS_UTF_8_DECODE_METHODDEF |
| 1005 | _CODECS_UTF_7_ENCODE_METHODDEF |
| 1006 | _CODECS_UTF_7_DECODE_METHODDEF |
| 1007 | _CODECS_UTF_16_ENCODE_METHODDEF |
| 1008 | _CODECS_UTF_16_LE_ENCODE_METHODDEF |
| 1009 | _CODECS_UTF_16_BE_ENCODE_METHODDEF |
| 1010 | _CODECS_UTF_16_DECODE_METHODDEF |
| 1011 | _CODECS_UTF_16_LE_DECODE_METHODDEF |
| 1012 | _CODECS_UTF_16_BE_DECODE_METHODDEF |
| 1013 | _CODECS_UTF_16_EX_DECODE_METHODDEF |
| 1014 | _CODECS_UTF_32_ENCODE_METHODDEF |
| 1015 | _CODECS_UTF_32_LE_ENCODE_METHODDEF |
| 1016 | _CODECS_UTF_32_BE_ENCODE_METHODDEF |
| 1017 | _CODECS_UTF_32_DECODE_METHODDEF |
| 1018 | _CODECS_UTF_32_LE_DECODE_METHODDEF |
| 1019 | _CODECS_UTF_32_BE_DECODE_METHODDEF |
| 1020 | _CODECS_UTF_32_EX_DECODE_METHODDEF |
| 1021 | _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF |
| 1022 | _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1023 | _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF |
| 1024 | _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF |
| 1025 | _CODECS_LATIN_1_ENCODE_METHODDEF |
| 1026 | _CODECS_LATIN_1_DECODE_METHODDEF |
| 1027 | _CODECS_ASCII_ENCODE_METHODDEF |
| 1028 | _CODECS_ASCII_DECODE_METHODDEF |
| 1029 | _CODECS_CHARMAP_ENCODE_METHODDEF |
| 1030 | _CODECS_CHARMAP_DECODE_METHODDEF |
| 1031 | _CODECS_CHARMAP_BUILD_METHODDEF |
| 1032 | _CODECS_READBUFFER_ENCODE_METHODDEF |
| 1033 | _CODECS_MBCS_ENCODE_METHODDEF |
| 1034 | _CODECS_MBCS_DECODE_METHODDEF |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1035 | _CODECS_OEM_ENCODE_METHODDEF |
| 1036 | _CODECS_OEM_DECODE_METHODDEF |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1037 | _CODECS_CODE_PAGE_ENCODE_METHODDEF |
| 1038 | _CODECS_CODE_PAGE_DECODE_METHODDEF |
| 1039 | _CODECS_REGISTER_ERROR_METHODDEF |
| 1040 | _CODECS_LOOKUP_ERROR_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1042 | }; |
| 1043 | |
Hai Shi | 1ea45ae | 2020-02-11 05:16:38 -0600 | [diff] [blame] | 1044 | static PyModuleDef_Slot _codecs_slots[] = { |
| 1045 | {0, NULL} |
| 1046 | }; |
| 1047 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1048 | static struct PyModuleDef codecsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | PyModuleDef_HEAD_INIT, |
| 1050 | "_codecs", |
| 1051 | NULL, |
Hai Shi | 1ea45ae | 2020-02-11 05:16:38 -0600 | [diff] [blame] | 1052 | 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | _codecs_functions, |
Hai Shi | 1ea45ae | 2020-02-11 05:16:38 -0600 | [diff] [blame] | 1054 | _codecs_slots, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | NULL, |
| 1056 | NULL, |
| 1057 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1058 | }; |
| 1059 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1060 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1061 | PyInit__codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1062 | { |
Hai Shi | 1ea45ae | 2020-02-11 05:16:38 -0600 | [diff] [blame] | 1063 | return PyModuleDef_Init(&codecsmodule); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1064 | } |