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; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 157 | return codec_tuple(PyBytes_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", |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 173 | &PyBytes_Type, &str, &errors)) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 176 | size = PyBytes_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 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 183 | v = PyBytes_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; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 191 | register char *p = PyBytes_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 */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 195 | assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); |
| 196 | c = PyBytes_AS_STRING(str)[i]; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 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'; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 215 | if (_PyBytes_Resize(&v, (p - PyBytes_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 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 220 | return codec_tuple(v, PyBytes_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, |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 253 | PyObject *args) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 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; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 258 | int final = 0; |
| 259 | Py_ssize_t consumed; |
| 260 | PyObject *decoded = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 261 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 262 | if (!PyArg_ParseTuple(args, "t#|zi:utf_7_decode", |
| 263 | &data, &size, &errors, &final)) |
| 264 | return NULL; |
| 265 | consumed = size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 266 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 267 | decoded = PyUnicode_DecodeUTF7Stateful(data, size, errors, |
| 268 | final ? NULL : &consumed); |
| 269 | if (decoded == NULL) |
| 270 | return NULL; |
| 271 | return codec_tuple(decoded, consumed); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 275 | utf_8_decode(PyObject *self, |
| 276 | PyObject *args) |
| 277 | { |
| 278 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 279 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 280 | const char *errors = NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 281 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 282 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 283 | PyObject *decoded = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 284 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 285 | if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode", |
| 286 | &data, &size, &errors, &final)) |
| 287 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 288 | if (size < 0) { |
| 289 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 290 | return 0; |
| 291 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 292 | consumed = size; |
| 293 | |
| 294 | decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors, |
| 295 | final ? NULL : &consumed); |
| 296 | if (decoded == NULL) |
| 297 | return NULL; |
| 298 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static PyObject * |
| 302 | utf_16_decode(PyObject *self, |
| 303 | PyObject *args) |
| 304 | { |
| 305 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 306 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 307 | const char *errors = NULL; |
| 308 | int byteorder = 0; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 309 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 310 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 311 | PyObject *decoded; |
| 312 | |
| 313 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode", |
| 314 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 315 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 316 | if (size < 0) { |
| 317 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 318 | return 0; |
| 319 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 320 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 321 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, |
| 322 | final ? NULL : &consumed); |
| 323 | if (decoded == NULL) |
| 324 | return NULL; |
| 325 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static PyObject * |
| 329 | utf_16_le_decode(PyObject *self, |
| 330 | PyObject *args) |
| 331 | { |
| 332 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 333 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 334 | const char *errors = NULL; |
| 335 | int byteorder = -1; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 336 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 337 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 338 | PyObject *decoded = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 339 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 340 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode", |
| 341 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 342 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 343 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 344 | if (size < 0) { |
| 345 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 346 | return 0; |
| 347 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 348 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 349 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, |
| 350 | &byteorder, final ? NULL : &consumed); |
| 351 | if (decoded == NULL) |
| 352 | return NULL; |
| 353 | return codec_tuple(decoded, consumed); |
| 354 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | utf_16_be_decode(PyObject *self, |
| 359 | PyObject *args) |
| 360 | { |
| 361 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 362 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 363 | const char *errors = NULL; |
| 364 | int byteorder = 1; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 365 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 366 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 367 | PyObject *decoded = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 368 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 369 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode", |
| 370 | &data, &size, &errors, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 371 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 372 | if (size < 0) { |
| 373 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 374 | return 0; |
| 375 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 376 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 377 | decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, |
| 378 | &byteorder, final ? NULL : &consumed); |
| 379 | if (decoded == NULL) |
| 380 | return NULL; |
| 381 | return codec_tuple(decoded, consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* This non-standard version also provides access to the byteorder |
| 385 | parameter of the builtin UTF-16 codec. |
| 386 | |
| 387 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 388 | being the value in effect at the end of data. |
| 389 | |
| 390 | */ |
| 391 | |
| 392 | static PyObject * |
| 393 | utf_16_ex_decode(PyObject *self, |
| 394 | PyObject *args) |
| 395 | { |
| 396 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 397 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 398 | const char *errors = NULL; |
| 399 | int byteorder = 0; |
| 400 | PyObject *unicode, *tuple; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 401 | int final = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 402 | Py_ssize_t consumed; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 403 | |
| 404 | if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode", |
| 405 | &data, &size, &errors, &byteorder, &final)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 406 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 407 | if (size < 0) { |
| 408 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 409 | return 0; |
| 410 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | consumed = size; /* This is overwritten unless final is true. */ |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 412 | unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, |
| 413 | final ? NULL : &consumed); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 414 | if (unicode == NULL) |
| 415 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 416 | tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 417 | Py_DECREF(unicode); |
| 418 | return tuple; |
| 419 | } |
| 420 | |
| 421 | static PyObject * |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 422 | utf_32_decode(PyObject *self, |
| 423 | PyObject *args) |
| 424 | { |
| 425 | const char *data; |
| 426 | Py_ssize_t size; |
| 427 | const char *errors = NULL; |
| 428 | int byteorder = 0; |
| 429 | int final = 0; |
| 430 | Py_ssize_t consumed; |
| 431 | PyObject *decoded; |
| 432 | |
| 433 | if (!PyArg_ParseTuple(args, "t#|zi:utf_32_decode", |
| 434 | &data, &size, &errors, &final)) |
| 435 | return NULL; |
| 436 | if (size < 0) { |
| 437 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 438 | return 0; |
| 439 | } |
| 440 | consumed = size; /* This is overwritten unless final is true. */ |
| 441 | decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder, |
| 442 | final ? NULL : &consumed); |
| 443 | if (decoded == NULL) |
| 444 | return NULL; |
| 445 | return codec_tuple(decoded, consumed); |
| 446 | } |
| 447 | |
| 448 | static PyObject * |
| 449 | utf_32_le_decode(PyObject *self, |
| 450 | PyObject *args) |
| 451 | { |
| 452 | const char *data; |
| 453 | Py_ssize_t size; |
| 454 | const char *errors = NULL; |
| 455 | int byteorder = -1; |
| 456 | int final = 0; |
| 457 | Py_ssize_t consumed; |
| 458 | PyObject *decoded = NULL; |
| 459 | |
| 460 | if (!PyArg_ParseTuple(args, "t#|zi:utf_32_le_decode", |
| 461 | &data, &size, &errors, &final)) |
| 462 | return NULL; |
| 463 | |
| 464 | if (size < 0) { |
| 465 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 466 | return 0; |
| 467 | } |
| 468 | consumed = size; /* This is overwritten unless final is true. */ |
| 469 | decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors, |
| 470 | &byteorder, final ? NULL : &consumed); |
| 471 | if (decoded == NULL) |
| 472 | return NULL; |
| 473 | return codec_tuple(decoded, consumed); |
| 474 | |
| 475 | } |
| 476 | |
| 477 | static PyObject * |
| 478 | utf_32_be_decode(PyObject *self, |
| 479 | PyObject *args) |
| 480 | { |
| 481 | const char *data; |
| 482 | Py_ssize_t size; |
| 483 | const char *errors = NULL; |
| 484 | int byteorder = 1; |
| 485 | int final = 0; |
| 486 | Py_ssize_t consumed; |
| 487 | PyObject *decoded = NULL; |
| 488 | |
| 489 | if (!PyArg_ParseTuple(args, "t#|zi:utf_32_be_decode", |
| 490 | &data, &size, &errors, &final)) |
| 491 | return NULL; |
| 492 | if (size < 0) { |
| 493 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 494 | return 0; |
| 495 | } |
| 496 | consumed = size; /* This is overwritten unless final is true. */ |
| 497 | decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors, |
| 498 | &byteorder, final ? NULL : &consumed); |
| 499 | if (decoded == NULL) |
| 500 | return NULL; |
| 501 | return codec_tuple(decoded, consumed); |
| 502 | } |
| 503 | |
| 504 | /* This non-standard version also provides access to the byteorder |
| 505 | parameter of the builtin UTF-32 codec. |
| 506 | |
| 507 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 508 | being the value in effect at the end of data. |
| 509 | |
| 510 | */ |
| 511 | |
| 512 | static PyObject * |
| 513 | utf_32_ex_decode(PyObject *self, |
| 514 | PyObject *args) |
| 515 | { |
| 516 | const char *data; |
| 517 | Py_ssize_t size; |
| 518 | const char *errors = NULL; |
| 519 | int byteorder = 0; |
| 520 | PyObject *unicode, *tuple; |
| 521 | int final = 0; |
| 522 | Py_ssize_t consumed; |
| 523 | |
| 524 | if (!PyArg_ParseTuple(args, "t#|zii:utf_32_ex_decode", |
| 525 | &data, &size, &errors, &byteorder, &final)) |
| 526 | return NULL; |
| 527 | if (size < 0) { |
| 528 | PyErr_SetString(PyExc_ValueError, "negative argument"); |
| 529 | return 0; |
| 530 | } |
| 531 | consumed = size; /* This is overwritten unless final is true. */ |
| 532 | unicode = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder, |
| 533 | final ? NULL : &consumed); |
| 534 | if (unicode == NULL) |
| 535 | return NULL; |
| 536 | tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); |
| 537 | Py_DECREF(unicode); |
| 538 | return tuple; |
| 539 | } |
| 540 | |
| 541 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 542 | unicode_escape_decode(PyObject *self, |
| 543 | PyObject *args) |
| 544 | { |
| 545 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 546 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 547 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 549 | if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", |
| 550 | &data, &size, &errors)) |
| 551 | return NULL; |
| 552 | |
| 553 | return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), |
| 554 | size); |
| 555 | } |
| 556 | |
| 557 | static PyObject * |
| 558 | raw_unicode_escape_decode(PyObject *self, |
| 559 | PyObject *args) |
| 560 | { |
| 561 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 562 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 563 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 565 | if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode", |
| 566 | &data, &size, &errors)) |
| 567 | return NULL; |
| 568 | |
| 569 | return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), |
| 570 | size); |
| 571 | } |
| 572 | |
| 573 | static PyObject * |
| 574 | latin_1_decode(PyObject *self, |
| 575 | PyObject *args) |
| 576 | { |
| 577 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 578 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 579 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 581 | if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode", |
| 582 | &data, &size, &errors)) |
| 583 | return NULL; |
| 584 | |
| 585 | return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), |
| 586 | size); |
| 587 | } |
| 588 | |
| 589 | static PyObject * |
| 590 | ascii_decode(PyObject *self, |
| 591 | PyObject *args) |
| 592 | { |
| 593 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 594 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 595 | const char *errors = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 597 | if (!PyArg_ParseTuple(args, "t#|z:ascii_decode", |
| 598 | &data, &size, &errors)) |
| 599 | return NULL; |
| 600 | |
| 601 | return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), |
| 602 | size); |
| 603 | } |
| 604 | |
| 605 | static PyObject * |
| 606 | charmap_decode(PyObject *self, |
| 607 | PyObject *args) |
| 608 | { |
| 609 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 610 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 611 | const char *errors = NULL; |
| 612 | PyObject *mapping = NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 614 | if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", |
| 615 | &data, &size, &errors, &mapping)) |
| 616 | return NULL; |
| 617 | if (mapping == Py_None) |
| 618 | mapping = NULL; |
| 619 | |
| 620 | return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), |
| 621 | size); |
| 622 | } |
| 623 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 624 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 625 | |
| 626 | static PyObject * |
| 627 | mbcs_decode(PyObject *self, |
| 628 | PyObject *args) |
| 629 | { |
| 630 | const char *data; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 631 | Py_ssize_t size, consumed; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 632 | const char *errors = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | int final = 0; |
| 634 | PyObject *decoded; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 635 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 636 | if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode", |
| 637 | &data, &size, &errors, &final)) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 638 | return NULL; |
| 639 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 640 | decoded = PyUnicode_DecodeMBCSStateful( |
| 641 | data, size, errors, final ? NULL : &consumed); |
| 642 | if (!decoded) |
| 643 | return NULL; |
| 644 | return codec_tuple(decoded, final ? size : consumed); |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 647 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 648 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 649 | /* --- Encoder ------------------------------------------------------------ */ |
| 650 | |
| 651 | static PyObject * |
| 652 | readbuffer_encode(PyObject *self, |
| 653 | PyObject *args) |
| 654 | { |
| 655 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 656 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 657 | const char *errors = NULL; |
| 658 | |
| 659 | if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode", |
| 660 | &data, &size, &errors)) |
| 661 | return NULL; |
| 662 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 663 | return codec_tuple(PyBytes_FromStringAndSize(data, size), size); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static PyObject * |
| 667 | charbuffer_encode(PyObject *self, |
| 668 | PyObject *args) |
| 669 | { |
| 670 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 671 | Py_ssize_t size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 672 | const char *errors = NULL; |
| 673 | |
| 674 | if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode", |
| 675 | &data, &size, &errors)) |
| 676 | return NULL; |
| 677 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 678 | return codec_tuple(PyBytes_FromStringAndSize(data, size), size); |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static PyObject * |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 682 | unicode_internal_encode(PyObject *self, |
| 683 | PyObject *args) |
| 684 | { |
| 685 | PyObject *obj; |
| 686 | const char *errors = NULL; |
| 687 | const char *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 688 | Py_ssize_t size; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 689 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 690 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode", |
| 691 | &obj, &errors)) |
| 692 | return NULL; |
| 693 | |
| 694 | if (PyUnicode_Check(obj)) { |
| 695 | data = PyUnicode_AS_DATA(obj); |
| 696 | size = PyUnicode_GET_DATA_SIZE(obj); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 697 | return codec_tuple(PyBytes_FromStringAndSize(data, size), size); |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 698 | } |
| 699 | else { |
| 700 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 701 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 702 | return codec_tuple(PyBytes_FromStringAndSize(data, size), size); |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | static PyObject * |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 707 | utf_7_encode(PyObject *self, |
| 708 | PyObject *args) |
| 709 | { |
| 710 | PyObject *str, *v; |
| 711 | const char *errors = NULL; |
| 712 | |
| 713 | if (!PyArg_ParseTuple(args, "O|z:utf_7_encode", |
| 714 | &str, &errors)) |
| 715 | return NULL; |
| 716 | |
| 717 | str = PyUnicode_FromObject(str); |
| 718 | if (str == NULL) |
| 719 | return NULL; |
| 720 | v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str), |
| 721 | PyUnicode_GET_SIZE(str), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 722 | 0, |
| 723 | 0, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 724 | errors), |
| 725 | PyUnicode_GET_SIZE(str)); |
| 726 | Py_DECREF(str); |
| 727 | return v; |
| 728 | } |
| 729 | |
| 730 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 731 | utf_8_encode(PyObject *self, |
| 732 | PyObject *args) |
| 733 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 734 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 735 | const char *errors = NULL; |
| 736 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 737 | if (!PyArg_ParseTuple(args, "O|z:utf_8_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 738 | &str, &errors)) |
| 739 | return NULL; |
| 740 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 741 | str = PyUnicode_FromObject(str); |
| 742 | if (str == NULL) |
| 743 | return NULL; |
| 744 | v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str), |
| 745 | PyUnicode_GET_SIZE(str), |
| 746 | errors), |
| 747 | PyUnicode_GET_SIZE(str)); |
| 748 | Py_DECREF(str); |
| 749 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* This version provides access to the byteorder parameter of the |
| 753 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 754 | 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] | 755 | BOM mark. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 756 | |
| 757 | */ |
| 758 | |
| 759 | static PyObject * |
| 760 | utf_16_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 | int byteorder = 0; |
| 766 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 767 | if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 768 | &str, &errors, &byteorder)) |
| 769 | return NULL; |
| 770 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 771 | str = PyUnicode_FromObject(str); |
| 772 | if (str == NULL) |
| 773 | return NULL; |
| 774 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 775 | PyUnicode_GET_SIZE(str), |
| 776 | errors, |
| 777 | byteorder), |
| 778 | PyUnicode_GET_SIZE(str)); |
| 779 | Py_DECREF(str); |
| 780 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static PyObject * |
| 784 | utf_16_le_encode(PyObject *self, |
| 785 | PyObject *args) |
| 786 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 787 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 788 | const char *errors = NULL; |
| 789 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 790 | if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 791 | &str, &errors)) |
| 792 | return NULL; |
| 793 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 794 | str = PyUnicode_FromObject(str); |
| 795 | if (str == NULL) |
| 796 | return NULL; |
| 797 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 798 | PyUnicode_GET_SIZE(str), |
| 799 | errors, |
| 800 | -1), |
| 801 | PyUnicode_GET_SIZE(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 802 | Py_DECREF(str); |
| 803 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static PyObject * |
| 807 | utf_16_be_encode(PyObject *self, |
| 808 | PyObject *args) |
| 809 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 810 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 811 | const char *errors = NULL; |
| 812 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 813 | if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 814 | &str, &errors)) |
| 815 | return NULL; |
| 816 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 817 | str = PyUnicode_FromObject(str); |
| 818 | if (str == NULL) |
| 819 | return NULL; |
| 820 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 821 | PyUnicode_GET_SIZE(str), |
| 822 | errors, |
| 823 | +1), |
| 824 | PyUnicode_GET_SIZE(str)); |
| 825 | Py_DECREF(str); |
| 826 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 829 | /* This version provides access to the byteorder parameter of the |
| 830 | builtin UTF-32 codecs as optional third argument. It defaults to 0 |
| 831 | which means: use the native byte order and prepend the data with a |
| 832 | BOM mark. |
| 833 | |
| 834 | */ |
| 835 | |
| 836 | static PyObject * |
| 837 | utf_32_encode(PyObject *self, |
| 838 | PyObject *args) |
| 839 | { |
| 840 | PyObject *str, *v; |
| 841 | const char *errors = NULL; |
| 842 | int byteorder = 0; |
| 843 | |
| 844 | if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode", |
| 845 | &str, &errors, &byteorder)) |
| 846 | return NULL; |
| 847 | |
| 848 | str = PyUnicode_FromObject(str); |
| 849 | if (str == NULL) |
| 850 | return NULL; |
| 851 | v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), |
| 852 | PyUnicode_GET_SIZE(str), |
| 853 | errors, |
| 854 | byteorder), |
| 855 | PyUnicode_GET_SIZE(str)); |
| 856 | Py_DECREF(str); |
| 857 | return v; |
| 858 | } |
| 859 | |
| 860 | static PyObject * |
| 861 | utf_32_le_encode(PyObject *self, |
| 862 | PyObject *args) |
| 863 | { |
| 864 | PyObject *str, *v; |
| 865 | const char *errors = NULL; |
| 866 | |
| 867 | if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode", |
| 868 | &str, &errors)) |
| 869 | return NULL; |
| 870 | |
| 871 | str = PyUnicode_FromObject(str); |
| 872 | if (str == NULL) |
| 873 | return NULL; |
| 874 | v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), |
| 875 | PyUnicode_GET_SIZE(str), |
| 876 | errors, |
| 877 | -1), |
| 878 | PyUnicode_GET_SIZE(str)); |
| 879 | Py_DECREF(str); |
| 880 | return v; |
| 881 | } |
| 882 | |
| 883 | static PyObject * |
| 884 | utf_32_be_encode(PyObject *self, |
| 885 | PyObject *args) |
| 886 | { |
| 887 | PyObject *str, *v; |
| 888 | const char *errors = NULL; |
| 889 | |
| 890 | if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode", |
| 891 | &str, &errors)) |
| 892 | return NULL; |
| 893 | |
| 894 | str = PyUnicode_FromObject(str); |
| 895 | if (str == NULL) |
| 896 | return NULL; |
| 897 | v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str), |
| 898 | PyUnicode_GET_SIZE(str), |
| 899 | errors, |
| 900 | +1), |
| 901 | PyUnicode_GET_SIZE(str)); |
| 902 | Py_DECREF(str); |
| 903 | return v; |
| 904 | } |
| 905 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 906 | static PyObject * |
| 907 | unicode_escape_encode(PyObject *self, |
| 908 | PyObject *args) |
| 909 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 910 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 911 | const char *errors = NULL; |
| 912 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 913 | if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 914 | &str, &errors)) |
| 915 | return NULL; |
| 916 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 917 | str = PyUnicode_FromObject(str); |
| 918 | if (str == NULL) |
| 919 | return NULL; |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 920 | v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 921 | PyUnicode_GET_SIZE(str)), |
| 922 | PyUnicode_GET_SIZE(str)); |
| 923 | Py_DECREF(str); |
| 924 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static PyObject * |
| 928 | raw_unicode_escape_encode(PyObject *self, |
| 929 | PyObject *args) |
| 930 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 931 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 932 | const char *errors = NULL; |
| 933 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 934 | if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 935 | &str, &errors)) |
| 936 | return NULL; |
| 937 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 938 | str = PyUnicode_FromObject(str); |
| 939 | if (str == NULL) |
| 940 | return NULL; |
| 941 | v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 942 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 943 | PyUnicode_GET_SIZE(str)), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 944 | PyUnicode_GET_SIZE(str)); |
| 945 | Py_DECREF(str); |
| 946 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static PyObject * |
| 950 | latin_1_encode(PyObject *self, |
| 951 | PyObject *args) |
| 952 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 953 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 954 | const char *errors = NULL; |
| 955 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 956 | if (!PyArg_ParseTuple(args, "O|z:latin_1_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 957 | &str, &errors)) |
| 958 | return NULL; |
| 959 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 960 | str = PyUnicode_FromObject(str); |
| 961 | if (str == NULL) |
| 962 | return NULL; |
| 963 | v = codec_tuple(PyUnicode_EncodeLatin1( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 964 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 965 | PyUnicode_GET_SIZE(str), |
| 966 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 967 | PyUnicode_GET_SIZE(str)); |
| 968 | Py_DECREF(str); |
| 969 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static PyObject * |
| 973 | ascii_encode(PyObject *self, |
| 974 | PyObject *args) |
| 975 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 976 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 977 | const char *errors = NULL; |
| 978 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 979 | if (!PyArg_ParseTuple(args, "O|z:ascii_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 980 | &str, &errors)) |
| 981 | return NULL; |
| 982 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 983 | str = PyUnicode_FromObject(str); |
| 984 | if (str == NULL) |
| 985 | return NULL; |
| 986 | v = codec_tuple(PyUnicode_EncodeASCII( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 987 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 988 | PyUnicode_GET_SIZE(str), |
| 989 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 990 | PyUnicode_GET_SIZE(str)); |
| 991 | Py_DECREF(str); |
| 992 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static PyObject * |
| 996 | charmap_encode(PyObject *self, |
| 997 | PyObject *args) |
| 998 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 999 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1000 | const char *errors = NULL; |
| 1001 | PyObject *mapping = NULL; |
| 1002 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1003 | if (!PyArg_ParseTuple(args, "O|zO:charmap_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1004 | &str, &errors, &mapping)) |
| 1005 | return NULL; |
| 1006 | if (mapping == Py_None) |
| 1007 | mapping = NULL; |
| 1008 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1009 | str = PyUnicode_FromObject(str); |
| 1010 | if (str == NULL) |
| 1011 | return NULL; |
| 1012 | v = codec_tuple(PyUnicode_EncodeCharmap( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 1013 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1014 | PyUnicode_GET_SIZE(str), |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 1015 | mapping, |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1016 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1017 | PyUnicode_GET_SIZE(str)); |
| 1018 | Py_DECREF(str); |
| 1019 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1022 | static PyObject* |
| 1023 | charmap_build(PyObject *self, PyObject *args) |
| 1024 | { |
| 1025 | PyObject *map; |
| 1026 | if (!PyArg_ParseTuple(args, "U:charmap_build", &map)) |
| 1027 | return NULL; |
| 1028 | return PyUnicode_BuildEncodingMap(map); |
| 1029 | } |
| 1030 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1031 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1032 | |
| 1033 | static PyObject * |
| 1034 | mbcs_encode(PyObject *self, |
| 1035 | PyObject *args) |
| 1036 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1037 | PyObject *str, *v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1038 | const char *errors = NULL; |
| 1039 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1040 | if (!PyArg_ParseTuple(args, "O|z:mbcs_encode", |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1041 | &str, &errors)) |
| 1042 | return NULL; |
| 1043 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1044 | str = PyUnicode_FromObject(str); |
| 1045 | if (str == NULL) |
| 1046 | return NULL; |
| 1047 | v = codec_tuple(PyUnicode_EncodeMBCS( |
Walter Dörwald | 9fd115c | 2005-11-02 08:30:08 +0000 | [diff] [blame] | 1048 | PyUnicode_AS_UNICODE(str), |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1049 | PyUnicode_GET_SIZE(str), |
| 1050 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 1051 | PyUnicode_GET_SIZE(str)); |
| 1052 | Py_DECREF(str); |
| 1053 | return v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1056 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1057 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1058 | /* --- Error handler registry --------------------------------------------- */ |
| 1059 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1060 | PyDoc_STRVAR(register_error__doc__, |
| 1061 | "register_error(errors, handler)\n\ |
| 1062 | \n\ |
| 1063 | Register the specified error handler under the name\n\ |
| 1064 | errors. handler must be a callable object, that\n\ |
| 1065 | will be called with an exception instance containing\n\ |
| 1066 | information about the location of the encoding/decoding\n\ |
| 1067 | error and must return a (replacement, new position) tuple."); |
| 1068 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1069 | static PyObject *register_error(PyObject *self, PyObject *args) |
| 1070 | { |
| 1071 | const char *name; |
| 1072 | PyObject *handler; |
| 1073 | |
| 1074 | if (!PyArg_ParseTuple(args, "sO:register_error", |
| 1075 | &name, &handler)) |
| 1076 | return NULL; |
| 1077 | if (PyCodec_RegisterError(name, handler)) |
| 1078 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1079 | Py_RETURN_NONE; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1082 | PyDoc_STRVAR(lookup_error__doc__, |
| 1083 | "lookup_error(errors) -> handler\n\ |
| 1084 | \n\ |
| 1085 | Return the error handler for the specified error handling name\n\ |
| 1086 | or raise a LookupError, if no handler exists under this name."); |
| 1087 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1088 | static PyObject *lookup_error(PyObject *self, PyObject *args) |
| 1089 | { |
| 1090 | const char *name; |
| 1091 | |
| 1092 | if (!PyArg_ParseTuple(args, "s:lookup_error", |
| 1093 | &name)) |
| 1094 | return NULL; |
| 1095 | return PyCodec_LookupError(name); |
| 1096 | } |
| 1097 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1098 | /* --- Module API --------------------------------------------------------- */ |
| 1099 | |
| 1100 | static PyMethodDef _codecs_functions[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1101 | {"register", codec_register, METH_O, |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1102 | register__doc__}, |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1103 | {"lookup", codec_lookup, METH_VARARGS, |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1104 | lookup__doc__}, |
Brett Cannon | 3e377de | 2004-07-10 21:41:14 +0000 | [diff] [blame] | 1105 | {"encode", codec_encode, METH_VARARGS, |
| 1106 | encode__doc__}, |
| 1107 | {"decode", codec_decode, METH_VARARGS, |
| 1108 | decode__doc__}, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1109 | {"escape_encode", escape_encode, METH_VARARGS}, |
| 1110 | {"escape_decode", escape_decode, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 1111 | {"utf_8_encode", utf_8_encode, METH_VARARGS}, |
| 1112 | {"utf_8_decode", utf_8_decode, METH_VARARGS}, |
| 1113 | {"utf_7_encode", utf_7_encode, METH_VARARGS}, |
| 1114 | {"utf_7_decode", utf_7_decode, METH_VARARGS}, |
| 1115 | {"utf_16_encode", utf_16_encode, METH_VARARGS}, |
| 1116 | {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS}, |
| 1117 | {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS}, |
| 1118 | {"utf_16_decode", utf_16_decode, METH_VARARGS}, |
| 1119 | {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS}, |
| 1120 | {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS}, |
| 1121 | {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS}, |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 1122 | {"utf_32_encode", utf_32_encode, METH_VARARGS}, |
| 1123 | {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS}, |
| 1124 | {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS}, |
| 1125 | {"utf_32_decode", utf_32_decode, METH_VARARGS}, |
| 1126 | {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS}, |
| 1127 | {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS}, |
| 1128 | {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 1129 | {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS}, |
| 1130 | {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS}, |
| 1131 | {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS}, |
| 1132 | {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS}, |
| 1133 | {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS}, |
| 1134 | {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS}, |
| 1135 | {"latin_1_encode", latin_1_encode, METH_VARARGS}, |
| 1136 | {"latin_1_decode", latin_1_decode, METH_VARARGS}, |
| 1137 | {"ascii_encode", ascii_encode, METH_VARARGS}, |
| 1138 | {"ascii_decode", ascii_decode, METH_VARARGS}, |
| 1139 | {"charmap_encode", charmap_encode, METH_VARARGS}, |
| 1140 | {"charmap_decode", charmap_decode, METH_VARARGS}, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1141 | {"charmap_build", charmap_build, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 1142 | {"readbuffer_encode", readbuffer_encode, METH_VARARGS}, |
| 1143 | {"charbuffer_encode", charbuffer_encode, METH_VARARGS}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1144 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 1145 | {"mbcs_encode", mbcs_encode, METH_VARARGS}, |
| 1146 | {"mbcs_decode", mbcs_decode, METH_VARARGS}, |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1147 | #endif |
Walter Dörwald | 0ae2981 | 2002-10-31 13:36:29 +0000 | [diff] [blame] | 1148 | {"register_error", register_error, METH_VARARGS, |
| 1149 | register_error__doc__}, |
| 1150 | {"lookup_error", lookup_error, METH_VARARGS, |
| 1151 | lookup_error__doc__}, |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1152 | {NULL, NULL} /* sentinel */ |
| 1153 | }; |
| 1154 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1155 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1156 | init_codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1157 | { |
| 1158 | Py_InitModule("_codecs", _codecs_functions); |
| 1159 | } |