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 | |
| 13 | lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer) |
| 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']) -> |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 18 | (string object, bytes consumed) |
| 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 | |
| 41 | /* --- Registry ----------------------------------------------------------- */ |
| 42 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 43 | PyDoc_STRVAR(register__doc__, |
| 44 | "register(search_function)\n\ |
| 45 | \n\ |
| 46 | Register a codec search function. Search functions are expected to take\n\ |
| 47 | one argument, the encoding name in all lower case letters, and return\n\ |
| 48 | a tuple of functions (encoder, decoder, stream_reader, stream_writer)."); |
| 49 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 50 | static |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 51 | PyObject *codec_register(PyObject *self, PyObject *search_function) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 52 | { |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 53 | if (PyCodec_Register(search_function)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 54 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 55 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 56 | Py_RETURN_NONE; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 59 | PyDoc_STRVAR(lookup__doc__, |
| 60 | "lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)\n\ |
| 61 | \n\ |
| 62 | Looks up a codec tuple in the Python codec registry and returns\n\ |
| 63 | a tuple of functions."); |
| 64 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 65 | static |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 66 | PyObject *codec_lookup(PyObject *self, PyObject *args) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 67 | { |
| 68 | char *encoding; |
| 69 | |
| 70 | if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 71 | return NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 72 | |
| 73 | return _PyCodec_Lookup(encoding); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 76 | PyDoc_STRVAR(encode__doc__, |
| 77 | "encode(obj, [encoding[,errors]]) -> object\n\ |
| 78 | \n\ |
| 79 | Encodes obj using the codec registered for encoding. encoding defaults\n\ |
| 80 | to the default encoding. errors may be given to set a different error\n\ |
| 81 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 82 | a ValueError. Other possible values are 'ignore', 'replace' and\n\ |
| 83 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 84 | codecs.register_error that can handle ValueErrors."); |
| 85 | |
| 86 | static PyObject * |
| 87 | codec_encode(PyObject *self, PyObject *args) |
| 88 | { |
Brett Cannon | 3e377de | 2004-07-10 21:41:14 +0000 | [diff] [blame] | 89 | const char *encoding = NULL; |
| 90 | const char *errors = NULL; |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 91 | PyObject *v; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 92 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 93 | if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors)) |
| 94 | return NULL; |
| 95 | |
| 96 | if (encoding == NULL) |
| 97 | encoding = PyUnicode_GetDefaultEncoding(); |
| 98 | |
| 99 | /* Encode via the codec registry */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 100 | return PyCodec_Encode(v, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | PyDoc_STRVAR(decode__doc__, |
| 104 | "decode(obj, [encoding[,errors]]) -> object\n\ |
| 105 | \n\ |
| 106 | Decodes obj using the codec registered for encoding. encoding defaults\n\ |
| 107 | to the default encoding. errors may be given to set a different error\n\ |
| 108 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 109 | a ValueError. Other possible values are 'ignore' and 'replace'\n\ |
| 110 | as well as any other name registerd with codecs.register_error that is\n\ |
| 111 | able to handle ValueErrors."); |
| 112 | |
| 113 | static PyObject * |
| 114 | codec_decode(PyObject *self, PyObject *args) |
| 115 | { |
Brett Cannon | 3e377de | 2004-07-10 21:41:14 +0000 | [diff] [blame] | 116 | const char *encoding = NULL; |
| 117 | const char *errors = NULL; |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 118 | PyObject *v; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 119 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 120 | if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors)) |
| 121 | return NULL; |
| 122 | |
| 123 | if (encoding == NULL) |
| 124 | encoding = PyUnicode_GetDefaultEncoding(); |
| 125 | |
| 126 | /* Decode via the codec registry */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 127 | return PyCodec_Decode(v, encoding, errors); |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 130 | /* --- Helpers ------------------------------------------------------------ */ |
| 131 | |
| 132 | static |
| 133 | PyObject *codec_tuple(PyObject *unicode, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 134 | Py_ssize_t len) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 135 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 136 | PyObject *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 137 | if (unicode == NULL) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | v = Py_BuildValue("On", unicode, len); |
| 140 | Py_DECREF(unicode); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 141 | return v; |
| 142 | } |
| 143 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 144 | /* --- String codecs ------------------------------------------------------ */ |
| 145 | static PyObject * |
| 146 | escape_decode(PyObject *self, |
| 147 | PyObject *args) |
| 148 | { |
| 149 | const char *errors = NULL; |
| 150 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 151 | Py_ssize_t size; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 152 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 153 | if (!PyArg_ParseTuple(args, "s#|z:escape_decode", |
| 154 | &data, &size, &errors)) |
| 155 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 156 | return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL), |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 157 | size); |
| 158 | } |
| 159 | |
| 160 | static PyObject * |
| 161 | escape_encode(PyObject *self, |
| 162 | PyObject *args) |
| 163 | { |
| 164 | PyObject *str; |
| 165 | const char *errors = NULL; |
| 166 | char *buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 167 | Py_ssize_t len; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 168 | |
| 169 | if (!PyArg_ParseTuple(args, "O!|z:escape_encode", |
| 170 | &PyString_Type, &str, &errors)) |
| 171 | return NULL; |
| 172 | |
| 173 | str = PyString_Repr(str, 0); |
| 174 | if (!str) |
| 175 | return NULL; |
| 176 | |
| 177 | /* The string will be quoted. Unquote, similar to unicode-escape. */ |
| 178 | buf = PyString_AS_STRING (str); |
| 179 | len = PyString_GET_SIZE (str); |
| 180 | memmove(buf, buf+1, len-2); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 181 | if (_PyString_Resize(&str, len-2) < 0) |
| 182 | return NULL; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 183 | |
| 184 | return codec_tuple(str, PyString_Size(str)); |
| 185 | } |
| 186 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 187 | /* --- Decoder ------------------------------------------------------------ */ |
| 188 | |
| 189 | static PyObject * |
| 190 | unicode_internal_decode(PyObject *self, |
| 191 | PyObject *args) |
| 192 | { |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 193 | PyObject *obj; |
| 194 | const char *errors = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 195 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 196 | Py_ssize_t size; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 197 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 198 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", |
| 199 | &obj, &errors)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 200 | return NULL; |
| 201 | |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 202 | if (PyUnicode_Check(obj)) { |
| 203 | Py_INCREF(obj); |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 204 | return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 205 | } |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 206 | else { |
| 207 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 208 | return NULL; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 209 | |
| 210 | return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors), |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 211 | size); |
| 212 | } |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static PyObject * |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 216 | utf_7_decode(PyObject *self, |
| 217 | PyObject *args) |
| 218 | { |
| 219 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 220 | Py_ssize_t size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 221 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 222 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 223 | if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode", |
| 224 | &data, &size, &errors)) |
| 225 | return NULL; |
| 226 | |
| 227 | return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors), |
| 228 | size); |
| 229 | } |
| 230 | |
| 231 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 232 | utf_8_decode(PyObject *self, |
| 233 | PyObject *args) |
| 234 | { |
| 235 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 236 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 237 | const char *errors = NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 238 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 239 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 240 | PyObject *decoded = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 241 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 242 | if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode", |
| 243 | &data, &size, &errors, &final)) |
| 244 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 245 | if (size < 0) { |
| 246 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 247 | return 0; |
| 248 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 249 | consumed = size; |
| 250 | |
| 251 | decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors, |
| 252 | final ? NULL : &consumed); |
| 253 | if (decoded == NULL) |
| 254 | return NULL; |
| 255 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static PyObject * |
| 259 | utf_16_decode(PyObject *self, |
| 260 | PyObject *args) |
| 261 | { |
| 262 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 263 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 264 | const char *errors = NULL; |
| 265 | int byteorder = 0; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 266 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 267 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 268 | PyObject *decoded; |
| 269 | |
| 270 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode", |
| 271 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 272 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 273 | if (size < 0) { |
| 274 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 275 | return 0; |
| 276 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 277 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 278 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, |
| 279 | final ? NULL : &consumed); |
| 280 | if (decoded == NULL) |
| 281 | return NULL; |
| 282 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static PyObject * |
| 286 | utf_16_le_decode(PyObject *self, |
| 287 | PyObject *args) |
| 288 | { |
| 289 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 290 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 291 | const char *errors = NULL; |
| 292 | int byteorder = -1; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 293 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 294 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 295 | PyObject *decoded = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 296 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 297 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode", |
| 298 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 299 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 300 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 301 | if (size < 0) { |
| 302 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 303 | return 0; |
| 304 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 305 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 306 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, |
| 307 | &byteorder, final ? NULL : &consumed); |
| 308 | if (decoded == NULL) |
| 309 | return NULL; |
| 310 | return codec_tuple(decoded, consumed); |
| 311 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyObject * |
| 315 | utf_16_be_decode(PyObject *self, |
| 316 | PyObject *args) |
| 317 | { |
| 318 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 319 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 320 | const char *errors = NULL; |
| 321 | int byteorder = 1; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 322 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 323 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 324 | PyObject *decoded = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 325 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 326 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode", |
| 327 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 328 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 329 | if (size < 0) { |
| 330 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 331 | return 0; |
| 332 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 333 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 334 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, |
| 335 | &byteorder, final ? NULL : &consumed); |
| 336 | if (decoded == NULL) |
| 337 | return NULL; |
| 338 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* This non-standard version also provides access to the byteorder |
| 342 | parameter of the builtin UTF-16 codec. |
| 343 | |
| 344 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 345 | being the value in effect at the end of data. |
| 346 | |
| 347 | */ |
| 348 | |
| 349 | static PyObject * |
| 350 | utf_16_ex_decode(PyObject *self, |
| 351 | PyObject *args) |
| 352 | { |
| 353 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 354 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 355 | const char *errors = NULL; |
| 356 | int byteorder = 0; |
| 357 | PyObject *unicode, *tuple; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 358 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 359 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 360 | |
| 361 | if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode", |
| 362 | &data, &size, &errors, &byteorder, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 363 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 364 | if (size < 0) { |
| 365 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 366 | return 0; |
| 367 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 368 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 369 | unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, |
| 370 | final ? NULL : &consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 371 | if (unicode == NULL) |
| 372 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 373 | tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 374 | Py_DECREF(unicode); |
| 375 | return tuple; |
| 376 | } |
| 377 | |
| 378 | static PyObject * |
| 379 | unicode_escape_decode(PyObject *self, |
| 380 | PyObject *args) |
| 381 | { |
| 382 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 383 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 384 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 386 | if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", |
| 387 | &data, &size, &errors)) |
| 388 | return NULL; |
| 389 | |
| 390 | return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), |
| 391 | size); |
| 392 | } |
| 393 | |
| 394 | static PyObject * |
| 395 | raw_unicode_escape_decode(PyObject *self, |
| 396 | PyObject *args) |
| 397 | { |
| 398 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 399 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 400 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 402 | if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode", |
| 403 | &data, &size, &errors)) |
| 404 | return NULL; |
| 405 | |
| 406 | return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), |
| 407 | size); |
| 408 | } |
| 409 | |
| 410 | static PyObject * |
| 411 | latin_1_decode(PyObject *self, |
| 412 | PyObject *args) |
| 413 | { |
| 414 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 415 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 416 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 418 | if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode", |
| 419 | &data, &size, &errors)) |
| 420 | return NULL; |
| 421 | |
| 422 | return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), |
| 423 | size); |
| 424 | } |
| 425 | |
| 426 | static PyObject * |
| 427 | ascii_decode(PyObject *self, |
| 428 | PyObject *args) |
| 429 | { |
| 430 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 431 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 432 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 434 | if (!PyArg_ParseTuple(args, "t#|z:ascii_decode", |
| 435 | &data, &size, &errors)) |
| 436 | return NULL; |
| 437 | |
| 438 | return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), |
| 439 | size); |
| 440 | } |
| 441 | |
| 442 | static PyObject * |
| 443 | charmap_decode(PyObject *self, |
| 444 | PyObject *args) |
| 445 | { |
| 446 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 447 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 448 | const char *errors = NULL; |
| 449 | PyObject *mapping = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 451 | if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", |
| 452 | &data, &size, &errors, &mapping)) |
| 453 | return NULL; |
| 454 | if (mapping == Py_None) |
| 455 | mapping = NULL; |
| 456 | |
| 457 | return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), |
| 458 | size); |
| 459 | } |
| 460 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 461 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 462 | |
| 463 | static PyObject * |
| 464 | mbcs_decode(PyObject *self, |
| 465 | PyObject *args) |
| 466 | { |
| 467 | const char *data; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 468 | Py_ssize_t size, consumed; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 469 | const char *errors = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 470 | int final = 0; |
| 471 | PyObject *decoded; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 473 | if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode", |
| 474 | &data, &size, &errors, &final)) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 475 | return NULL; |
| 476 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 477 | decoded = PyUnicode_DecodeMBCSStateful( |
| 478 | data, size, errors, final ? NULL : &consumed); |
| 479 | if (!decoded) |
| 480 | return NULL; |
| 481 | return codec_tuple(decoded, final ? size : consumed); |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 484 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 486 | /* --- Encoder ------------------------------------------------------------ */ |
| 487 | |
| 488 | static PyObject * |
| 489 | readbuffer_encode(PyObject *self, |
| 490 | PyObject *args) |
| 491 | { |
| 492 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 493 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 494 | const char *errors = NULL; |
| 495 | |
| 496 | if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode", |
| 497 | &data, &size, &errors)) |
| 498 | return NULL; |
| 499 | |
| 500 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 501 | size); |
| 502 | } |
| 503 | |
| 504 | static PyObject * |
| 505 | charbuffer_encode(PyObject *self, |
| 506 | PyObject *args) |
| 507 | { |
| 508 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 509 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 510 | const char *errors = NULL; |
| 511 | |
| 512 | if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode", |
| 513 | &data, &size, &errors)) |
| 514 | return NULL; |
| 515 | |
| 516 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 517 | size); |
| 518 | } |
| 519 | |
| 520 | static PyObject * |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 521 | unicode_internal_encode(PyObject *self, |
| 522 | PyObject *args) |
| 523 | { |
| 524 | PyObject *obj; |
| 525 | const char *errors = NULL; |
| 526 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 527 | Py_ssize_t size; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 528 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 529 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode", |
| 530 | &obj, &errors)) |
| 531 | return NULL; |
| 532 | |
| 533 | if (PyUnicode_Check(obj)) { |
| 534 | data = PyUnicode_AS_DATA(obj); |
| 535 | size = PyUnicode_GET_DATA_SIZE(obj); |
| 536 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 537 | size); |
| 538 | } |
| 539 | else { |
| 540 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 541 | return NULL; |
| 542 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 543 | size); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | static PyObject * |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 548 | utf_7_encode(PyObject *self, |
| 549 | PyObject *args) |
| 550 | { |
| 551 | PyObject *str, *v; |
| 552 | const char *errors = NULL; |
| 553 | |
| 554 | if (!PyArg_ParseTuple(args, "O|z:utf_7_encode", |
| 555 | &str, &errors)) |
| 556 | return NULL; |
| 557 | |
| 558 | str = PyUnicode_FromObject(str); |
| 559 | if (str == NULL) |
| 560 | return NULL; |
| 561 | v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str), |
| 562 | PyUnicode_GET_SIZE(str), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 563 | 0, |
| 564 | 0, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 565 | errors), |
| 566 | PyUnicode_GET_SIZE(str)); |
| 567 | Py_DECREF(str); |
| 568 | return v; |
| 569 | } |
| 570 | |
| 571 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 572 | utf_8_encode(PyObject *self, |
| 573 | PyObject *args) |
| 574 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 575 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 576 | const char *errors = NULL; |
| 577 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 578 | if (!PyArg_ParseTuple(args, "O|z:utf_8_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 579 | &str, &errors)) |
| 580 | return NULL; |
| 581 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 582 | str = PyUnicode_FromObject(str); |
| 583 | if (str == NULL) |
| 584 | return NULL; |
| 585 | v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str), |
| 586 | PyUnicode_GET_SIZE(str), |
| 587 | errors), |
| 588 | PyUnicode_GET_SIZE(str)); |
| 589 | Py_DECREF(str); |
| 590 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* This version provides access to the byteorder parameter of the |
| 594 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 595 | 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] | 596 | BOM mark. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 597 | |
| 598 | */ |
| 599 | |
| 600 | static PyObject * |
| 601 | utf_16_encode(PyObject *self, |
| 602 | PyObject *args) |
| 603 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 604 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 605 | const char *errors = NULL; |
| 606 | int byteorder = 0; |
| 607 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 608 | if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 609 | &str, &errors, &byteorder)) |
| 610 | return NULL; |
| 611 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 612 | str = PyUnicode_FromObject(str); |
| 613 | if (str == NULL) |
| 614 | return NULL; |
| 615 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 616 | PyUnicode_GET_SIZE(str), |
| 617 | errors, |
| 618 | byteorder), |
| 619 | PyUnicode_GET_SIZE(str)); |
| 620 | Py_DECREF(str); |
| 621 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static PyObject * |
| 625 | utf_16_le_encode(PyObject *self, |
| 626 | PyObject *args) |
| 627 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 628 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 629 | const char *errors = NULL; |
| 630 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 631 | if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 632 | &str, &errors)) |
| 633 | return NULL; |
| 634 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 635 | str = PyUnicode_FromObject(str); |
| 636 | if (str == NULL) |
| 637 | return NULL; |
| 638 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 639 | PyUnicode_GET_SIZE(str), |
| 640 | errors, |
| 641 | -1), |
| 642 | PyUnicode_GET_SIZE(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 643 | Py_DECREF(str); |
| 644 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static PyObject * |
| 648 | utf_16_be_encode(PyObject *self, |
| 649 | PyObject *args) |
| 650 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 651 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 652 | const char *errors = NULL; |
| 653 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 654 | if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 655 | &str, &errors)) |
| 656 | return NULL; |
| 657 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 658 | str = PyUnicode_FromObject(str); |
| 659 | if (str == NULL) |
| 660 | return NULL; |
| 661 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 662 | PyUnicode_GET_SIZE(str), |
| 663 | errors, |
| 664 | +1), |
| 665 | PyUnicode_GET_SIZE(str)); |
| 666 | Py_DECREF(str); |
| 667 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static PyObject * |
| 671 | unicode_escape_encode(PyObject *self, |
| 672 | PyObject *args) |
| 673 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 674 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 675 | const char *errors = NULL; |
| 676 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 677 | if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 678 | &str, &errors)) |
| 679 | return NULL; |
| 680 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 681 | str = PyUnicode_FromObject(str); |
| 682 | if (str == NULL) |
| 683 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 684 | v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 685 | PyUnicode_GET_SIZE(str)), |
| 686 | PyUnicode_GET_SIZE(str)); |
| 687 | Py_DECREF(str); |
| 688 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static PyObject * |
| 692 | raw_unicode_escape_encode(PyObject *self, |
| 693 | PyObject *args) |
| 694 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 695 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 696 | const char *errors = NULL; |
| 697 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 698 | if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 699 | &str, &errors)) |
| 700 | return NULL; |
| 701 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 702 | str = PyUnicode_FromObject(str); |
| 703 | if (str == NULL) |
| 704 | return NULL; |
| 705 | v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 706 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 707 | PyUnicode_GET_SIZE(str)), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 708 | PyUnicode_GET_SIZE(str)); |
| 709 | Py_DECREF(str); |
| 710 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static PyObject * |
| 714 | latin_1_encode(PyObject *self, |
| 715 | PyObject *args) |
| 716 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 717 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 718 | const char *errors = NULL; |
| 719 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 720 | if (!PyArg_ParseTuple(args, "O|z:latin_1_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 721 | &str, &errors)) |
| 722 | return NULL; |
| 723 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 724 | str = PyUnicode_FromObject(str); |
| 725 | if (str == NULL) |
| 726 | return NULL; |
| 727 | v = codec_tuple(PyUnicode_EncodeLatin1( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 728 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 729 | PyUnicode_GET_SIZE(str), |
| 730 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 731 | PyUnicode_GET_SIZE(str)); |
| 732 | Py_DECREF(str); |
| 733 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static PyObject * |
| 737 | ascii_encode(PyObject *self, |
| 738 | PyObject *args) |
| 739 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 740 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 741 | const char *errors = NULL; |
| 742 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 743 | if (!PyArg_ParseTuple(args, "O|z:ascii_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 744 | &str, &errors)) |
| 745 | return NULL; |
| 746 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 747 | str = PyUnicode_FromObject(str); |
| 748 | if (str == NULL) |
| 749 | return NULL; |
| 750 | v = codec_tuple(PyUnicode_EncodeASCII( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 751 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 752 | PyUnicode_GET_SIZE(str), |
| 753 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 754 | PyUnicode_GET_SIZE(str)); |
| 755 | Py_DECREF(str); |
| 756 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static PyObject * |
| 760 | charmap_encode(PyObject *self, |
| 761 | PyObject *args) |
| 762 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 763 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 764 | const char *errors = NULL; |
| 765 | PyObject *mapping = NULL; |
| 766 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 767 | if (!PyArg_ParseTuple(args, "O|zO:charmap_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 768 | &str, &errors, &mapping)) |
| 769 | return NULL; |
| 770 | if (mapping == Py_None) |
| 771 | mapping = NULL; |
| 772 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 773 | str = PyUnicode_FromObject(str); |
| 774 | if (str == NULL) |
| 775 | return NULL; |
| 776 | v = codec_tuple(PyUnicode_EncodeCharmap( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 777 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 778 | PyUnicode_GET_SIZE(str), |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 779 | mapping, |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 780 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 781 | PyUnicode_GET_SIZE(str)); |
| 782 | Py_DECREF(str); |
| 783 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 786 | static PyObject* |
| 787 | charmap_build(PyObject *self, PyObject *args) |
| 788 | { |
| 789 | PyObject *map; |
| 790 | if (!PyArg_ParseTuple(args, "U:charmap_build", &map)) |
| 791 | return NULL; |
| 792 | return PyUnicode_BuildEncodingMap(map); |
| 793 | } |
| 794 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 795 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 796 | |
| 797 | static PyObject * |
| 798 | mbcs_encode(PyObject *self, |
| 799 | PyObject *args) |
| 800 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 801 | PyObject *str, *v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 802 | const char *errors = NULL; |
| 803 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 804 | if (!PyArg_ParseTuple(args, "O|z:mbcs_encode", |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 805 | &str, &errors)) |
| 806 | return NULL; |
| 807 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 808 | str = PyUnicode_FromObject(str); |
| 809 | if (str == NULL) |
| 810 | return NULL; |
| 811 | v = codec_tuple(PyUnicode_EncodeMBCS( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 812 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 813 | PyUnicode_GET_SIZE(str), |
| 814 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 815 | PyUnicode_GET_SIZE(str)); |
| 816 | Py_DECREF(str); |
| 817 | return v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 820 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 821 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 822 | /* --- Error handler registry --------------------------------------------- */ |
| 823 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 824 | PyDoc_STRVAR(register_error__doc__, |
| 825 | "register_error(errors, handler)\n\ |
| 826 | \n\ |
| 827 | Register the specified error handler under the name\n\ |
| 828 | errors. handler must be a callable object, that\n\ |
| 829 | will be called with an exception instance containing\n\ |
| 830 | information about the location of the encoding/decoding\n\ |
| 831 | error and must return a (replacement, new position) tuple."); |
| 832 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 833 | static PyObject *register_error(PyObject *self, PyObject *args) |
| 834 | { |
| 835 | const char *name; |
| 836 | PyObject *handler; |
| 837 | |
| 838 | if (!PyArg_ParseTuple(args, "sO:register_error", |
| 839 | &name, &handler)) |
| 840 | return NULL; |
| 841 | if (PyCodec_RegisterError(name, handler)) |
| 842 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 843 | Py_RETURN_NONE; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 846 | PyDoc_STRVAR(lookup_error__doc__, |
| 847 | "lookup_error(errors) -> handler\n\ |
| 848 | \n\ |
| 849 | Return the error handler for the specified error handling name\n\ |
| 850 | or raise a LookupError, if no handler exists under this name."); |
| 851 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 852 | static PyObject *lookup_error(PyObject *self, PyObject *args) |
| 853 | { |
| 854 | const char *name; |
| 855 | |
| 856 | if (!PyArg_ParseTuple(args, "s:lookup_error", |
| 857 | &name)) |
| 858 | return NULL; |
| 859 | return PyCodec_LookupError(name); |
| 860 | } |
| 861 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 862 | /* --- Module API --------------------------------------------------------- */ |
| 863 | |
| 864 | static PyMethodDef _codecs_functions[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 865 | {"register", codec_register, METH_O, |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 866 | register__doc__}, |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 867 | {"lookup", codec_lookup, METH_VARARGS, |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 868 | lookup__doc__}, |
Brett Cannon | 3e377de | 2004-07-10 21:41:14 +0000 | [diff] [blame] | 869 | {"encode", codec_encode, METH_VARARGS, |
| 870 | encode__doc__}, |
| 871 | {"decode", codec_decode, METH_VARARGS, |
| 872 | decode__doc__}, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 873 | {"escape_encode", escape_encode, METH_VARARGS}, |
| 874 | {"escape_decode", escape_decode, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 875 | {"utf_8_encode", utf_8_encode, METH_VARARGS}, |
| 876 | {"utf_8_decode", utf_8_decode, METH_VARARGS}, |
| 877 | {"utf_7_encode", utf_7_encode, METH_VARARGS}, |
| 878 | {"utf_7_decode", utf_7_decode, METH_VARARGS}, |
| 879 | {"utf_16_encode", utf_16_encode, METH_VARARGS}, |
| 880 | {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS}, |
| 881 | {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS}, |
| 882 | {"utf_16_decode", utf_16_decode, METH_VARARGS}, |
| 883 | {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS}, |
| 884 | {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS}, |
| 885 | {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS}, |
| 886 | {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS}, |
| 887 | {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS}, |
| 888 | {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS}, |
| 889 | {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS}, |
| 890 | {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS}, |
| 891 | {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS}, |
| 892 | {"latin_1_encode", latin_1_encode, METH_VARARGS}, |
| 893 | {"latin_1_decode", latin_1_decode, METH_VARARGS}, |
| 894 | {"ascii_encode", ascii_encode, METH_VARARGS}, |
| 895 | {"ascii_decode", ascii_decode, METH_VARARGS}, |
| 896 | {"charmap_encode", charmap_encode, METH_VARARGS}, |
| 897 | {"charmap_decode", charmap_decode, METH_VARARGS}, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 898 | {"charmap_build", charmap_build, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 899 | {"readbuffer_encode", readbuffer_encode, METH_VARARGS}, |
| 900 | {"charbuffer_encode", charbuffer_encode, METH_VARARGS}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 901 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 902 | {"mbcs_encode", mbcs_encode, METH_VARARGS}, |
| 903 | {"mbcs_decode", mbcs_decode, METH_VARARGS}, |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 904 | #endif |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 905 | {"register_error", register_error, METH_VARARGS, |
| 906 | register_error__doc__}, |
| 907 | {"lookup_error", lookup_error, METH_VARARGS, |
| 908 | lookup_error__doc__}, |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 909 | {NULL, NULL} /* sentinel */ |
| 910 | }; |
| 911 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 912 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 913 | init_codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 914 | { |
| 915 | Py_InitModule("_codecs", _codecs_functions); |
| 916 | } |