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