blob: 119967154ca20c5f1317d5ab0149bc775de4cdfa [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
13 lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)
14
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']) ->
Guido van Rossume2d67f92000-03-10 23:09:23 +000018 (string object, bytes consumed)
19
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\
48a tuple of functions (encoder, decoder, stream_reader, stream_writer).");
49
Guido van Rossume2d67f92000-03-10 23:09:23 +000050static
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000051PyObject *codec_register(PyObject *self, PyObject *search_function)
Guido van Rossume2d67f92000-03-10 23:09:23 +000052{
Guido van Rossume2d67f92000-03-10 23:09:23 +000053 if (PyCodec_Register(search_function))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000054 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000055
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000056 Py_RETURN_NONE;
Guido van Rossume2d67f92000-03-10 23:09:23 +000057}
58
Walter Dörwald0ae29812002-10-31 13:36:29 +000059PyDoc_STRVAR(lookup__doc__,
60"lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)\n\
61\n\
62Looks up a codec tuple in the Python codec registry and returns\n\
63a tuple of functions.");
64
Guido van Rossume2d67f92000-03-10 23:09:23 +000065static
Marc-André Lemburg3f419742004-07-10 12:06:10 +000066PyObject *codec_lookup(PyObject *self, PyObject *args)
Guido van Rossume2d67f92000-03-10 23:09:23 +000067{
68 char *encoding;
69
70 if (!PyArg_ParseTuple(args, "s:lookup", &encoding))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000071 return NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +000072
73 return _PyCodec_Lookup(encoding);
Guido van Rossume2d67f92000-03-10 23:09:23 +000074}
75
Marc-André Lemburg3f419742004-07-10 12:06:10 +000076PyDoc_STRVAR(encode__doc__,
77"encode(obj, [encoding[,errors]]) -> object\n\
78\n\
79Encodes obj using the codec registered for encoding. encoding defaults\n\
80to the default encoding. errors may be given to set a different error\n\
81handling scheme. Default is 'strict' meaning that encoding errors raise\n\
82a ValueError. Other possible values are 'ignore', 'replace' and\n\
83'xmlcharrefreplace' as well as any other name registered with\n\
84codecs.register_error that can handle ValueErrors.");
85
86static PyObject *
87codec_encode(PyObject *self, PyObject *args)
88{
Brett Cannon3e377de2004-07-10 21:41:14 +000089 const char *encoding = NULL;
90 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +000091 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000092
Marc-André Lemburg3f419742004-07-10 12:06:10 +000093 if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))
94 return NULL;
95
96 if (encoding == NULL)
97 encoding = PyUnicode_GetDefaultEncoding();
98
99 /* Encode via the codec registry */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000100 return PyCodec_Encode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000101}
102
103PyDoc_STRVAR(decode__doc__,
104"decode(obj, [encoding[,errors]]) -> object\n\
105\n\
106Decodes obj using the codec registered for encoding. encoding defaults\n\
107to the default encoding. errors may be given to set a different error\n\
108handling scheme. Default is 'strict' meaning that encoding errors raise\n\
109a ValueError. Other possible values are 'ignore' and 'replace'\n\
110as well as any other name registerd with codecs.register_error that is\n\
111able to handle ValueErrors.");
112
113static PyObject *
114codec_decode(PyObject *self, PyObject *args)
115{
Brett Cannon3e377de2004-07-10 21:41:14 +0000116 const char *encoding = NULL;
117 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000118 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000119
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000120 if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
121 return NULL;
122
123 if (encoding == NULL)
124 encoding = PyUnicode_GetDefaultEncoding();
125
126 /* Decode via the codec registry */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000127 return PyCodec_Decode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000128}
129
Guido van Rossume2d67f92000-03-10 23:09:23 +0000130/* --- Helpers ------------------------------------------------------------ */
131
132static
133PyObject *codec_tuple(PyObject *unicode,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134 Py_ssize_t len)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000135{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000136 PyObject *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000137 if (unicode == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000138 return NULL;
139 v = Py_BuildValue("On", unicode, len);
140 Py_DECREF(unicode);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000141 return v;
142}
143
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000144/* --- String codecs ------------------------------------------------------ */
145static PyObject *
146escape_decode(PyObject *self,
147 PyObject *args)
148{
149 const char *errors = NULL;
150 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000151 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000152
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000153 if (!PyArg_ParseTuple(args, "s#|z:escape_decode",
154 &data, &size, &errors))
155 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000156 return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL),
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000157 size);
158}
159
160static PyObject *
161escape_encode(PyObject *self,
162 PyObject *args)
163{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000164 static const char *hexdigits = "0123456789abcdef";
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000165 PyObject *str;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000166 Py_ssize_t size;
167 Py_ssize_t newsize;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000168 const char *errors = NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000169 PyObject *v;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000170
171 if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
172 &PyString_Type, &str, &errors))
173 return NULL;
174
Martin v. Löwis5b222132007-06-10 09:51:05 +0000175 size = PyString_GET_SIZE(str);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000176 newsize = 4*size;
177 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
178 PyErr_SetString(PyExc_OverflowError,
179 "string is too large to encode");
180 return NULL;
181 }
182 v = PyBytes_FromStringAndSize(NULL, newsize);
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000183
Walter Dörwald1ab83302007-05-18 17:15:44 +0000184 if (v == NULL) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000185 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000186 }
187 else {
188 register Py_ssize_t i;
189 register char c;
190 register char *p = PyBytes_AS_STRING(v);
191
192 for (i = 0; i < size; i++) {
193 /* There's at least enough room for a hex escape */
194 assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
195 c = PyString_AS_STRING(str)[i];
196 if (c == '\'' || c == '\\')
197 *p++ = '\\', *p++ = c;
198 else if (c == '\t')
199 *p++ = '\\', *p++ = 't';
200 else if (c == '\n')
201 *p++ = '\\', *p++ = 'n';
202 else if (c == '\r')
203 *p++ = '\\', *p++ = 'r';
204 else if (c < ' ' || c >= 0x7f) {
205 *p++ = '\\';
206 *p++ = 'x';
207 *p++ = hexdigits[(c & 0xf0) >> 4];
208 *p++ = hexdigits[c & 0xf];
209 }
210 else
211 *p++ = c;
212 }
213 *p = '\0';
214 if (PyBytes_Resize(v, (p - PyBytes_AS_STRING(v)))) {
215 Py_DECREF(v);
216 return NULL;
217 }
218 }
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000219
Walter Dörwald1ab83302007-05-18 17:15:44 +0000220 return codec_tuple(v, PyBytes_Size(v));
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,
227 PyObject *args)
228{
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",
235 &obj, &errors))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000236 return NULL;
237
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000238 if (PyUnicode_Check(obj)) {
239 Py_INCREF(obj);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000240 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 {
243 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
244 return NULL;
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000245
246 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000247 size);
248 }
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,
253 PyObject *args)
254{
255 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256 Py_ssize_t size;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000257 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000258
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000259 if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode",
260 &data, &size, &errors))
261 return NULL;
262
263 return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors),
264 size);
265}
266
267static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000268utf_8_decode(PyObject *self,
269 PyObject *args)
270{
271 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000273 const char *errors = NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000274 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000275 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000276 PyObject *decoded = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000277
Walter Dörwald69652032004-09-07 20:24:22 +0000278 if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode",
279 &data, &size, &errors, &final))
280 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000281 if (size < 0) {
282 PyErr_SetString(PyExc_ValueError, "negative argument");
283 return 0;
284 }
Walter Dörwald69652032004-09-07 20:24:22 +0000285 consumed = size;
286
287 decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors,
288 final ? NULL : &consumed);
289 if (decoded == NULL)
290 return NULL;
291 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000292}
293
294static PyObject *
295utf_16_decode(PyObject *self,
296 PyObject *args)
297{
298 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000300 const char *errors = NULL;
301 int byteorder = 0;
Walter Dörwald69652032004-09-07 20:24:22 +0000302 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000303 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000304 PyObject *decoded;
305
306 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode",
307 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000308 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309 if (size < 0) {
310 PyErr_SetString(PyExc_ValueError, "negative argument");
311 return 0;
312 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000314 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
315 final ? NULL : &consumed);
316 if (decoded == NULL)
317 return NULL;
318 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000319}
320
321static PyObject *
322utf_16_le_decode(PyObject *self,
323 PyObject *args)
324{
325 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000326 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000327 const char *errors = NULL;
328 int byteorder = -1;
Walter Dörwald69652032004-09-07 20:24:22 +0000329 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000331 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000332
Walter Dörwald69652032004-09-07 20:24:22 +0000333 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode",
334 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000335 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000336
Martin v. Löwis18e16552006-02-15 17:27:45 +0000337 if (size < 0) {
338 PyErr_SetString(PyExc_ValueError, "negative argument");
339 return 0;
340 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000342 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
343 &byteorder, final ? NULL : &consumed);
344 if (decoded == NULL)
345 return NULL;
346 return codec_tuple(decoded, consumed);
347
Guido van Rossume2d67f92000-03-10 23:09:23 +0000348}
349
350static PyObject *
351utf_16_be_decode(PyObject *self,
352 PyObject *args)
353{
354 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000356 const char *errors = NULL;
357 int byteorder = 1;
Walter Dörwald69652032004-09-07 20:24:22 +0000358 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000359 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000360 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000361
Walter Dörwald69652032004-09-07 20:24:22 +0000362 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode",
363 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000364 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000365 if (size < 0) {
366 PyErr_SetString(PyExc_ValueError, "negative argument");
367 return 0;
368 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000370 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
371 &byteorder, final ? NULL : &consumed);
372 if (decoded == NULL)
373 return NULL;
374 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000375}
376
377/* This non-standard version also provides access to the byteorder
378 parameter of the builtin UTF-16 codec.
379
380 It returns a tuple (unicode, bytesread, byteorder) with byteorder
381 being the value in effect at the end of data.
382
383*/
384
385static PyObject *
386utf_16_ex_decode(PyObject *self,
387 PyObject *args)
388{
389 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000391 const char *errors = NULL;
392 int byteorder = 0;
393 PyObject *unicode, *tuple;
Walter Dörwald69652032004-09-07 20:24:22 +0000394 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000396
397 if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode",
398 &data, &size, &errors, &byteorder, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000399 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000400 if (size < 0) {
401 PyErr_SetString(PyExc_ValueError, "negative argument");
402 return 0;
403 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000405 unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
406 final ? NULL : &consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000407 if (unicode == NULL)
408 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000409 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000410 Py_DECREF(unicode);
411 return tuple;
412}
413
414static PyObject *
415unicode_escape_decode(PyObject *self,
416 PyObject *args)
417{
418 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000419 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000420 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000421
Guido van Rossume2d67f92000-03-10 23:09:23 +0000422 if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
423 &data, &size, &errors))
424 return NULL;
425
426 return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
427 size);
428}
429
430static PyObject *
431raw_unicode_escape_decode(PyObject *self,
432 PyObject *args)
433{
434 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000435 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000436 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000437
Guido van Rossume2d67f92000-03-10 23:09:23 +0000438 if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
439 &data, &size, &errors))
440 return NULL;
441
442 return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
443 size);
444}
445
446static PyObject *
447latin_1_decode(PyObject *self,
448 PyObject *args)
449{
450 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000452 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000453
Guido van Rossume2d67f92000-03-10 23:09:23 +0000454 if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
455 &data, &size, &errors))
456 return NULL;
457
458 return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
459 size);
460}
461
462static PyObject *
463ascii_decode(PyObject *self,
464 PyObject *args)
465{
466 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000468 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000469
Guido van Rossume2d67f92000-03-10 23:09:23 +0000470 if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
471 &data, &size, &errors))
472 return NULL;
473
474 return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
475 size);
476}
477
478static PyObject *
479charmap_decode(PyObject *self,
480 PyObject *args)
481{
482 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000483 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000484 const char *errors = NULL;
485 PyObject *mapping = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000486
Guido van Rossume2d67f92000-03-10 23:09:23 +0000487 if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
488 &data, &size, &errors, &mapping))
489 return NULL;
490 if (mapping == Py_None)
491 mapping = NULL;
492
493 return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
494 size);
495}
496
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000497#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000498
499static PyObject *
500mbcs_decode(PyObject *self,
501 PyObject *args)
502{
503 const char *data;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 Py_ssize_t size, consumed;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000505 const char *errors = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506 int final = 0;
507 PyObject *decoded;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000508
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000509 if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode",
510 &data, &size, &errors, &final))
Guido van Rossum24bdb042000-03-28 20:29:59 +0000511 return NULL;
512
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 decoded = PyUnicode_DecodeMBCSStateful(
514 data, size, errors, final ? NULL : &consumed);
515 if (!decoded)
516 return NULL;
517 return codec_tuple(decoded, final ? size : consumed);
Guido van Rossum24bdb042000-03-28 20:29:59 +0000518}
519
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000520#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000521
Guido van Rossume2d67f92000-03-10 23:09:23 +0000522/* --- Encoder ------------------------------------------------------------ */
523
524static PyObject *
525readbuffer_encode(PyObject *self,
526 PyObject *args)
527{
528 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000530 const char *errors = NULL;
531
532 if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
533 &data, &size, &errors))
534 return NULL;
535
Walter Dörwald2233d272007-06-22 12:17:08 +0000536 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000537}
538
539static PyObject *
540charbuffer_encode(PyObject *self,
541 PyObject *args)
542{
543 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000544 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000545 const char *errors = NULL;
546
547 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
548 &data, &size, &errors))
549 return NULL;
550
Walter Dörwald2233d272007-06-22 12:17:08 +0000551 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000552}
553
554static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000555unicode_internal_encode(PyObject *self,
556 PyObject *args)
557{
558 PyObject *obj;
559 const char *errors = NULL;
560 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000561 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000562
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000563 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
564 &obj, &errors))
565 return NULL;
566
567 if (PyUnicode_Check(obj)) {
568 data = PyUnicode_AS_DATA(obj);
569 size = PyUnicode_GET_DATA_SIZE(obj);
Walter Dörwald2233d272007-06-22 12:17:08 +0000570 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000571 }
572 else {
573 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
574 return NULL;
Walter Dörwald2233d272007-06-22 12:17:08 +0000575 return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000576 }
577}
578
579static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000580utf_7_encode(PyObject *self,
581 PyObject *args)
582{
583 PyObject *str, *v;
584 const char *errors = NULL;
585
586 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
587 &str, &errors))
588 return NULL;
589
590 str = PyUnicode_FromObject(str);
591 if (str == NULL)
592 return NULL;
593 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
594 PyUnicode_GET_SIZE(str),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000595 0,
596 0,
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000597 errors),
598 PyUnicode_GET_SIZE(str));
599 Py_DECREF(str);
600 return v;
601}
602
603static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000604utf_8_encode(PyObject *self,
605 PyObject *args)
606{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000607 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000608 const char *errors = NULL;
609
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000610 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000611 &str, &errors))
612 return NULL;
613
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000614 str = PyUnicode_FromObject(str);
615 if (str == NULL)
616 return NULL;
617 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
618 PyUnicode_GET_SIZE(str),
619 errors),
620 PyUnicode_GET_SIZE(str));
621 Py_DECREF(str);
622 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000623}
624
625/* This version provides access to the byteorder parameter of the
626 builtin UTF-16 codecs as optional third argument. It defaults to 0
627 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000628 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000629
630*/
631
632static PyObject *
633utf_16_encode(PyObject *self,
634 PyObject *args)
635{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000636 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000637 const char *errors = NULL;
638 int byteorder = 0;
639
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000640 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000641 &str, &errors, &byteorder))
642 return NULL;
643
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000644 str = PyUnicode_FromObject(str);
645 if (str == NULL)
646 return NULL;
647 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
648 PyUnicode_GET_SIZE(str),
649 errors,
650 byteorder),
651 PyUnicode_GET_SIZE(str));
652 Py_DECREF(str);
653 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000654}
655
656static PyObject *
657utf_16_le_encode(PyObject *self,
658 PyObject *args)
659{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000660 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000661 const char *errors = NULL;
662
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000663 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000664 &str, &errors))
665 return NULL;
666
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000667 str = PyUnicode_FromObject(str);
668 if (str == NULL)
669 return NULL;
670 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000671 PyUnicode_GET_SIZE(str),
672 errors,
673 -1),
674 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000675 Py_DECREF(str);
676 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000677}
678
679static PyObject *
680utf_16_be_encode(PyObject *self,
681 PyObject *args)
682{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000683 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000684 const char *errors = NULL;
685
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000686 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000687 &str, &errors))
688 return NULL;
689
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000690 str = PyUnicode_FromObject(str);
691 if (str == NULL)
692 return NULL;
693 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
694 PyUnicode_GET_SIZE(str),
695 errors,
696 +1),
697 PyUnicode_GET_SIZE(str));
698 Py_DECREF(str);
699 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000700}
701
702static PyObject *
703unicode_escape_encode(PyObject *self,
704 PyObject *args)
705{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000706 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000707 const char *errors = NULL;
708
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000709 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000710 &str, &errors))
711 return NULL;
712
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000713 str = PyUnicode_FromObject(str);
714 if (str == NULL)
715 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000716 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000717 PyUnicode_GET_SIZE(str)),
718 PyUnicode_GET_SIZE(str));
719 Py_DECREF(str);
720 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000721}
722
723static PyObject *
724raw_unicode_escape_encode(PyObject *self,
725 PyObject *args)
726{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000727 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000728 const char *errors = NULL;
729
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000730 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000731 &str, &errors))
732 return NULL;
733
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000734 str = PyUnicode_FromObject(str);
735 if (str == NULL)
736 return NULL;
737 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000738 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000739 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000740 PyUnicode_GET_SIZE(str));
741 Py_DECREF(str);
742 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000743}
744
745static PyObject *
746latin_1_encode(PyObject *self,
747 PyObject *args)
748{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000749 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000750 const char *errors = NULL;
751
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000752 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000753 &str, &errors))
754 return NULL;
755
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000756 str = PyUnicode_FromObject(str);
757 if (str == NULL)
758 return NULL;
759 v = codec_tuple(PyUnicode_EncodeLatin1(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000760 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000761 PyUnicode_GET_SIZE(str),
762 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000763 PyUnicode_GET_SIZE(str));
764 Py_DECREF(str);
765 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000766}
767
768static PyObject *
769ascii_encode(PyObject *self,
770 PyObject *args)
771{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000772 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000773 const char *errors = NULL;
774
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000775 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000776 &str, &errors))
777 return NULL;
778
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000779 str = PyUnicode_FromObject(str);
780 if (str == NULL)
781 return NULL;
782 v = codec_tuple(PyUnicode_EncodeASCII(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000783 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000784 PyUnicode_GET_SIZE(str),
785 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000786 PyUnicode_GET_SIZE(str));
787 Py_DECREF(str);
788 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000789}
790
791static PyObject *
792charmap_encode(PyObject *self,
793 PyObject *args)
794{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000795 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000796 const char *errors = NULL;
797 PyObject *mapping = NULL;
798
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000799 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000800 &str, &errors, &mapping))
801 return NULL;
802 if (mapping == Py_None)
803 mapping = NULL;
804
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000805 str = PyUnicode_FromObject(str);
806 if (str == NULL)
807 return NULL;
808 v = codec_tuple(PyUnicode_EncodeCharmap(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000809 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000810 PyUnicode_GET_SIZE(str),
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000811 mapping,
Guido van Rossume2d67f92000-03-10 23:09:23 +0000812 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000813 PyUnicode_GET_SIZE(str));
814 Py_DECREF(str);
815 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000816}
817
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000818static PyObject*
819charmap_build(PyObject *self, PyObject *args)
820{
821 PyObject *map;
822 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
823 return NULL;
824 return PyUnicode_BuildEncodingMap(map);
825}
826
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000827#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000828
829static PyObject *
830mbcs_encode(PyObject *self,
831 PyObject *args)
832{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000833 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000834 const char *errors = NULL;
835
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000836 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000837 &str, &errors))
838 return NULL;
839
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000840 str = PyUnicode_FromObject(str);
841 if (str == NULL)
842 return NULL;
843 v = codec_tuple(PyUnicode_EncodeMBCS(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000844 PyUnicode_AS_UNICODE(str),
Guido van Rossum24bdb042000-03-28 20:29:59 +0000845 PyUnicode_GET_SIZE(str),
846 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000847 PyUnicode_GET_SIZE(str));
848 Py_DECREF(str);
849 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000850}
851
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000852#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000853
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000854/* --- Error handler registry --------------------------------------------- */
855
Walter Dörwald0ae29812002-10-31 13:36:29 +0000856PyDoc_STRVAR(register_error__doc__,
857"register_error(errors, handler)\n\
858\n\
859Register the specified error handler under the name\n\
860errors. handler must be a callable object, that\n\
861will be called with an exception instance containing\n\
862information about the location of the encoding/decoding\n\
863error and must return a (replacement, new position) tuple.");
864
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000865static PyObject *register_error(PyObject *self, PyObject *args)
866{
867 const char *name;
868 PyObject *handler;
869
870 if (!PyArg_ParseTuple(args, "sO:register_error",
871 &name, &handler))
872 return NULL;
873 if (PyCodec_RegisterError(name, handler))
874 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000876}
877
Walter Dörwald0ae29812002-10-31 13:36:29 +0000878PyDoc_STRVAR(lookup_error__doc__,
879"lookup_error(errors) -> handler\n\
880\n\
881Return the error handler for the specified error handling name\n\
882or raise a LookupError, if no handler exists under this name.");
883
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000884static PyObject *lookup_error(PyObject *self, PyObject *args)
885{
886 const char *name;
887
888 if (!PyArg_ParseTuple(args, "s:lookup_error",
889 &name))
890 return NULL;
891 return PyCodec_LookupError(name);
892}
893
Guido van Rossume2d67f92000-03-10 23:09:23 +0000894/* --- Module API --------------------------------------------------------- */
895
896static PyMethodDef _codecs_functions[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000897 {"register", codec_register, METH_O,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000898 register__doc__},
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000899 {"lookup", codec_lookup, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000900 lookup__doc__},
Brett Cannon3e377de2004-07-10 21:41:14 +0000901 {"encode", codec_encode, METH_VARARGS,
902 encode__doc__},
903 {"decode", codec_decode, METH_VARARGS,
904 decode__doc__},
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000905 {"escape_encode", escape_encode, METH_VARARGS},
906 {"escape_decode", escape_decode, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000907 {"utf_8_encode", utf_8_encode, METH_VARARGS},
908 {"utf_8_decode", utf_8_decode, METH_VARARGS},
909 {"utf_7_encode", utf_7_encode, METH_VARARGS},
910 {"utf_7_decode", utf_7_decode, METH_VARARGS},
911 {"utf_16_encode", utf_16_encode, METH_VARARGS},
912 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
913 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
914 {"utf_16_decode", utf_16_decode, METH_VARARGS},
915 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
916 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
917 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
918 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
919 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
920 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
921 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
922 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
923 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
924 {"latin_1_encode", latin_1_encode, METH_VARARGS},
925 {"latin_1_decode", latin_1_decode, METH_VARARGS},
926 {"ascii_encode", ascii_encode, METH_VARARGS},
927 {"ascii_decode", ascii_decode, METH_VARARGS},
928 {"charmap_encode", charmap_encode, METH_VARARGS},
929 {"charmap_decode", charmap_decode, METH_VARARGS},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000930 {"charmap_build", charmap_build, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000931 {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
932 {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000933#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000934 {"mbcs_encode", mbcs_encode, METH_VARARGS},
935 {"mbcs_decode", mbcs_decode, METH_VARARGS},
Guido van Rossum24bdb042000-03-28 20:29:59 +0000936#endif
Walter Dörwald0ae29812002-10-31 13:36:29 +0000937 {"register_error", register_error, METH_VARARGS,
938 register_error__doc__},
939 {"lookup_error", lookup_error, METH_VARARGS,
940 lookup_error__doc__},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000941 {NULL, NULL} /* sentinel */
942};
943
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000944PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000945init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000946{
947 Py_InitModule("_codecs", _codecs_functions);
948}