blob: aac44703adfa1cbafc25385a46bf5c9d083e5d01 [file] [log] [blame]
Guido van Rossume2d67f92000-03-10 23:09:23 +00001/* ------------------------------------------------------------------------
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 Rossum36e0a922007-07-20 04:05:57 +000013 lookup(encoding) -> CodecInfo object
Guido van Rossume2d67f92000-03-10 23:09:23 +000014
15 The builtin Unicode codecs use the following interface:
16
Walter Dörwald9fd115c2005-11-02 08:30:08 +000017 <encoding>_encode(Unicode_object[,errors='strict']) ->
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000018 (string object, bytes consumed)
Guido van Rossume2d67f92000-03-10 23:09:23 +000019
Walter Dörwald9fd115c2005-11-02 08:30:08 +000020 <encoding>_decode(char_buffer_obj[,errors='strict']) ->
Guido van Rossume2d67f92000-03-10 23:09:23 +000021 (Unicode object, bytes consumed)
22
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +000023 <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 Rossume2d67f92000-03-10 23:09:23 +000027 These <encoding>s are available: utf_8, unicode_escape,
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +000028 raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
29 mbcs (on win32).
30
Guido van Rossume2d67f92000-03-10 23:09:23 +000031
32Written by Marc-Andre Lemburg (mal@lemburg.com).
33
Guido van Rossum16b1ad92000-08-03 16:24:25 +000034Copyright (c) Corporation for National Research Initiatives.
Guido van Rossume2d67f92000-03-10 23:09:23 +000035
36 ------------------------------------------------------------------------ */
37
Martin v. Löwis18e16552006-02-15 17:27:45 +000038#define PY_SSIZE_T_CLEAN
Guido van Rossume2d67f92000-03-10 23:09:23 +000039#include "Python.h"
40
41/* --- Registry ----------------------------------------------------------- */
42
Walter Dörwald0ae29812002-10-31 13:36:29 +000043PyDoc_STRVAR(register__doc__,
44"register(search_function)\n\
45\n\
46Register a codec search function. Search functions are expected to take\n\
47one argument, the encoding name in all lower case letters, and return\n\
Guido van Rossum36e0a922007-07-20 04:05:57 +000048a tuple of functions (encoder, decoder, stream_reader, stream_writer)\n\
49(or a CodecInfo object).");
Walter Dörwald0ae29812002-10-31 13:36:29 +000050
Guido van Rossume2d67f92000-03-10 23:09:23 +000051static
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000052PyObject *codec_register(PyObject *self, PyObject *search_function)
Guido van Rossume2d67f92000-03-10 23:09:23 +000053{
Guido van Rossume2d67f92000-03-10 23:09:23 +000054 if (PyCodec_Register(search_function))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000055 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000056
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000057 Py_RETURN_NONE;
Guido van Rossume2d67f92000-03-10 23:09:23 +000058}
59
Walter Dörwald0ae29812002-10-31 13:36:29 +000060PyDoc_STRVAR(lookup__doc__,
Guido van Rossum36e0a922007-07-20 04:05:57 +000061"lookup(encoding) -> CodecInfo\n\
Walter Dörwald0ae29812002-10-31 13:36:29 +000062\n\
63Looks up a codec tuple in the Python codec registry and returns\n\
Benjamin Petersonf07d0022009-03-21 17:31:58 +000064a CodecInfo object.");
Walter Dörwald0ae29812002-10-31 13:36:29 +000065
Guido van Rossume2d67f92000-03-10 23:09:23 +000066static
Marc-André Lemburg3f419742004-07-10 12:06:10 +000067PyObject *codec_lookup(PyObject *self, PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +000068{
69 char *encoding;
70
71 if (!PyArg_ParseTuple(args, "s:lookup", &encoding))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000072 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +000073
74 return _PyCodec_Lookup(encoding);
Guido van Rossume2d67f92000-03-10 23:09:23 +000075}
76
Marc-André Lemburg3f419742004-07-10 12:06:10 +000077PyDoc_STRVAR(encode__doc__,
78"encode(obj, [encoding[,errors]]) -> object\n\
79\n\
80Encodes obj using the codec registered for encoding. encoding defaults\n\
81to the default encoding. errors may be given to set a different error\n\
82handling scheme. Default is 'strict' meaning that encoding errors raise\n\
83a ValueError. Other possible values are 'ignore', 'replace' and\n\
84'xmlcharrefreplace' as well as any other name registered with\n\
85codecs.register_error that can handle ValueErrors.");
86
87static PyObject *
88codec_encode(PyObject *self, PyObject *args)
89{
Brett Cannon3e377de2004-07-10 21:41:14 +000090 const char *encoding = NULL;
91 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +000092 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000093
Marc-André Lemburg3f419742004-07-10 12:06:10 +000094 if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))
95 return NULL;
96
97 if (encoding == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000098 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburg3f419742004-07-10 12:06:10 +000099
100 /* Encode via the codec registry */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000101 return PyCodec_Encode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000102}
103
104PyDoc_STRVAR(decode__doc__,
105"decode(obj, [encoding[,errors]]) -> object\n\
106\n\
107Decodes obj using the codec registered for encoding. encoding defaults\n\
108to the default encoding. errors may be given to set a different error\n\
109handling scheme. Default is 'strict' meaning that encoding errors raise\n\
110a ValueError. Other possible values are 'ignore' and 'replace'\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000111as well as any other name registered with codecs.register_error that is\n\
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000112able to handle ValueErrors.");
113
114static PyObject *
115codec_decode(PyObject *self, PyObject *args)
116{
Brett Cannon3e377de2004-07-10 21:41:14 +0000117 const char *encoding = NULL;
118 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000119 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000120
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000121 if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
122 return NULL;
123
124 if (encoding == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000126
127 /* Decode via the codec registry */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000128 return PyCodec_Decode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000129}
130
Guido van Rossume2d67f92000-03-10 23:09:23 +0000131/* --- Helpers ------------------------------------------------------------ */
132
133static
134PyObject *codec_tuple(PyObject *unicode,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 Py_ssize_t len)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000136{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000137 PyObject *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000138 if (unicode == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000139 return NULL;
140 v = Py_BuildValue("On", unicode, len);
141 Py_DECREF(unicode);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000142 return v;
143}
144
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000145/* --- String codecs ------------------------------------------------------ */
146static PyObject *
147escape_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000148 PyObject *args)
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000149{
150 const char *errors = NULL;
151 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000152 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000153
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000154 if (!PyArg_ParseTuple(args, "s#|z:escape_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000155 &data, &size, &errors))
156 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000157 return codec_tuple(PyBytes_DecodeEscape(data, size, errors, 0, NULL),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000158 size);
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000159}
160
161static PyObject *
162escape_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyObject *args)
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000164{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000165 static const char *hexdigits = "0123456789abcdef";
166 PyObject *str;
167 Py_ssize_t size;
168 Py_ssize_t newsize;
169 const char *errors = NULL;
170 PyObject *v;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000171
Antoine Pitroua57aae72010-06-09 16:58:35 +0000172 if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
173 &PyBytes_Type, &str, &errors))
174 return NULL;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000175
Antoine Pitroua57aae72010-06-09 16:58:35 +0000176 size = PyBytes_GET_SIZE(str);
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 }
183 v = PyBytes_FromStringAndSize(NULL, newsize);
184
185 if (v == NULL) {
186 return NULL;
187 }
188 else {
189 register Py_ssize_t i;
190 register char c;
191 register char *p = PyBytes_AS_STRING(v);
192
193 for (i = 0; i < size; i++) {
194 /* There's at least enough room for a hex escape */
195 assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
196 c = PyBytes_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;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 }
Antoine Pitroua57aae72010-06-09 16:58:35 +0000214 *p = '\0';
215 if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
216 return NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 }
Antoine Pitroua57aae72010-06-09 16:58:35 +0000218 }
Walter Dörwald1ab83302007-05-18 17:15:44 +0000219
Philip Jenveyddf0d032010-06-09 17:56:11 +0000220 return codec_tuple(v, size);
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000221}
222
Guido van Rossume2d67f92000-03-10 23:09:23 +0000223/* --- Decoder ------------------------------------------------------------ */
224
225static PyObject *
226unicode_internal_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000227 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000228{
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000229 PyObject *obj;
230 const char *errors = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000231 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000232 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000233
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000234 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 &obj, &errors))
236 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000237
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000238 if (PyUnicode_Check(obj)) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 Py_INCREF(obj);
240 return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000241 }
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000242 else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
244 return NULL;
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000246 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
247 size);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000248 }
Guido van Rossume2d67f92000-03-10 23:09:23 +0000249}
250
251static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000252utf_7_decode(PyObject *self,
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000253 PyObject *args)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000254{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000255 Py_buffer pbuf;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000256 const char *errors = NULL;
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000257 int final = 0;
258 Py_ssize_t consumed;
259 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000260
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000261 if (!PyArg_ParseTuple(args, "y*|zi:utf_7_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000262 &pbuf, &errors, &final))
263 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000264 consumed = pbuf.len;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000265
Martin v. Löwis423be952008-08-13 15:53:07 +0000266 decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000268 PyBuffer_Release(&pbuf);
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000269 if (decoded == NULL)
270 return NULL;
271 return codec_tuple(decoded, consumed);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000272}
273
274static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000275utf_8_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000277{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000278 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000279 const char *errors = NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000280 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000281 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000282 PyObject *decoded = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000283
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000284 if (!PyArg_ParseTuple(args, "y*|zi:utf_8_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 &pbuf, &errors, &final))
286 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000287 consumed = pbuf.len;
288
289 decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000290 final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000291 PyBuffer_Release(&pbuf);
Walter Dörwald69652032004-09-07 20:24:22 +0000292 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000293 return NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000294 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000295}
296
297static PyObject *
298utf_16_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000299 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000300{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000301 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000302 const char *errors = NULL;
303 int byteorder = 0;
Walter Dörwald69652032004-09-07 20:24:22 +0000304 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000306 PyObject *decoded;
307
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000308 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 &pbuf, &errors, &final))
310 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000311 consumed = pbuf.len; /* This is overwritten unless final is true. */
312 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000313 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000314 PyBuffer_Release(&pbuf);
Walter Dörwald69652032004-09-07 20:24:22 +0000315 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000316 return NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000317 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000318}
319
320static PyObject *
321utf_16_le_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000322 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000323{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000324 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000325 const char *errors = NULL;
326 int byteorder = -1;
Walter Dörwald69652032004-09-07 20:24:22 +0000327 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000329 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000330
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000331 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_le_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 &pbuf, &errors, &final))
333 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000334
Martin v. Löwis423be952008-08-13 15:53:07 +0000335 consumed = pbuf.len; /* This is overwritten unless final is true. */
336 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000338 PyBuffer_Release(&pbuf);
Walter Dörwald69652032004-09-07 20:24:22 +0000339 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 return NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000341 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000342}
343
344static PyObject *
345utf_16_be_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000346 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000347{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000348 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000349 const char *errors = NULL;
350 int byteorder = 1;
Walter Dörwald69652032004-09-07 20:24:22 +0000351 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000352 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000353 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000354
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000355 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_be_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 &pbuf, &errors, &final))
357 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000358
359 consumed = pbuf.len; /* This is overwritten unless final is true. */
360 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000361 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000362 PyBuffer_Release(&pbuf);
Walter Dörwald69652032004-09-07 20:24:22 +0000363 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000364 return NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000365 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000366}
367
368/* This non-standard version also provides access to the byteorder
369 parameter of the builtin UTF-16 codec.
370
371 It returns a tuple (unicode, bytesread, byteorder) with byteorder
372 being the value in effect at the end of data.
373
374*/
375
376static PyObject *
377utf_16_ex_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000378 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000379{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000380 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000381 const char *errors = NULL;
382 int byteorder = 0;
383 PyObject *unicode, *tuple;
Walter Dörwald69652032004-09-07 20:24:22 +0000384 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000385 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000386
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000387 if (!PyArg_ParseTuple(args, "y*|zii:utf_16_ex_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000388 &pbuf, &errors, &byteorder, &final))
389 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000390 consumed = pbuf.len; /* This is overwritten unless final is true. */
391 unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000392 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000393 PyBuffer_Release(&pbuf);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000394 if (unicode == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000395 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000396 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000397 Py_DECREF(unicode);
398 return tuple;
399}
400
401static PyObject *
Walter Dörwald41980ca2007-08-16 21:55:45 +0000402utf_32_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000403 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000404{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000405 Py_buffer pbuf;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000406 const char *errors = NULL;
407 int byteorder = 0;
408 int final = 0;
409 Py_ssize_t consumed;
410 PyObject *decoded;
411
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000412 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 &pbuf, &errors, &final))
414 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000415 consumed = pbuf.len; /* This is overwritten unless final is true. */
416 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000418 PyBuffer_Release(&pbuf);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000419 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000420 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000421 return codec_tuple(decoded, consumed);
422}
423
424static PyObject *
425utf_32_le_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000426 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000427{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000428 Py_buffer pbuf;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000429 const char *errors = NULL;
430 int byteorder = -1;
431 int final = 0;
432 Py_ssize_t consumed;
Martin v. Löwis423be952008-08-13 15:53:07 +0000433 PyObject *decoded;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000434
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000435 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_le_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 &pbuf, &errors, &final))
437 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000438 consumed = pbuf.len; /* This is overwritten unless final is true. */
439 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000440 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000441 PyBuffer_Release(&pbuf);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000442 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000443 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000444 return codec_tuple(decoded, consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000445}
446
447static PyObject *
448utf_32_be_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000449 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000450{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000451 Py_buffer pbuf;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000452 const char *errors = NULL;
453 int byteorder = 1;
454 int final = 0;
455 Py_ssize_t consumed;
Martin v. Löwis423be952008-08-13 15:53:07 +0000456 PyObject *decoded;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000457
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000458 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_be_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 &pbuf, &errors, &final))
460 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000461 consumed = pbuf.len; /* This is overwritten unless final is true. */
462 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000464 PyBuffer_Release(&pbuf);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000465 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000467 return codec_tuple(decoded, consumed);
468}
469
470/* This non-standard version also provides access to the byteorder
471 parameter of the builtin UTF-32 codec.
472
473 It returns a tuple (unicode, bytesread, byteorder) with byteorder
474 being the value in effect at the end of data.
475
476*/
477
478static PyObject *
479utf_32_ex_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000481{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000482 Py_buffer pbuf;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000483 const char *errors = NULL;
484 int byteorder = 0;
485 PyObject *unicode, *tuple;
486 int final = 0;
487 Py_ssize_t consumed;
488
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000489 if (!PyArg_ParseTuple(args, "y*|zii:utf_32_ex_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 &pbuf, &errors, &byteorder, &final))
491 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000492 consumed = pbuf.len; /* This is overwritten unless final is true. */
493 unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 &byteorder, final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000495 PyBuffer_Release(&pbuf);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000496 if (unicode == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000497 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000498 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
499 Py_DECREF(unicode);
500 return tuple;
501}
502
503static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000504unicode_escape_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000505 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000506{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000507 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000508 const char *errors = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000509 PyObject *unicode;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000510
Martin v. Löwis423be952008-08-13 15:53:07 +0000511 if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 &pbuf, &errors))
513 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000514
Antoine Pitroua57aae72010-06-09 16:58:35 +0000515 unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
516 PyBuffer_Release(&pbuf);
517 return codec_tuple(unicode, pbuf.len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000518}
519
520static PyObject *
521raw_unicode_escape_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000522 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000523{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000524 Py_buffer pbuf;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000525 const char *errors = NULL;
Antoine Pitroua57aae72010-06-09 16:58:35 +0000526 PyObject *unicode;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000527
Martin v. Löwis423be952008-08-13 15:53:07 +0000528 if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 &pbuf, &errors))
530 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000531
Antoine Pitroua57aae72010-06-09 16:58:35 +0000532 unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
533 PyBuffer_Release(&pbuf);
534 return codec_tuple(unicode, pbuf.len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000535}
536
537static PyObject *
538latin_1_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000539 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000540{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000541 Py_buffer pbuf;
542 PyObject *unicode;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000543 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000544
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000545 if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000546 &pbuf, &errors))
547 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000548
Antoine Pitroua57aae72010-06-09 16:58:35 +0000549 unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
550 PyBuffer_Release(&pbuf);
551 return codec_tuple(unicode, pbuf.len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000552}
553
554static PyObject *
555ascii_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000557{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000558 Py_buffer pbuf;
559 PyObject *unicode;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000560 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000561
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000562 if (!PyArg_ParseTuple(args, "y*|z:ascii_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 &pbuf, &errors))
564 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000565
Antoine Pitroua57aae72010-06-09 16:58:35 +0000566 unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
567 PyBuffer_Release(&pbuf);
568 return codec_tuple(unicode, pbuf.len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000569}
570
571static PyObject *
572charmap_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000574{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000575 Py_buffer pbuf;
576 PyObject *unicode;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000577 const char *errors = NULL;
578 PyObject *mapping = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000579
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000580 if (!PyArg_ParseTuple(args, "y*|zO:charmap_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000581 &pbuf, &errors, &mapping))
582 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000583 if (mapping == Py_None)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000584 mapping = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000585
Antoine Pitroua57aae72010-06-09 16:58:35 +0000586 unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
587 PyBuffer_Release(&pbuf);
588 return codec_tuple(unicode, pbuf.len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000589}
590
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000591#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000592
593static PyObject *
594mbcs_decode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 PyObject *args)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000596{
Antoine Pitroua57aae72010-06-09 16:58:35 +0000597 Py_buffer pbuf;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000598 const char *errors = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599 int final = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +0000600 Py_ssize_t consumed;
601 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000602
Antoine Pitrou81fabdb2009-01-22 10:11:36 +0000603 if (!PyArg_ParseTuple(args, "y*|zi:mbcs_decode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000604 &pbuf, &errors, &final))
605 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000606 consumed = pbuf.len;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000607
Martin v. Löwis423be952008-08-13 15:53:07 +0000608 decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 final ? NULL : &consumed);
Antoine Pitroua57aae72010-06-09 16:58:35 +0000610 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +0000611 if (decoded == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000612 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000613 return codec_tuple(decoded, consumed);
Guido van Rossum24bdb042000-03-28 20:29:59 +0000614}
615
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000616#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000617
Guido van Rossume2d67f92000-03-10 23:09:23 +0000618/* --- Encoder ------------------------------------------------------------ */
619
620static PyObject *
621readbuffer_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000623{
Martin v. Löwis423be952008-08-13 15:53:07 +0000624 Py_buffer pdata;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000625 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000626 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000627 const char *errors = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000628 PyObject *result;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000629
Martin v. Löwis423be952008-08-13 15:53:07 +0000630 if (!PyArg_ParseTuple(args, "s*|z:readbuffer_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 &pdata, &errors))
632 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000633 data = pdata.buf;
634 size = pdata.len;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000635
Martin v. Löwis423be952008-08-13 15:53:07 +0000636 result = PyBytes_FromStringAndSize(data, size);
637 PyBuffer_Release(&pdata);
638 return codec_tuple(result, size);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000639}
640
641static PyObject *
642charbuffer_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000644{
645 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000647 const char *errors = NULL;
648
649 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 &data, &size, &errors))
651 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000652
Christian Heimes72b710a2008-05-26 13:28:38 +0000653 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000654}
655
656static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000657unicode_internal_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000658 PyObject *args)
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000659{
660 PyObject *obj;
661 const char *errors = NULL;
662 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000663 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000664
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000665 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 &obj, &errors))
667 return NULL;
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000668
669 if (PyUnicode_Check(obj)) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 data = PyUnicode_AS_DATA(obj);
671 size = PyUnicode_GET_DATA_SIZE(obj);
672 return codec_tuple(PyBytes_FromStringAndSize(data, size),
673 PyUnicode_GET_SIZE(obj));
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000674 }
675 else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
677 return NULL;
678 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000679 }
680}
681
682static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000683utf_7_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000684 PyObject *args)
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000685{
686 PyObject *str, *v;
687 const char *errors = NULL;
688
689 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 &str, &errors))
691 return NULL;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000692
693 str = PyUnicode_FromObject(str);
694 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 return NULL;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000696 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000697 PyUnicode_GET_SIZE(str),
698 0,
699 0,
700 errors),
701 PyUnicode_GET_SIZE(str));
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000702 Py_DECREF(str);
703 return v;
704}
705
706static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000707utf_8_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000709{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000710 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000711 const char *errors = NULL;
712
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000713 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 &str, &errors))
715 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000716
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000717 str = PyUnicode_FromObject(str);
718 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000720 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000721 PyUnicode_GET_SIZE(str),
722 errors),
723 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000724 Py_DECREF(str);
725 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000726}
727
728/* This version provides access to the byteorder parameter of the
729 builtin UTF-16 codecs as optional third argument. It defaults to 0
730 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000731 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000732
733*/
734
735static PyObject *
736utf_16_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000737 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000738{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000739 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000740 const char *errors = NULL;
741 int byteorder = 0;
742
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000743 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000744 &str, &errors, &byteorder))
745 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000746
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000747 str = PyUnicode_FromObject(str);
748 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000749 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000750 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 PyUnicode_GET_SIZE(str),
752 errors,
753 byteorder),
754 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000755 Py_DECREF(str);
756 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000757}
758
759static PyObject *
760utf_16_le_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000762{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000763 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000764 const char *errors = NULL;
765
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000766 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000767 &str, &errors))
768 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000769
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000770 str = PyUnicode_FromObject(str);
771 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000772 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000773 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000774 PyUnicode_GET_SIZE(str),
775 errors,
776 -1),
777 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000778 Py_DECREF(str);
779 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000780}
781
782static PyObject *
783utf_16_be_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000785{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000786 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000787 const char *errors = NULL;
788
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000789 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 &str, &errors))
791 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000792
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000793 str = PyUnicode_FromObject(str);
794 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000795 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000796 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 PyUnicode_GET_SIZE(str),
798 errors,
799 +1),
800 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000801 Py_DECREF(str);
802 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000803}
804
Walter Dörwald41980ca2007-08-16 21:55:45 +0000805/* This version provides access to the byteorder parameter of the
806 builtin UTF-32 codecs as optional third argument. It defaults to 0
807 which means: use the native byte order and prepend the data with a
808 BOM mark.
809
810*/
811
812static PyObject *
813utf_32_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000815{
816 PyObject *str, *v;
817 const char *errors = NULL;
818 int byteorder = 0;
819
820 if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000821 &str, &errors, &byteorder))
822 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000823
824 str = PyUnicode_FromObject(str);
825 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000827 v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 PyUnicode_GET_SIZE(str),
829 errors,
830 byteorder),
831 PyUnicode_GET_SIZE(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000832 Py_DECREF(str);
833 return v;
834}
835
836static PyObject *
837utf_32_le_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000839{
840 PyObject *str, *v;
841 const char *errors = NULL;
842
843 if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000844 &str, &errors))
845 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000846
847 str = PyUnicode_FromObject(str);
848 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000849 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000850 v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000851 PyUnicode_GET_SIZE(str),
852 errors,
853 -1),
854 PyUnicode_GET_SIZE(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000855 Py_DECREF(str);
856 return v;
857}
858
859static PyObject *
860utf_32_be_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 PyObject *args)
Walter Dörwald41980ca2007-08-16 21:55:45 +0000862{
863 PyObject *str, *v;
864 const char *errors = NULL;
865
866 if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 &str, &errors))
868 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000869
870 str = PyUnicode_FromObject(str);
871 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000872 return NULL;
Walter Dörwald41980ca2007-08-16 21:55:45 +0000873 v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 PyUnicode_GET_SIZE(str),
875 errors,
876 +1),
877 PyUnicode_GET_SIZE(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000878 Py_DECREF(str);
879 return v;
880}
881
Guido van Rossume2d67f92000-03-10 23:09:23 +0000882static PyObject *
883unicode_escape_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000885{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000886 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000887 const char *errors = NULL;
888
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000889 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 &str, &errors))
891 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000892
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000893 str = PyUnicode_FromObject(str);
894 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000895 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000896 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 PyUnicode_GET_SIZE(str)),
898 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000899 Py_DECREF(str);
900 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000901}
902
903static PyObject *
904raw_unicode_escape_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000905 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000906{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000907 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000908 const char *errors = NULL;
909
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000910 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 &str, &errors))
912 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000913
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000914 str = PyUnicode_FromObject(str);
915 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000917 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000918 PyUnicode_AS_UNICODE(str),
919 PyUnicode_GET_SIZE(str)),
920 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000921 Py_DECREF(str);
922 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000923}
924
925static PyObject *
926latin_1_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000928{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000929 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000930 const char *errors = NULL;
931
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000932 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000933 &str, &errors))
934 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000935
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000936 str = PyUnicode_FromObject(str);
937 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000938 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000939 v = codec_tuple(PyUnicode_EncodeLatin1(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000940 PyUnicode_AS_UNICODE(str),
941 PyUnicode_GET_SIZE(str),
942 errors),
943 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000944 Py_DECREF(str);
945 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000946}
947
948static PyObject *
949ascii_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000950 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000951{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000952 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000953 const char *errors = NULL;
954
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000955 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000956 &str, &errors))
957 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000958
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000959 str = PyUnicode_FromObject(str);
960 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000961 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000962 v = codec_tuple(PyUnicode_EncodeASCII(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 PyUnicode_AS_UNICODE(str),
964 PyUnicode_GET_SIZE(str),
965 errors),
966 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000967 Py_DECREF(str);
968 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000969}
970
971static PyObject *
972charmap_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000973 PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000974{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000975 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000976 const char *errors = NULL;
977 PyObject *mapping = NULL;
978
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000979 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000980 &str, &errors, &mapping))
981 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000982 if (mapping == Py_None)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 mapping = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000984
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000985 str = PyUnicode_FromObject(str);
986 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000987 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000988 v = codec_tuple(PyUnicode_EncodeCharmap(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000989 PyUnicode_AS_UNICODE(str),
990 PyUnicode_GET_SIZE(str),
991 mapping,
992 errors),
993 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000994 Py_DECREF(str);
995 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000996}
997
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000998static PyObject*
999charmap_build(PyObject *self, PyObject *args)
1000{
1001 PyObject *map;
1002 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
1003 return NULL;
1004 return PyUnicode_BuildEncodingMap(map);
1005}
1006
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001007#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +00001008
1009static PyObject *
1010mbcs_encode(PyObject *self,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001011 PyObject *args)
Guido van Rossum24bdb042000-03-28 20:29:59 +00001012{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +00001013 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +00001014 const char *errors = NULL;
1015
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +00001016 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001017 &str, &errors))
1018 return NULL;
Guido van Rossum24bdb042000-03-28 20:29:59 +00001019
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +00001020 str = PyUnicode_FromObject(str);
1021 if (str == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001022 return NULL;
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +00001023 v = codec_tuple(PyUnicode_EncodeMBCS(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001024 PyUnicode_AS_UNICODE(str),
1025 PyUnicode_GET_SIZE(str),
1026 errors),
1027 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +00001028 Py_DECREF(str);
1029 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +00001030}
1031
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001032#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +00001033
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001034/* --- Error handler registry --------------------------------------------- */
1035
Walter Dörwald0ae29812002-10-31 13:36:29 +00001036PyDoc_STRVAR(register_error__doc__,
1037"register_error(errors, handler)\n\
1038\n\
1039Register the specified error handler under the name\n\
1040errors. handler must be a callable object, that\n\
1041will be called with an exception instance containing\n\
1042information about the location of the encoding/decoding\n\
1043error and must return a (replacement, new position) tuple.");
1044
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001045static PyObject *register_error(PyObject *self, PyObject *args)
1046{
1047 const char *name;
1048 PyObject *handler;
1049
1050 if (!PyArg_ParseTuple(args, "sO:register_error",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001051 &name, &handler))
1052 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001053 if (PyCodec_RegisterError(name, handler))
1054 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001055 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001056}
1057
Walter Dörwald0ae29812002-10-31 13:36:29 +00001058PyDoc_STRVAR(lookup_error__doc__,
1059"lookup_error(errors) -> handler\n\
1060\n\
1061Return the error handler for the specified error handling name\n\
1062or raise a LookupError, if no handler exists under this name.");
1063
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001064static PyObject *lookup_error(PyObject *self, PyObject *args)
1065{
1066 const char *name;
1067
1068 if (!PyArg_ParseTuple(args, "s:lookup_error",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 &name))
1070 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001071 return PyCodec_LookupError(name);
1072}
1073
Guido van Rossume2d67f92000-03-10 23:09:23 +00001074/* --- Module API --------------------------------------------------------- */
1075
1076static PyMethodDef _codecs_functions[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001077 {"register", codec_register, METH_O,
Walter Dörwald0ae29812002-10-31 13:36:29 +00001078 register__doc__},
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001079 {"lookup", codec_lookup, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +00001080 lookup__doc__},
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001081 {"encode", codec_encode, METH_VARARGS,
1082 encode__doc__},
1083 {"decode", codec_decode, METH_VARARGS,
1084 decode__doc__},
1085 {"escape_encode", escape_encode, METH_VARARGS},
1086 {"escape_decode", escape_decode, METH_VARARGS},
1087 {"utf_8_encode", utf_8_encode, METH_VARARGS},
1088 {"utf_8_decode", utf_8_decode, METH_VARARGS},
1089 {"utf_7_encode", utf_7_encode, METH_VARARGS},
1090 {"utf_7_decode", utf_7_decode, METH_VARARGS},
1091 {"utf_16_encode", utf_16_encode, METH_VARARGS},
1092 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
1093 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
1094 {"utf_16_decode", utf_16_decode, METH_VARARGS},
1095 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
1096 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
1097 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
1098 {"utf_32_encode", utf_32_encode, METH_VARARGS},
1099 {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS},
1100 {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS},
1101 {"utf_32_decode", utf_32_decode, METH_VARARGS},
1102 {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS},
1103 {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS},
1104 {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS},
1105 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
1106 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
1107 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
1108 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
1109 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
1110 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
1111 {"latin_1_encode", latin_1_encode, METH_VARARGS},
1112 {"latin_1_decode", latin_1_decode, METH_VARARGS},
1113 {"ascii_encode", ascii_encode, METH_VARARGS},
1114 {"ascii_decode", ascii_decode, METH_VARARGS},
1115 {"charmap_encode", charmap_encode, METH_VARARGS},
1116 {"charmap_decode", charmap_decode, METH_VARARGS},
1117 {"charmap_build", charmap_build, METH_VARARGS},
1118 {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
1119 {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001120#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001121 {"mbcs_encode", mbcs_encode, METH_VARARGS},
1122 {"mbcs_decode", mbcs_decode, METH_VARARGS},
Guido van Rossum24bdb042000-03-28 20:29:59 +00001123#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 {"register_error", register_error, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +00001125 register_error__doc__},
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 {"lookup_error", lookup_error, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +00001127 lookup_error__doc__},
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001128 {NULL, NULL} /* sentinel */
Guido van Rossume2d67f92000-03-10 23:09:23 +00001129};
1130
Martin v. Löwis1a214512008-06-11 05:26:20 +00001131static struct PyModuleDef codecsmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001132 PyModuleDef_HEAD_INIT,
1133 "_codecs",
1134 NULL,
1135 -1,
1136 _codecs_functions,
1137 NULL,
1138 NULL,
1139 NULL,
1140 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001141};
1142
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001143PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001144PyInit__codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +00001145{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 return PyModule_Create(&codecsmodule);
Guido van Rossume2d67f92000-03-10 23:09:23 +00001147}