blob: 4dbceb78be64283bea82b7c07e85d6b2142ecde6 [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
Georg Brandl96a8c392006-05-29 21:04:52 +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))
Georg Brandl96a8c392006-05-29 21:04:52 +000054 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000055
Georg Brandl96a8c392006-05-29 21:04:52 +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))
Georg Brandl96a8c392006-05-29 21:04:52 +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
Martin v. Löwise2713be2005-03-08 15:03:08 +000096#ifdef Py_USING_UNICODE
Marc-André Lemburg3f419742004-07-10 12:06:10 +000097 if (encoding == NULL)
98 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwise2713be2005-03-08 15:03:08 +000099#else
100 if (encoding == NULL) {
101 PyErr_SetString(PyExc_ValueError, "no encoding specified");
102 return NULL;
103 }
104#endif
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000105
106 /* Encode via the codec registry */
Georg Brandl96a8c392006-05-29 21:04:52 +0000107 return PyCodec_Encode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000108}
109
110PyDoc_STRVAR(decode__doc__,
111"decode(obj, [encoding[,errors]]) -> object\n\
112\n\
113Decodes obj using the codec registered for encoding. encoding defaults\n\
114to the default encoding. errors may be given to set a different error\n\
115handling scheme. Default is 'strict' meaning that encoding errors raise\n\
116a ValueError. Other possible values are 'ignore' and 'replace'\n\
117as well as any other name registerd with codecs.register_error that is\n\
118able to handle ValueErrors.");
119
120static PyObject *
121codec_decode(PyObject *self, PyObject *args)
122{
Brett Cannon3e377de2004-07-10 21:41:14 +0000123 const char *encoding = NULL;
124 const char *errors = NULL;
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000125 PyObject *v;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000126
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000127 if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
128 return NULL;
129
Martin v. Löwise2713be2005-03-08 15:03:08 +0000130#ifdef Py_USING_UNICODE
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000131 if (encoding == NULL)
132 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwise2713be2005-03-08 15:03:08 +0000133#else
134 if (encoding == NULL) {
135 PyErr_SetString(PyExc_ValueError, "no encoding specified");
136 return NULL;
137 }
138#endif
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000139
140 /* Decode via the codec registry */
Georg Brandl96a8c392006-05-29 21:04:52 +0000141 return PyCodec_Decode(v, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000142}
143
Guido van Rossume2d67f92000-03-10 23:09:23 +0000144/* --- Helpers ------------------------------------------------------------ */
145
146static
147PyObject *codec_tuple(PyObject *unicode,
Martin v. Löwis66851282006-04-22 11:40:03 +0000148 Py_ssize_t len)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000149{
Georg Brandl96a8c392006-05-29 21:04:52 +0000150 PyObject *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000151 if (unicode == NULL)
Georg Brandl96a8c392006-05-29 21:04:52 +0000152 return NULL;
153 v = Py_BuildValue("On", unicode, len);
154 Py_DECREF(unicode);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000155 return v;
156}
157
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000158/* --- String codecs ------------------------------------------------------ */
159static PyObject *
160escape_decode(PyObject *self,
161 PyObject *args)
162{
163 const char *errors = NULL;
164 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000165 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000166
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000167 if (!PyArg_ParseTuple(args, "s#|z:escape_decode",
168 &data, &size, &errors))
169 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000170 return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL),
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000171 size);
172}
173
174static PyObject *
175escape_encode(PyObject *self,
176 PyObject *args)
177{
178 PyObject *str;
179 const char *errors = NULL;
180 char *buf;
Martin v. Löwis66851282006-04-22 11:40:03 +0000181 Py_ssize_t len;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000182
183 if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
184 &PyString_Type, &str, &errors))
185 return NULL;
186
187 str = PyString_Repr(str, 0);
188 if (!str)
189 return NULL;
190
191 /* The string will be quoted. Unquote, similar to unicode-escape. */
192 buf = PyString_AS_STRING (str);
193 len = PyString_GET_SIZE (str);
194 memmove(buf, buf+1, len-2);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000195 if (_PyString_Resize(&str, len-2) < 0)
196 return NULL;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000197
198 return codec_tuple(str, PyString_Size(str));
199}
200
201#ifdef Py_USING_UNICODE
Guido van Rossume2d67f92000-03-10 23:09:23 +0000202/* --- Decoder ------------------------------------------------------------ */
203
204static PyObject *
205unicode_internal_decode(PyObject *self,
206 PyObject *args)
207{
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000208 PyObject *obj;
209 const char *errors = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000210 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000212
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000213 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
214 &obj, &errors))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000215 return NULL;
216
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000217 if (PyUnicode_Check(obj)) {
218 Py_INCREF(obj);
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000219 return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
Marc-André Lemburg29273c82003-02-04 19:35:03 +0000220 }
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000221 else {
222 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
223 return NULL;
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000224
225 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000226 size);
227 }
Guido van Rossume2d67f92000-03-10 23:09:23 +0000228}
229
230static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000231utf_7_decode(PyObject *self,
232 PyObject *args)
233{
234 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000235 Py_ssize_t size;
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000236 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000237
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000238 if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode",
239 &data, &size, &errors))
240 return NULL;
241
242 return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors),
243 size);
244}
245
246static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000247utf_8_decode(PyObject *self,
248 PyObject *args)
249{
250 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000251 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000252 const char *errors = NULL;
Walter Dörwald69652032004-09-07 20:24:22 +0000253 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000255 PyObject *decoded = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000256
Walter Dörwald69652032004-09-07 20:24:22 +0000257 if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode",
258 &data, &size, &errors, &final))
259 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000260 if (size < 0) {
261 PyErr_SetString(PyExc_ValueError, "negative argument");
262 return 0;
263 }
Walter Dörwald69652032004-09-07 20:24:22 +0000264 consumed = size;
265
266 decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors,
267 final ? NULL : &consumed);
268 if (decoded == NULL)
269 return NULL;
270 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000271}
272
273static PyObject *
274utf_16_decode(PyObject *self,
275 PyObject *args)
276{
277 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000278 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000279 const char *errors = NULL;
280 int byteorder = 0;
Walter Dörwald69652032004-09-07 20:24:22 +0000281 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000282 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000283 PyObject *decoded;
284
285 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode",
286 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000287 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000288 if (size < 0) {
289 PyErr_SetString(PyExc_ValueError, "negative argument");
290 return 0;
291 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000292 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000293 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
294 final ? NULL : &consumed);
295 if (decoded == NULL)
296 return NULL;
297 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000298}
299
300static PyObject *
301utf_16_le_decode(PyObject *self,
302 PyObject *args)
303{
304 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000306 const char *errors = NULL;
307 int byteorder = -1;
Walter Dörwald69652032004-09-07 20:24:22 +0000308 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000310 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000311
Walter Dörwald69652032004-09-07 20:24:22 +0000312 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode",
313 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000314 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000315
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316 if (size < 0) {
317 PyErr_SetString(PyExc_ValueError, "negative argument");
318 return 0;
319 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000320 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000321 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
322 &byteorder, final ? NULL : &consumed);
323 if (decoded == NULL)
324 return NULL;
325 return codec_tuple(decoded, consumed);
326
Guido van Rossume2d67f92000-03-10 23:09:23 +0000327}
328
329static PyObject *
330utf_16_be_decode(PyObject *self,
331 PyObject *args)
332{
333 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000334 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000335 const char *errors = NULL;
336 int byteorder = 1;
Walter Dörwald69652032004-09-07 20:24:22 +0000337 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000338 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000339 PyObject *decoded = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000340
Walter Dörwald69652032004-09-07 20:24:22 +0000341 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode",
342 &data, &size, &errors, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000343 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000344 if (size < 0) {
345 PyErr_SetString(PyExc_ValueError, "negative argument");
346 return 0;
347 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000348 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000349 decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
350 &byteorder, final ? NULL : &consumed);
351 if (decoded == NULL)
352 return NULL;
353 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000354}
355
356/* This non-standard version also provides access to the byteorder
357 parameter of the builtin UTF-16 codec.
358
359 It returns a tuple (unicode, bytesread, byteorder) with byteorder
360 being the value in effect at the end of data.
361
362*/
363
364static PyObject *
365utf_16_ex_decode(PyObject *self,
366 PyObject *args)
367{
368 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000369 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000370 const char *errors = NULL;
371 int byteorder = 0;
372 PyObject *unicode, *tuple;
Walter Dörwald69652032004-09-07 20:24:22 +0000373 int final = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000374 Py_ssize_t consumed;
Walter Dörwald69652032004-09-07 20:24:22 +0000375
376 if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode",
377 &data, &size, &errors, &byteorder, &final))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000378 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379 if (size < 0) {
380 PyErr_SetString(PyExc_ValueError, "negative argument");
381 return 0;
382 }
Martin v. Löwisd532ba02006-05-27 08:54:29 +0000383 consumed = size; /* This is overwritten unless final is true. */
Walter Dörwald69652032004-09-07 20:24:22 +0000384 unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
385 final ? NULL : &consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000386 if (unicode == NULL)
387 return NULL;
Georg Brandl96a8c392006-05-29 21:04:52 +0000388 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000389 Py_DECREF(unicode);
390 return tuple;
391}
392
393static PyObject *
394unicode_escape_decode(PyObject *self,
395 PyObject *args)
396{
397 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000398 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000399 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000400
Guido van Rossume2d67f92000-03-10 23:09:23 +0000401 if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
402 &data, &size, &errors))
403 return NULL;
404
405 return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
406 size);
407}
408
409static PyObject *
410raw_unicode_escape_decode(PyObject *self,
411 PyObject *args)
412{
413 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000414 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000415 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000416
Guido van Rossume2d67f92000-03-10 23:09:23 +0000417 if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
418 &data, &size, &errors))
419 return NULL;
420
421 return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
422 size);
423}
424
425static PyObject *
426latin_1_decode(PyObject *self,
427 PyObject *args)
428{
429 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000430 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000431 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000432
Guido van Rossume2d67f92000-03-10 23:09:23 +0000433 if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
434 &data, &size, &errors))
435 return NULL;
436
437 return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
438 size);
439}
440
441static PyObject *
442ascii_decode(PyObject *self,
443 PyObject *args)
444{
445 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000447 const char *errors = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000448
Guido van Rossume2d67f92000-03-10 23:09:23 +0000449 if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
450 &data, &size, &errors))
451 return NULL;
452
453 return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
454 size);
455}
456
457static PyObject *
458charmap_decode(PyObject *self,
459 PyObject *args)
460{
461 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000463 const char *errors = NULL;
464 PyObject *mapping = NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000465
Guido van Rossume2d67f92000-03-10 23:09:23 +0000466 if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
467 &data, &size, &errors, &mapping))
468 return NULL;
469 if (mapping == Py_None)
470 mapping = NULL;
471
472 return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
473 size);
474}
475
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000476#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000477
478static PyObject *
479mbcs_decode(PyObject *self,
480 PyObject *args)
481{
482 const char *data;
Martin v. Löwisd8251432006-06-14 05:21:04 +0000483 Py_ssize_t size, consumed;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000484 const char *errors = NULL;
Martin v. Löwis961b91b2006-08-02 13:53:55 +0000485 int final = 0;
Martin v. Löwisd8251432006-06-14 05:21:04 +0000486 PyObject *decoded;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000487
Martin v. Löwisd8251432006-06-14 05:21:04 +0000488 if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode",
489 &data, &size, &errors, &final))
Guido van Rossum24bdb042000-03-28 20:29:59 +0000490 return NULL;
491
Martin v. Löwisd8251432006-06-14 05:21:04 +0000492 decoded = PyUnicode_DecodeMBCSStateful(
493 data, size, errors, final ? NULL : &consumed);
494 if (!decoded)
495 return NULL;
496 return codec_tuple(decoded, final ? size : consumed);
Guido van Rossum24bdb042000-03-28 20:29:59 +0000497}
498
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000499#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000500
Guido van Rossume2d67f92000-03-10 23:09:23 +0000501/* --- Encoder ------------------------------------------------------------ */
502
503static PyObject *
504readbuffer_encode(PyObject *self,
505 PyObject *args)
506{
507 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000509 const char *errors = NULL;
510
511 if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
512 &data, &size, &errors))
513 return NULL;
514
515 return codec_tuple(PyString_FromStringAndSize(data, size),
516 size);
517}
518
519static PyObject *
520charbuffer_encode(PyObject *self,
521 PyObject *args)
522{
523 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000525 const char *errors = NULL;
526
527 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
528 &data, &size, &errors))
529 return NULL;
530
531 return codec_tuple(PyString_FromStringAndSize(data, size),
532 size);
533}
534
535static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000536unicode_internal_encode(PyObject *self,
537 PyObject *args)
538{
539 PyObject *obj;
540 const char *errors = NULL;
541 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000542 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000543
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000544 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
545 &obj, &errors))
546 return NULL;
547
548 if (PyUnicode_Check(obj)) {
549 data = PyUnicode_AS_DATA(obj);
550 size = PyUnicode_GET_DATA_SIZE(obj);
551 return codec_tuple(PyString_FromStringAndSize(data, size),
552 size);
553 }
554 else {
555 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
556 return NULL;
557 return codec_tuple(PyString_FromStringAndSize(data, size),
558 size);
559 }
560}
561
562static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000563utf_7_encode(PyObject *self,
564 PyObject *args)
565{
566 PyObject *str, *v;
567 const char *errors = NULL;
568
569 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
570 &str, &errors))
571 return NULL;
572
573 str = PyUnicode_FromObject(str);
574 if (str == NULL)
575 return NULL;
576 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
577 PyUnicode_GET_SIZE(str),
Georg Brandl96a8c392006-05-29 21:04:52 +0000578 0,
579 0,
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000580 errors),
581 PyUnicode_GET_SIZE(str));
582 Py_DECREF(str);
583 return v;
584}
585
586static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000587utf_8_encode(PyObject *self,
588 PyObject *args)
589{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000590 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000591 const char *errors = NULL;
592
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000593 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000594 &str, &errors))
595 return NULL;
596
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000597 str = PyUnicode_FromObject(str);
598 if (str == NULL)
599 return NULL;
600 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
601 PyUnicode_GET_SIZE(str),
602 errors),
603 PyUnicode_GET_SIZE(str));
604 Py_DECREF(str);
605 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000606}
607
608/* This version provides access to the byteorder parameter of the
609 builtin UTF-16 codecs as optional third argument. It defaults to 0
610 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000611 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000612
613*/
614
615static PyObject *
616utf_16_encode(PyObject *self,
617 PyObject *args)
618{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000619 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000620 const char *errors = NULL;
621 int byteorder = 0;
622
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000623 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000624 &str, &errors, &byteorder))
625 return NULL;
626
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000627 str = PyUnicode_FromObject(str);
628 if (str == NULL)
629 return NULL;
630 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
631 PyUnicode_GET_SIZE(str),
632 errors,
633 byteorder),
634 PyUnicode_GET_SIZE(str));
635 Py_DECREF(str);
636 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000637}
638
639static PyObject *
640utf_16_le_encode(PyObject *self,
641 PyObject *args)
642{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000643 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000644 const char *errors = NULL;
645
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000646 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000647 &str, &errors))
648 return NULL;
649
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000650 str = PyUnicode_FromObject(str);
651 if (str == NULL)
652 return NULL;
653 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000654 PyUnicode_GET_SIZE(str),
655 errors,
656 -1),
657 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000658 Py_DECREF(str);
659 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000660}
661
662static PyObject *
663utf_16_be_encode(PyObject *self,
664 PyObject *args)
665{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000666 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000667 const char *errors = NULL;
668
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000669 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000670 &str, &errors))
671 return NULL;
672
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000673 str = PyUnicode_FromObject(str);
674 if (str == NULL)
675 return NULL;
676 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
677 PyUnicode_GET_SIZE(str),
678 errors,
679 +1),
680 PyUnicode_GET_SIZE(str));
681 Py_DECREF(str);
682 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000683}
684
685static PyObject *
686unicode_escape_encode(PyObject *self,
687 PyObject *args)
688{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000689 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000690 const char *errors = NULL;
691
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000692 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000693 &str, &errors))
694 return NULL;
695
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000696 str = PyUnicode_FromObject(str);
697 if (str == NULL)
698 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000699 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000700 PyUnicode_GET_SIZE(str)),
701 PyUnicode_GET_SIZE(str));
702 Py_DECREF(str);
703 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000704}
705
706static PyObject *
707raw_unicode_escape_encode(PyObject *self,
708 PyObject *args)
709{
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:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000714 &str, &errors))
715 return NULL;
716
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000717 str = PyUnicode_FromObject(str);
718 if (str == NULL)
719 return NULL;
720 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000721 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000722 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000723 PyUnicode_GET_SIZE(str));
724 Py_DECREF(str);
725 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000726}
727
728static PyObject *
729latin_1_encode(PyObject *self,
730 PyObject *args)
731{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000732 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000733 const char *errors = NULL;
734
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000735 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000736 &str, &errors))
737 return NULL;
738
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000739 str = PyUnicode_FromObject(str);
740 if (str == NULL)
741 return NULL;
742 v = codec_tuple(PyUnicode_EncodeLatin1(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000743 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000744 PyUnicode_GET_SIZE(str),
745 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000746 PyUnicode_GET_SIZE(str));
747 Py_DECREF(str);
748 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000749}
750
751static PyObject *
752ascii_encode(PyObject *self,
753 PyObject *args)
754{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000755 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000756 const char *errors = NULL;
757
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000758 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000759 &str, &errors))
760 return NULL;
761
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000762 str = PyUnicode_FromObject(str);
763 if (str == NULL)
764 return NULL;
765 v = codec_tuple(PyUnicode_EncodeASCII(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000766 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000767 PyUnicode_GET_SIZE(str),
768 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000769 PyUnicode_GET_SIZE(str));
770 Py_DECREF(str);
771 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000772}
773
774static PyObject *
775charmap_encode(PyObject *self,
776 PyObject *args)
777{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000778 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000779 const char *errors = NULL;
780 PyObject *mapping = NULL;
781
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000782 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000783 &str, &errors, &mapping))
784 return NULL;
785 if (mapping == Py_None)
786 mapping = NULL;
787
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000788 str = PyUnicode_FromObject(str);
789 if (str == NULL)
790 return NULL;
791 v = codec_tuple(PyUnicode_EncodeCharmap(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000792 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000793 PyUnicode_GET_SIZE(str),
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000794 mapping,
Guido van Rossume2d67f92000-03-10 23:09:23 +0000795 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000796 PyUnicode_GET_SIZE(str));
797 Py_DECREF(str);
798 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000799}
800
Martin v. Löwis3f767792006-06-04 19:36:28 +0000801static PyObject*
802charmap_build(PyObject *self, PyObject *args)
803{
804 PyObject *map;
805 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
806 return NULL;
807 return PyUnicode_BuildEncodingMap(map);
808}
809
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000810#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000811
812static PyObject *
813mbcs_encode(PyObject *self,
814 PyObject *args)
815{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000816 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000817 const char *errors = NULL;
818
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000819 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000820 &str, &errors))
821 return NULL;
822
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000823 str = PyUnicode_FromObject(str);
824 if (str == NULL)
825 return NULL;
826 v = codec_tuple(PyUnicode_EncodeMBCS(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000827 PyUnicode_AS_UNICODE(str),
Guido van Rossum24bdb042000-03-28 20:29:59 +0000828 PyUnicode_GET_SIZE(str),
829 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000830 PyUnicode_GET_SIZE(str));
831 Py_DECREF(str);
832 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000833}
834
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000835#endif /* MS_WINDOWS */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000836#endif /* Py_USING_UNICODE */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000837
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000838/* --- Error handler registry --------------------------------------------- */
839
Walter Dörwald0ae29812002-10-31 13:36:29 +0000840PyDoc_STRVAR(register_error__doc__,
841"register_error(errors, handler)\n\
842\n\
843Register the specified error handler under the name\n\
844errors. handler must be a callable object, that\n\
845will be called with an exception instance containing\n\
846information about the location of the encoding/decoding\n\
847error and must return a (replacement, new position) tuple.");
848
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000849static PyObject *register_error(PyObject *self, PyObject *args)
850{
851 const char *name;
852 PyObject *handler;
853
854 if (!PyArg_ParseTuple(args, "sO:register_error",
855 &name, &handler))
856 return NULL;
857 if (PyCodec_RegisterError(name, handler))
858 return NULL;
Georg Brandl96a8c392006-05-29 21:04:52 +0000859 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000860}
861
Walter Dörwald0ae29812002-10-31 13:36:29 +0000862PyDoc_STRVAR(lookup_error__doc__,
863"lookup_error(errors) -> handler\n\
864\n\
865Return the error handler for the specified error handling name\n\
866or raise a LookupError, if no handler exists under this name.");
867
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000868static PyObject *lookup_error(PyObject *self, PyObject *args)
869{
870 const char *name;
871
872 if (!PyArg_ParseTuple(args, "s:lookup_error",
873 &name))
874 return NULL;
875 return PyCodec_LookupError(name);
876}
877
Guido van Rossume2d67f92000-03-10 23:09:23 +0000878/* --- Module API --------------------------------------------------------- */
879
880static PyMethodDef _codecs_functions[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +0000881 {"register", codec_register, METH_O,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000882 register__doc__},
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000883 {"lookup", codec_lookup, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000884 lookup__doc__},
Brett Cannon3e377de2004-07-10 21:41:14 +0000885 {"encode", codec_encode, METH_VARARGS,
886 encode__doc__},
887 {"decode", codec_decode, METH_VARARGS,
888 decode__doc__},
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000889 {"escape_encode", escape_encode, METH_VARARGS},
890 {"escape_decode", escape_decode, METH_VARARGS},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000891#ifdef Py_USING_UNICODE
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000892 {"utf_8_encode", utf_8_encode, METH_VARARGS},
893 {"utf_8_decode", utf_8_decode, METH_VARARGS},
894 {"utf_7_encode", utf_7_encode, METH_VARARGS},
895 {"utf_7_decode", utf_7_decode, METH_VARARGS},
896 {"utf_16_encode", utf_16_encode, METH_VARARGS},
897 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
898 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
899 {"utf_16_decode", utf_16_decode, METH_VARARGS},
900 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
901 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
902 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
903 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
904 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
905 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
906 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
907 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
908 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
909 {"latin_1_encode", latin_1_encode, METH_VARARGS},
910 {"latin_1_decode", latin_1_decode, METH_VARARGS},
911 {"ascii_encode", ascii_encode, METH_VARARGS},
912 {"ascii_decode", ascii_decode, METH_VARARGS},
913 {"charmap_encode", charmap_encode, METH_VARARGS},
914 {"charmap_decode", charmap_decode, METH_VARARGS},
Martin v. Löwis3f767792006-06-04 19:36:28 +0000915 {"charmap_build", charmap_build, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000916 {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
917 {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000918#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000919 {"mbcs_encode", mbcs_encode, METH_VARARGS},
920 {"mbcs_decode", mbcs_decode, METH_VARARGS},
Guido van Rossum24bdb042000-03-28 20:29:59 +0000921#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000922#endif /* Py_USING_UNICODE */
Walter Dörwald0ae29812002-10-31 13:36:29 +0000923 {"register_error", register_error, METH_VARARGS,
924 register_error__doc__},
925 {"lookup_error", lookup_error, METH_VARARGS,
926 lookup_error__doc__},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000927 {NULL, NULL} /* sentinel */
928};
929
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000930PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000931init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000932{
933 Py_InitModule("_codecs", _codecs_functions);
934}