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