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