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