blob: 655277c58e511c325d832f1c494e2494adbb1348 [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
Walter Dörwald219336a2007-07-19 13:04:38 +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']) ->
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\
Walter Dörwald219336a2007-07-19 13:04:38 +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
Georg Brandl96a8c392006-05-29 21:04:52 +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))
Georg Brandl96a8c392006-05-29 21:04:52 +000055 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000056
Georg Brandl96a8c392006-05-29 21:04:52 +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__,
Walter Dörwald219336a2007-07-19 13:04:38 +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\
Walter Dörwald219336a2007-07-19 13:04:38 +000064a tuple of function (or a 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))
Georg Brandl96a8c392006-05-29 21:04:52 +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
Martin v. Löwise2713be2005-03-08 15:03:08 +000097#ifdef Py_USING_UNICODE
Marc-André Lemburg3f419742004-07-10 12:06:10 +000098 if (encoding == NULL)
99 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwise2713be2005-03-08 15:03:08 +0000100#else
101 if (encoding == NULL) {
102 PyErr_SetString(PyExc_ValueError, "no encoding specified");
103 return NULL;
104 }
105#endif
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000106
107 /* Encode via the codec registry */
Georg Brandl96a8c392006-05-29 21:04:52 +0000108 return PyCodec_Encode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000109}
110
111PyDoc_STRVAR(decode__doc__,
112"decode(obj, [encoding[,errors]]) -> object\n\
113\n\
114Decodes obj using the codec registered for encoding. encoding defaults\n\
115to the default encoding. errors may be given to set a different error\n\
116handling scheme. Default is 'strict' meaning that encoding errors raise\n\
117a ValueError. Other possible values are 'ignore' and 'replace'\n\
118as well as any other name registerd with codecs.register_error that is\n\
119able to handle ValueErrors.");
120
121static PyObject *
122codec_decode(PyObject *self, PyObject *args)
123{
Brett Cannon3e377de2004-07-10 21:41:14 +0000124 const char *encoding = NULL;
125 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000126 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000127
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000128 if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
129 return NULL;
130
Martin v. Löwise2713be2005-03-08 15:03:08 +0000131#ifdef Py_USING_UNICODE
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000132 if (encoding == NULL)
133 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwise2713be2005-03-08 15:03:08 +0000134#else
135 if (encoding == NULL) {
136 PyErr_SetString(PyExc_ValueError, "no encoding specified");
137 return NULL;
138 }
139#endif
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000140
141 /* Decode via the codec registry */
Georg Brandl96a8c392006-05-29 21:04:52 +0000142 return PyCodec_Decode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000143}
144
Guido van Rossume2d67f92000-03-10 23:09:23 +0000145/* --- Helpers ------------------------------------------------------------ */
146
147static
148PyObject *codec_tuple(PyObject *unicode,
Martin v. Löwis66851282006-04-22 11:40:03 +0000149 Py_ssize_t len)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000150{
Georg Brandl96a8c392006-05-29 21:04:52 +0000151 PyObject *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000152 if (unicode == NULL)
Georg Brandl96a8c392006-05-29 21:04:52 +0000153 return NULL;
154 v = Py_BuildValue("On", unicode, len);
155 Py_DECREF(unicode);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000156 return v;
157}
158
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000159/* --- String codecs ------------------------------------------------------ */
160static PyObject *
161escape_decode(PyObject *self,
162 PyObject *args)
163{
164 const char *errors = NULL;
165 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000166 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000167
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000168 if (!PyArg_ParseTuple(args, "s#|z:escape_decode",
169 &data, &size, &errors))
170 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000171 return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL),
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000172 size);
173}
174
175static PyObject *
176escape_encode(PyObject *self,
177 PyObject *args)
178{
179 PyObject *str;
180 const char *errors = NULL;
181 char *buf;
Martin v. Löwis66851282006-04-22 11:40:03 +0000182 Py_ssize_t len;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000183
184 if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
185 &PyString_Type, &str, &errors))
186 return NULL;
187
188 str = PyString_Repr(str, 0);
189 if (!str)
190 return NULL;
191
192 /* The string will be quoted. Unquote, similar to unicode-escape. */
193 buf = PyString_AS_STRING (str);
194 len = PyString_GET_SIZE (str);
195 memmove(buf, buf+1, len-2);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000196 if (_PyString_Resize(&str, len-2) < 0)
197 return NULL;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000198
199 return codec_tuple(str, PyString_Size(str));
200}
201
202#ifdef Py_USING_UNICODE
Guido van Rossume2d67f92000-03-10 23:09:23 +0000203/* --- Decoder ------------------------------------------------------------ */
204
205static PyObject *
206unicode_internal_decode(PyObject *self,
207 PyObject *args)
208{
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000209 PyObject *obj;
210 const char *errors = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000211 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000213
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000214 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
215 &obj, &errors))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000216 return NULL;
217
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000218 if (PyUnicode_Check(obj)) {
219 Py_INCREF(obj);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000220 return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000221 }
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000222 else {
223 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
224 return NULL;
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000225
226 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000227 size);
228 }
Guido van Rossume2d67f92000-03-10 23:09:23 +0000229}
230
231static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000232utf_7_decode(PyObject *self,
233 PyObject *args)
234{
235 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000236 Py_ssize_t size;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000237 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000238
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000239 if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode",
240 &data, &size, &errors))
241 return NULL;
242
243 return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors),
244 size);
245}
246
247static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000248utf_8_decode(PyObject *self,
249 PyObject *args)
250{
251 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000252 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000253 const char *errors = NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000254 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000256 PyObject *decoded = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000257
Walter Dörwald69652032004-09-07 20:24:22 +0000258 if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode",
259 &data, &size, &errors, &final))
260 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000261 if (size < 0) {
262 PyErr_SetString(PyExc_ValueError, "negative argument");
263 return 0;
264 }
Walter Dörwald69652032004-09-07 20:24:22 +0000265 consumed = size;
266
267 decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors,
268 final ? NULL : &consumed);
269 if (decoded == NULL)
270 return NULL;
271 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000272}
273
274static PyObject *
275utf_16_decode(PyObject *self,
276 PyObject *args)
277{
278 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000279 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000280 const char *errors = NULL;
281 int byteorder = 0;
Walter Dörwald69652032004-09-07 20:24:22 +0000282 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000283 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000284 PyObject *decoded;
285
286 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode",
287 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000288 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000289 if (size < 0) {
290 PyErr_SetString(PyExc_ValueError, "negative argument");
291 return 0;
292 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000293 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000294 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
295 final ? NULL : &consumed);
296 if (decoded == NULL)
297 return NULL;
298 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000299}
300
301static PyObject *
302utf_16_le_decode(PyObject *self,
303 PyObject *args)
304{
305 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000306 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000307 const char *errors = NULL;
308 int byteorder = -1;
Walter Dörwald69652032004-09-07 20:24:22 +0000309 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000310 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000311 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000312
Walter Dörwald69652032004-09-07 20:24:22 +0000313 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode",
314 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000315 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316
Martin v. Löwis18e16552006-02-15 17:27:45 +0000317 if (size < 0) {
318 PyErr_SetString(PyExc_ValueError, "negative argument");
319 return 0;
320 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000321 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000322 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
323 &byteorder, final ? NULL : &consumed);
324 if (decoded == NULL)
325 return NULL;
326 return codec_tuple(decoded, consumed);
327
Guido van Rossume2d67f92000-03-10 23:09:23 +0000328}
329
330static PyObject *
331utf_16_be_decode(PyObject *self,
332 PyObject *args)
333{
334 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000335 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000336 const char *errors = NULL;
337 int byteorder = 1;
Walter Dörwald69652032004-09-07 20:24:22 +0000338 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000339 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000340 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000341
Walter Dörwald69652032004-09-07 20:24:22 +0000342 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode",
343 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000344 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000345 if (size < 0) {
346 PyErr_SetString(PyExc_ValueError, "negative argument");
347 return 0;
348 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000349 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000350 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
351 &byteorder, final ? NULL : &consumed);
352 if (decoded == NULL)
353 return NULL;
354 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000355}
356
357/* This non-standard version also provides access to the byteorder
358 parameter of the builtin UTF-16 codec.
359
360 It returns a tuple (unicode, bytesread, byteorder) with byteorder
361 being the value in effect at the end of data.
362
363*/
364
365static PyObject *
366utf_16_ex_decode(PyObject *self,
367 PyObject *args)
368{
369 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000370 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000371 const char *errors = NULL;
372 int byteorder = 0;
373 PyObject *unicode, *tuple;
Walter Dörwald69652032004-09-07 20:24:22 +0000374 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000375 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000376
377 if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode",
378 &data, &size, &errors, &byteorder, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000379 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000380 if (size < 0) {
381 PyErr_SetString(PyExc_ValueError, "negative argument");
382 return 0;
383 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000384 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000385 unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
386 final ? NULL : &consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000387 if (unicode == NULL)
388 return NULL;
Georg Brandl96a8c392006-05-29 21:04:52 +0000389 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000390 Py_DECREF(unicode);
391 return tuple;
392}
393
394static PyObject *
395unicode_escape_decode(PyObject *self,
396 PyObject *args)
397{
398 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000399 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000400 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000401
Guido van Rossume2d67f92000-03-10 23:09:23 +0000402 if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
403 &data, &size, &errors))
404 return NULL;
405
406 return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
407 size);
408}
409
410static PyObject *
411raw_unicode_escape_decode(PyObject *self,
412 PyObject *args)
413{
414 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000415 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000416 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000417
Guido van Rossume2d67f92000-03-10 23:09:23 +0000418 if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
419 &data, &size, &errors))
420 return NULL;
421
422 return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
423 size);
424}
425
426static PyObject *
427latin_1_decode(PyObject *self,
428 PyObject *args)
429{
430 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000431 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000432 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000433
Guido van Rossume2d67f92000-03-10 23:09:23 +0000434 if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
435 &data, &size, &errors))
436 return NULL;
437
438 return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
439 size);
440}
441
442static PyObject *
443ascii_decode(PyObject *self,
444 PyObject *args)
445{
446 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000447 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000448 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000449
Guido van Rossume2d67f92000-03-10 23:09:23 +0000450 if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
451 &data, &size, &errors))
452 return NULL;
453
454 return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
455 size);
456}
457
458static PyObject *
459charmap_decode(PyObject *self,
460 PyObject *args)
461{
462 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000464 const char *errors = NULL;
465 PyObject *mapping = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000466
Guido van Rossume2d67f92000-03-10 23:09:23 +0000467 if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
468 &data, &size, &errors, &mapping))
469 return NULL;
470 if (mapping == Py_None)
471 mapping = NULL;
472
473 return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
474 size);
475}
476
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000477#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000478
479static PyObject *
480mbcs_decode(PyObject *self,
481 PyObject *args)
482{
483 const char *data;
Martin v. Löwisd8251432006-06-14 05:21:04 +0000484 Py_ssize_t size, consumed;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000485 const char *errors = NULL;
Martin v. Löwis961b91b2006-08-02 13:53:55 +0000486 int final = 0;
Martin v. Löwisd8251432006-06-14 05:21:04 +0000487 PyObject *decoded;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000488
Martin v. Löwisd8251432006-06-14 05:21:04 +0000489 if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode",
490 &data, &size, &errors, &final))
Guido van Rossum24bdb042000-03-28 20:29:59 +0000491 return NULL;
492
Martin v. Löwisd8251432006-06-14 05:21:04 +0000493 decoded = PyUnicode_DecodeMBCSStateful(
494 data, size, errors, final ? NULL : &consumed);
495 if (!decoded)
496 return NULL;
497 return codec_tuple(decoded, final ? size : consumed);
Guido van Rossum24bdb042000-03-28 20:29:59 +0000498}
499
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000500#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000501
Guido van Rossume2d67f92000-03-10 23:09:23 +0000502/* --- Encoder ------------------------------------------------------------ */
503
504static PyObject *
505readbuffer_encode(PyObject *self,
506 PyObject *args)
507{
508 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000509 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000510 const char *errors = NULL;
511
512 if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
513 &data, &size, &errors))
514 return NULL;
515
516 return codec_tuple(PyString_FromStringAndSize(data, size),
517 size);
518}
519
520static PyObject *
521charbuffer_encode(PyObject *self,
522 PyObject *args)
523{
524 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000526 const char *errors = NULL;
527
528 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
529 &data, &size, &errors))
530 return NULL;
531
532 return codec_tuple(PyString_FromStringAndSize(data, size),
533 size);
534}
535
536static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000537unicode_internal_encode(PyObject *self,
538 PyObject *args)
539{
540 PyObject *obj;
541 const char *errors = NULL;
542 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000543 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000544
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000545 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
546 &obj, &errors))
547 return NULL;
548
549 if (PyUnicode_Check(obj)) {
550 data = PyUnicode_AS_DATA(obj);
551 size = PyUnicode_GET_DATA_SIZE(obj);
552 return codec_tuple(PyString_FromStringAndSize(data, size),
553 size);
554 }
555 else {
556 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
557 return NULL;
558 return codec_tuple(PyString_FromStringAndSize(data, size),
559 size);
560 }
561}
562
563static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000564utf_7_encode(PyObject *self,
565 PyObject *args)
566{
567 PyObject *str, *v;
568 const char *errors = NULL;
569
570 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
571 &str, &errors))
572 return NULL;
573
574 str = PyUnicode_FromObject(str);
575 if (str == NULL)
576 return NULL;
577 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
578 PyUnicode_GET_SIZE(str),
Georg Brandl96a8c392006-05-29 21:04:52 +0000579 0,
580 0,
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000581 errors),
582 PyUnicode_GET_SIZE(str));
583 Py_DECREF(str);
584 return v;
585}
586
587static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000588utf_8_encode(PyObject *self,
589 PyObject *args)
590{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000591 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000592 const char *errors = NULL;
593
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000594 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000595 &str, &errors))
596 return NULL;
597
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000598 str = PyUnicode_FromObject(str);
599 if (str == NULL)
600 return NULL;
601 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
602 PyUnicode_GET_SIZE(str),
603 errors),
604 PyUnicode_GET_SIZE(str));
605 Py_DECREF(str);
606 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000607}
608
609/* This version provides access to the byteorder parameter of the
610 builtin UTF-16 codecs as optional third argument. It defaults to 0
611 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000612 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000613
614*/
615
616static PyObject *
617utf_16_encode(PyObject *self,
618 PyObject *args)
619{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000620 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000621 const char *errors = NULL;
622 int byteorder = 0;
623
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000624 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000625 &str, &errors, &byteorder))
626 return NULL;
627
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000628 str = PyUnicode_FromObject(str);
629 if (str == NULL)
630 return NULL;
631 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
632 PyUnicode_GET_SIZE(str),
633 errors,
634 byteorder),
635 PyUnicode_GET_SIZE(str));
636 Py_DECREF(str);
637 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000638}
639
640static PyObject *
641utf_16_le_encode(PyObject *self,
642 PyObject *args)
643{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000644 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000645 const char *errors = NULL;
646
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000647 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000648 &str, &errors))
649 return NULL;
650
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000651 str = PyUnicode_FromObject(str);
652 if (str == NULL)
653 return NULL;
654 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000655 PyUnicode_GET_SIZE(str),
656 errors,
657 -1),
658 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000659 Py_DECREF(str);
660 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000661}
662
663static PyObject *
664utf_16_be_encode(PyObject *self,
665 PyObject *args)
666{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000667 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000668 const char *errors = NULL;
669
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000670 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000671 &str, &errors))
672 return NULL;
673
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000674 str = PyUnicode_FromObject(str);
675 if (str == NULL)
676 return NULL;
677 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
678 PyUnicode_GET_SIZE(str),
679 errors,
680 +1),
681 PyUnicode_GET_SIZE(str));
682 Py_DECREF(str);
683 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000684}
685
686static PyObject *
687unicode_escape_encode(PyObject *self,
688 PyObject *args)
689{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000690 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000691 const char *errors = NULL;
692
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000693 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000694 &str, &errors))
695 return NULL;
696
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000697 str = PyUnicode_FromObject(str);
698 if (str == NULL)
699 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000700 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000701 PyUnicode_GET_SIZE(str)),
702 PyUnicode_GET_SIZE(str));
703 Py_DECREF(str);
704 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000705}
706
707static PyObject *
708raw_unicode_escape_encode(PyObject *self,
709 PyObject *args)
710{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000711 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000712 const char *errors = NULL;
713
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000714 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000715 &str, &errors))
716 return NULL;
717
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000718 str = PyUnicode_FromObject(str);
719 if (str == NULL)
720 return NULL;
721 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000722 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000723 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000724 PyUnicode_GET_SIZE(str));
725 Py_DECREF(str);
726 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000727}
728
729static PyObject *
730latin_1_encode(PyObject *self,
731 PyObject *args)
732{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000733 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000734 const char *errors = NULL;
735
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000736 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000737 &str, &errors))
738 return NULL;
739
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000740 str = PyUnicode_FromObject(str);
741 if (str == NULL)
742 return NULL;
743 v = codec_tuple(PyUnicode_EncodeLatin1(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000744 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000745 PyUnicode_GET_SIZE(str),
746 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000747 PyUnicode_GET_SIZE(str));
748 Py_DECREF(str);
749 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000750}
751
752static PyObject *
753ascii_encode(PyObject *self,
754 PyObject *args)
755{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000756 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000757 const char *errors = NULL;
758
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000759 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000760 &str, &errors))
761 return NULL;
762
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000763 str = PyUnicode_FromObject(str);
764 if (str == NULL)
765 return NULL;
766 v = codec_tuple(PyUnicode_EncodeASCII(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000767 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000768 PyUnicode_GET_SIZE(str),
769 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000770 PyUnicode_GET_SIZE(str));
771 Py_DECREF(str);
772 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000773}
774
775static PyObject *
776charmap_encode(PyObject *self,
777 PyObject *args)
778{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000779 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000780 const char *errors = NULL;
781 PyObject *mapping = NULL;
782
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000783 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000784 &str, &errors, &mapping))
785 return NULL;
786 if (mapping == Py_None)
787 mapping = NULL;
788
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000789 str = PyUnicode_FromObject(str);
790 if (str == NULL)
791 return NULL;
792 v = codec_tuple(PyUnicode_EncodeCharmap(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000793 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000794 PyUnicode_GET_SIZE(str),
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000795 mapping,
Guido van Rossume2d67f92000-03-10 23:09:23 +0000796 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000797 PyUnicode_GET_SIZE(str));
798 Py_DECREF(str);
799 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000800}
801
Martin v. Löwis3f767792006-06-04 19:36:28 +0000802static PyObject*
803charmap_build(PyObject *self, PyObject *args)
804{
805 PyObject *map;
806 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
807 return NULL;
808 return PyUnicode_BuildEncodingMap(map);
809}
810
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000811#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000812
813static PyObject *
814mbcs_encode(PyObject *self,
815 PyObject *args)
816{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000817 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000818 const char *errors = NULL;
819
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000820 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000821 &str, &errors))
822 return NULL;
823
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000824 str = PyUnicode_FromObject(str);
825 if (str == NULL)
826 return NULL;
827 v = codec_tuple(PyUnicode_EncodeMBCS(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000828 PyUnicode_AS_UNICODE(str),
Guido van Rossum24bdb042000-03-28 20:29:59 +0000829 PyUnicode_GET_SIZE(str),
830 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000831 PyUnicode_GET_SIZE(str));
832 Py_DECREF(str);
833 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000834}
835
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000836#endif /* MS_WINDOWS */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000837#endif /* Py_USING_UNICODE */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000838
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000839/* --- Error handler registry --------------------------------------------- */
840
Walter Dörwald0ae29812002-10-31 13:36:29 +0000841PyDoc_STRVAR(register_error__doc__,
842"register_error(errors, handler)\n\
843\n\
844Register the specified error handler under the name\n\
845errors. handler must be a callable object, that\n\
846will be called with an exception instance containing\n\
847information about the location of the encoding/decoding\n\
848error and must return a (replacement, new position) tuple.");
849
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000850static PyObject *register_error(PyObject *self, PyObject *args)
851{
852 const char *name;
853 PyObject *handler;
854
855 if (!PyArg_ParseTuple(args, "sO:register_error",
856 &name, &handler))
857 return NULL;
858 if (PyCodec_RegisterError(name, handler))
859 return NULL;
Georg Brandl96a8c392006-05-29 21:04:52 +0000860 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000861}
862
Walter Dörwald0ae29812002-10-31 13:36:29 +0000863PyDoc_STRVAR(lookup_error__doc__,
864"lookup_error(errors) -> handler\n\
865\n\
866Return the error handler for the specified error handling name\n\
867or raise a LookupError, if no handler exists under this name.");
868
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000869static PyObject *lookup_error(PyObject *self, PyObject *args)
870{
871 const char *name;
872
873 if (!PyArg_ParseTuple(args, "s:lookup_error",
874 &name))
875 return NULL;
876 return PyCodec_LookupError(name);
877}
878
Guido van Rossume2d67f92000-03-10 23:09:23 +0000879/* --- Module API --------------------------------------------------------- */
880
881static PyMethodDef _codecs_functions[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +0000882 {"register", codec_register, METH_O,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000883 register__doc__},
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000884 {"lookup", codec_lookup, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000885 lookup__doc__},
Brett Cannon3e377de2004-07-10 21:41:14 +0000886 {"encode", codec_encode, METH_VARARGS,
887 encode__doc__},
888 {"decode", codec_decode, METH_VARARGS,
889 decode__doc__},
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000890 {"escape_encode", escape_encode, METH_VARARGS},
891 {"escape_decode", escape_decode, METH_VARARGS},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000892#ifdef Py_USING_UNICODE
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000893 {"utf_8_encode", utf_8_encode, METH_VARARGS},
894 {"utf_8_decode", utf_8_decode, METH_VARARGS},
895 {"utf_7_encode", utf_7_encode, METH_VARARGS},
896 {"utf_7_decode", utf_7_decode, METH_VARARGS},
897 {"utf_16_encode", utf_16_encode, METH_VARARGS},
898 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
899 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
900 {"utf_16_decode", utf_16_decode, METH_VARARGS},
901 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
902 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
903 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
904 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
905 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
906 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
907 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
908 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
909 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
910 {"latin_1_encode", latin_1_encode, METH_VARARGS},
911 {"latin_1_decode", latin_1_decode, METH_VARARGS},
912 {"ascii_encode", ascii_encode, METH_VARARGS},
913 {"ascii_decode", ascii_decode, METH_VARARGS},
914 {"charmap_encode", charmap_encode, METH_VARARGS},
915 {"charmap_decode", charmap_decode, METH_VARARGS},
Martin v. Löwis3f767792006-06-04 19:36:28 +0000916 {"charmap_build", charmap_build, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000917 {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
918 {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000919#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000920 {"mbcs_encode", mbcs_encode, METH_VARARGS},
921 {"mbcs_decode", mbcs_decode, METH_VARARGS},
Guido van Rossum24bdb042000-03-28 20:29:59 +0000922#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000923#endif /* Py_USING_UNICODE */
Walter Dörwald0ae29812002-10-31 13:36:29 +0000924 {"register_error", register_error, METH_VARARGS,
925 register_error__doc__},
926 {"lookup_error", lookup_error, METH_VARARGS,
927 lookup_error__doc__},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000928 {NULL, NULL} /* sentinel */
929};
930
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000931PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000932init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000933{
934 Py_InitModule("_codecs", _codecs_functions);
935}