blob: cd766c333470a11c6003807371ca3b40fa3fc18d [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
Walter Dörwald1ab83302007-05-18 17:15:44 +0000175 size = PyUnicode_GET_SIZE(str);
176 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
536 return codec_tuple(PyString_FromStringAndSize(data, size),
537 size);
538}
539
540static PyObject *
541charbuffer_encode(PyObject *self,
542 PyObject *args)
543{
544 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000545 Py_ssize_t size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000546 const char *errors = NULL;
547
548 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
549 &data, &size, &errors))
550 return NULL;
551
552 return codec_tuple(PyString_FromStringAndSize(data, size),
553 size);
554}
555
556static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000557unicode_internal_encode(PyObject *self,
558 PyObject *args)
559{
560 PyObject *obj;
561 const char *errors = NULL;
562 const char *data;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563 Py_ssize_t size;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000564
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000565 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
566 &obj, &errors))
567 return NULL;
568
569 if (PyUnicode_Check(obj)) {
570 data = PyUnicode_AS_DATA(obj);
571 size = PyUnicode_GET_DATA_SIZE(obj);
572 return codec_tuple(PyString_FromStringAndSize(data, size),
573 size);
574 }
575 else {
576 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
577 return NULL;
578 return codec_tuple(PyString_FromStringAndSize(data, size),
579 size);
580 }
581}
582
583static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000584utf_7_encode(PyObject *self,
585 PyObject *args)
586{
587 PyObject *str, *v;
588 const char *errors = NULL;
589
590 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
591 &str, &errors))
592 return NULL;
593
594 str = PyUnicode_FromObject(str);
595 if (str == NULL)
596 return NULL;
597 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
598 PyUnicode_GET_SIZE(str),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000599 0,
600 0,
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000601 errors),
602 PyUnicode_GET_SIZE(str));
603 Py_DECREF(str);
604 return v;
605}
606
607static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000608utf_8_encode(PyObject *self,
609 PyObject *args)
610{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000611 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000612 const char *errors = NULL;
613
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000614 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000615 &str, &errors))
616 return NULL;
617
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000618 str = PyUnicode_FromObject(str);
619 if (str == NULL)
620 return NULL;
621 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
622 PyUnicode_GET_SIZE(str),
623 errors),
624 PyUnicode_GET_SIZE(str));
625 Py_DECREF(str);
626 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000627}
628
629/* This version provides access to the byteorder parameter of the
630 builtin UTF-16 codecs as optional third argument. It defaults to 0
631 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000632 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000633
634*/
635
636static PyObject *
637utf_16_encode(PyObject *self,
638 PyObject *args)
639{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000640 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000641 const char *errors = NULL;
642 int byteorder = 0;
643
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000644 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000645 &str, &errors, &byteorder))
646 return NULL;
647
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000648 str = PyUnicode_FromObject(str);
649 if (str == NULL)
650 return NULL;
651 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
652 PyUnicode_GET_SIZE(str),
653 errors,
654 byteorder),
655 PyUnicode_GET_SIZE(str));
656 Py_DECREF(str);
657 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000658}
659
660static PyObject *
661utf_16_le_encode(PyObject *self,
662 PyObject *args)
663{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000664 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000665 const char *errors = NULL;
666
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000667 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000668 &str, &errors))
669 return NULL;
670
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000671 str = PyUnicode_FromObject(str);
672 if (str == NULL)
673 return NULL;
674 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000675 PyUnicode_GET_SIZE(str),
676 errors,
677 -1),
678 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000679 Py_DECREF(str);
680 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000681}
682
683static PyObject *
684utf_16_be_encode(PyObject *self,
685 PyObject *args)
686{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000687 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000688 const char *errors = NULL;
689
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000690 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000691 &str, &errors))
692 return NULL;
693
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000694 str = PyUnicode_FromObject(str);
695 if (str == NULL)
696 return NULL;
697 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
698 PyUnicode_GET_SIZE(str),
699 errors,
700 +1),
701 PyUnicode_GET_SIZE(str));
702 Py_DECREF(str);
703 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000704}
705
706static PyObject *
707unicode_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: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;
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000720 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000721 PyUnicode_GET_SIZE(str)),
722 PyUnicode_GET_SIZE(str));
723 Py_DECREF(str);
724 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000725}
726
727static PyObject *
728raw_unicode_escape_encode(PyObject *self,
729 PyObject *args)
730{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000731 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000732 const char *errors = NULL;
733
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000734 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000735 &str, &errors))
736 return NULL;
737
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000738 str = PyUnicode_FromObject(str);
739 if (str == NULL)
740 return NULL;
741 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000742 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000743 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000744 PyUnicode_GET_SIZE(str));
745 Py_DECREF(str);
746 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000747}
748
749static PyObject *
750latin_1_encode(PyObject *self,
751 PyObject *args)
752{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000753 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000754 const char *errors = NULL;
755
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000756 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000757 &str, &errors))
758 return NULL;
759
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000760 str = PyUnicode_FromObject(str);
761 if (str == NULL)
762 return NULL;
763 v = codec_tuple(PyUnicode_EncodeLatin1(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000764 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000765 PyUnicode_GET_SIZE(str),
766 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000767 PyUnicode_GET_SIZE(str));
768 Py_DECREF(str);
769 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000770}
771
772static PyObject *
773ascii_encode(PyObject *self,
774 PyObject *args)
775{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000776 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000777 const char *errors = NULL;
778
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000779 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000780 &str, &errors))
781 return NULL;
782
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000783 str = PyUnicode_FromObject(str);
784 if (str == NULL)
785 return NULL;
786 v = codec_tuple(PyUnicode_EncodeASCII(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000787 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000788 PyUnicode_GET_SIZE(str),
789 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000790 PyUnicode_GET_SIZE(str));
791 Py_DECREF(str);
792 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000793}
794
795static PyObject *
796charmap_encode(PyObject *self,
797 PyObject *args)
798{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000799 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000800 const char *errors = NULL;
801 PyObject *mapping = NULL;
802
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000803 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000804 &str, &errors, &mapping))
805 return NULL;
806 if (mapping == Py_None)
807 mapping = NULL;
808
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000809 str = PyUnicode_FromObject(str);
810 if (str == NULL)
811 return NULL;
812 v = codec_tuple(PyUnicode_EncodeCharmap(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000813 PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000814 PyUnicode_GET_SIZE(str),
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000815 mapping,
Guido van Rossume2d67f92000-03-10 23:09:23 +0000816 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000817 PyUnicode_GET_SIZE(str));
818 Py_DECREF(str);
819 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000820}
821
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000822static PyObject*
823charmap_build(PyObject *self, PyObject *args)
824{
825 PyObject *map;
826 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
827 return NULL;
828 return PyUnicode_BuildEncodingMap(map);
829}
830
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000831#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000832
833static PyObject *
834mbcs_encode(PyObject *self,
835 PyObject *args)
836{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000837 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000838 const char *errors = NULL;
839
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000840 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000841 &str, &errors))
842 return NULL;
843
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000844 str = PyUnicode_FromObject(str);
845 if (str == NULL)
846 return NULL;
847 v = codec_tuple(PyUnicode_EncodeMBCS(
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000848 PyUnicode_AS_UNICODE(str),
Guido van Rossum24bdb042000-03-28 20:29:59 +0000849 PyUnicode_GET_SIZE(str),
850 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000851 PyUnicode_GET_SIZE(str));
852 Py_DECREF(str);
853 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000854}
855
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000856#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000857
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000858/* --- Error handler registry --------------------------------------------- */
859
Walter Dörwald0ae29812002-10-31 13:36:29 +0000860PyDoc_STRVAR(register_error__doc__,
861"register_error(errors, handler)\n\
862\n\
863Register the specified error handler under the name\n\
864errors. handler must be a callable object, that\n\
865will be called with an exception instance containing\n\
866information about the location of the encoding/decoding\n\
867error and must return a (replacement, new position) tuple.");
868
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000869static PyObject *register_error(PyObject *self, PyObject *args)
870{
871 const char *name;
872 PyObject *handler;
873
874 if (!PyArg_ParseTuple(args, "sO:register_error",
875 &name, &handler))
876 return NULL;
877 if (PyCodec_RegisterError(name, handler))
878 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000879 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000880}
881
Walter Dörwald0ae29812002-10-31 13:36:29 +0000882PyDoc_STRVAR(lookup_error__doc__,
883"lookup_error(errors) -> handler\n\
884\n\
885Return the error handler for the specified error handling name\n\
886or raise a LookupError, if no handler exists under this name.");
887
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000888static PyObject *lookup_error(PyObject *self, PyObject *args)
889{
890 const char *name;
891
892 if (!PyArg_ParseTuple(args, "s:lookup_error",
893 &name))
894 return NULL;
895 return PyCodec_LookupError(name);
896}
897
Guido van Rossume2d67f92000-03-10 23:09:23 +0000898/* --- Module API --------------------------------------------------------- */
899
900static PyMethodDef _codecs_functions[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000901 {"register", codec_register, METH_O,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000902 register__doc__},
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000903 {"lookup", codec_lookup, METH_VARARGS,
Walter Dörwald0ae29812002-10-31 13:36:29 +0000904 lookup__doc__},
Brett Cannon3e377de2004-07-10 21:41:14 +0000905 {"encode", codec_encode, METH_VARARGS,
906 encode__doc__},
907 {"decode", codec_decode, METH_VARARGS,
908 decode__doc__},
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000909 {"escape_encode", escape_encode, METH_VARARGS},
910 {"escape_decode", escape_decode, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000911 {"utf_8_encode", utf_8_encode, METH_VARARGS},
912 {"utf_8_decode", utf_8_decode, METH_VARARGS},
913 {"utf_7_encode", utf_7_encode, METH_VARARGS},
914 {"utf_7_decode", utf_7_decode, METH_VARARGS},
915 {"utf_16_encode", utf_16_encode, METH_VARARGS},
916 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
917 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
918 {"utf_16_decode", utf_16_decode, METH_VARARGS},
919 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
920 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
921 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
922 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
923 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
924 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
925 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
926 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
927 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
928 {"latin_1_encode", latin_1_encode, METH_VARARGS},
929 {"latin_1_decode", latin_1_decode, METH_VARARGS},
930 {"ascii_encode", ascii_encode, METH_VARARGS},
931 {"ascii_decode", ascii_decode, METH_VARARGS},
932 {"charmap_encode", charmap_encode, METH_VARARGS},
933 {"charmap_decode", charmap_decode, METH_VARARGS},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000934 {"charmap_build", charmap_build, METH_VARARGS},
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000935 {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
936 {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000937#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000938 {"mbcs_encode", mbcs_encode, METH_VARARGS},
939 {"mbcs_decode", mbcs_decode, METH_VARARGS},
Guido van Rossum24bdb042000-03-28 20:29:59 +0000940#endif
Walter Dörwald0ae29812002-10-31 13:36:29 +0000941 {"register_error", register_error, METH_VARARGS,
942 register_error__doc__},
943 {"lookup_error", lookup_error, METH_VARARGS,
944 lookup_error__doc__},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000945 {NULL, NULL} /* sentinel */
946};
947
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000948PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000949init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000950{
951 Py_InitModule("_codecs", _codecs_functions);
952}