blob: a085bcf3ab99c4a5395b0b6d605f7b180506753e [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
17 <encoding>_encode(Unicode_object[,errors='strict']) ->
18 (string object, bytes consumed)
19
20 <encoding>_decode(char_buffer_obj[,errors='strict']) ->
21 (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
38#include "Python.h"
39
40/* --- Registry ----------------------------------------------------------- */
41
42static
43PyObject *codecregister(PyObject *self, PyObject *args)
44{
45 PyObject *search_function;
46
47 if (!PyArg_ParseTuple(args, "O:register", &search_function))
48 goto onError;
49
50 if (PyCodec_Register(search_function))
51 goto onError;
52
53 Py_INCREF(Py_None);
54 return Py_None;
55
56 onError:
57 return NULL;
58}
59
60static
61PyObject *codeclookup(PyObject *self, PyObject *args)
62{
63 char *encoding;
64
65 if (!PyArg_ParseTuple(args, "s:lookup", &encoding))
66 goto onError;
67
68 return _PyCodec_Lookup(encoding);
69
70 onError:
71 return NULL;
72}
73
Martin v. Löwis339d0f72001-08-17 18:39:25 +000074#ifdef Py_USING_UNICODE
Guido van Rossume2d67f92000-03-10 23:09:23 +000075/* --- Helpers ------------------------------------------------------------ */
76
77static
78PyObject *codec_tuple(PyObject *unicode,
79 int len)
80{
81 PyObject *v,*w;
82
83 if (unicode == NULL)
84 return NULL;
85 v = PyTuple_New(2);
86 if (v == NULL) {
87 Py_DECREF(unicode);
88 return NULL;
89 }
90 PyTuple_SET_ITEM(v,0,unicode);
91 w = PyInt_FromLong(len);
92 if (w == NULL) {
93 Py_DECREF(v);
94 return NULL;
95 }
96 PyTuple_SET_ITEM(v,1,w);
97 return v;
98}
99
100/* --- Decoder ------------------------------------------------------------ */
101
102static PyObject *
103unicode_internal_decode(PyObject *self,
104 PyObject *args)
105{
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000106 PyObject *obj;
107 const char *errors = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000108 const char *data;
109 int size;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000110
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000111 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
112 &obj, &errors))
Guido van Rossume2d67f92000-03-10 23:09:23 +0000113 return NULL;
114
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000115 if (PyUnicode_Check(obj))
116 return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
117 else {
118 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
119 return NULL;
120 return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
121 size / sizeof(Py_UNICODE)),
122 size);
123 }
Guido van Rossume2d67f92000-03-10 23:09:23 +0000124}
125
126static PyObject *
127utf_8_decode(PyObject *self,
128 PyObject *args)
129{
130 const char *data;
131 int size;
132 const char *errors = NULL;
133
134 if (!PyArg_ParseTuple(args, "t#|z:utf_8_decode",
135 &data, &size, &errors))
136 return NULL;
137
138 return codec_tuple(PyUnicode_DecodeUTF8(data, size, errors),
139 size);
140}
141
142static PyObject *
143utf_16_decode(PyObject *self,
144 PyObject *args)
145{
146 const char *data;
147 int size;
148 const char *errors = NULL;
149 int byteorder = 0;
150
151 if (!PyArg_ParseTuple(args, "t#|z:utf_16_decode",
152 &data, &size, &errors))
153 return NULL;
154 return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
155 size);
156}
157
158static PyObject *
159utf_16_le_decode(PyObject *self,
160 PyObject *args)
161{
162 const char *data;
163 int size;
164 const char *errors = NULL;
165 int byteorder = -1;
166
167 if (!PyArg_ParseTuple(args, "t#|z:utf_16_le_decode",
168 &data, &size, &errors))
169 return NULL;
170 return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
171 size);
172}
173
174static PyObject *
175utf_16_be_decode(PyObject *self,
176 PyObject *args)
177{
178 const char *data;
179 int size;
180 const char *errors = NULL;
181 int byteorder = 1;
182
183 if (!PyArg_ParseTuple(args, "t#|z:utf_16_be_decode",
184 &data, &size, &errors))
185 return NULL;
186 return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
187 size);
188}
189
190/* This non-standard version also provides access to the byteorder
191 parameter of the builtin UTF-16 codec.
192
193 It returns a tuple (unicode, bytesread, byteorder) with byteorder
194 being the value in effect at the end of data.
195
196*/
197
198static PyObject *
199utf_16_ex_decode(PyObject *self,
200 PyObject *args)
201{
202 const char *data;
203 int size;
204 const char *errors = NULL;
205 int byteorder = 0;
206 PyObject *unicode, *tuple;
207
208 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_ex_decode",
209 &data, &size, &errors, &byteorder))
210 return NULL;
211
212 unicode = PyUnicode_DecodeUTF16(data, size, errors, &byteorder);
213 if (unicode == NULL)
214 return NULL;
215 tuple = Py_BuildValue("Oii", unicode, size, byteorder);
216 Py_DECREF(unicode);
217 return tuple;
218}
219
220static PyObject *
221unicode_escape_decode(PyObject *self,
222 PyObject *args)
223{
224 const char *data;
225 int size;
226 const char *errors = NULL;
227
228 if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
229 &data, &size, &errors))
230 return NULL;
231
232 return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
233 size);
234}
235
236static PyObject *
237raw_unicode_escape_decode(PyObject *self,
238 PyObject *args)
239{
240 const char *data;
241 int size;
242 const char *errors = NULL;
243
244 if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
245 &data, &size, &errors))
246 return NULL;
247
248 return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
249 size);
250}
251
252static PyObject *
253latin_1_decode(PyObject *self,
254 PyObject *args)
255{
256 const char *data;
257 int size;
258 const char *errors = NULL;
259
260 if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
261 &data, &size, &errors))
262 return NULL;
263
264 return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
265 size);
266}
267
268static PyObject *
269ascii_decode(PyObject *self,
270 PyObject *args)
271{
272 const char *data;
273 int size;
274 const char *errors = NULL;
275
276 if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
277 &data, &size, &errors))
278 return NULL;
279
280 return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
281 size);
282}
283
284static PyObject *
285charmap_decode(PyObject *self,
286 PyObject *args)
287{
288 const char *data;
289 int size;
290 const char *errors = NULL;
291 PyObject *mapping = NULL;
292
293 if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
294 &data, &size, &errors, &mapping))
295 return NULL;
296 if (mapping == Py_None)
297 mapping = NULL;
298
299 return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
300 size);
301}
302
Fredrik Lundh30831632001-06-26 15:11:00 +0000303#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000304
305static PyObject *
306mbcs_decode(PyObject *self,
307 PyObject *args)
308{
309 const char *data;
310 int size;
311 const char *errors = NULL;
312
313 if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode",
314 &data, &size, &errors))
315 return NULL;
316
317 return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors),
318 size);
319}
320
321#endif /* MS_WIN32 */
322
Guido van Rossume2d67f92000-03-10 23:09:23 +0000323/* --- Encoder ------------------------------------------------------------ */
324
325static PyObject *
326readbuffer_encode(PyObject *self,
327 PyObject *args)
328{
329 const char *data;
330 int size;
331 const char *errors = NULL;
332
333 if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
334 &data, &size, &errors))
335 return NULL;
336
337 return codec_tuple(PyString_FromStringAndSize(data, size),
338 size);
339}
340
341static PyObject *
342charbuffer_encode(PyObject *self,
343 PyObject *args)
344{
345 const char *data;
346 int size;
347 const char *errors = NULL;
348
349 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
350 &data, &size, &errors))
351 return NULL;
352
353 return codec_tuple(PyString_FromStringAndSize(data, size),
354 size);
355}
356
357static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000358unicode_internal_encode(PyObject *self,
359 PyObject *args)
360{
361 PyObject *obj;
362 const char *errors = NULL;
363 const char *data;
364 int size;
365
366 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
367 &obj, &errors))
368 return NULL;
369
370 if (PyUnicode_Check(obj)) {
371 data = PyUnicode_AS_DATA(obj);
372 size = PyUnicode_GET_DATA_SIZE(obj);
373 return codec_tuple(PyString_FromStringAndSize(data, size),
374 size);
375 }
376 else {
377 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
378 return NULL;
379 return codec_tuple(PyString_FromStringAndSize(data, size),
380 size);
381 }
382}
383
384static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000385utf_8_encode(PyObject *self,
386 PyObject *args)
387{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000388 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000389 const char *errors = NULL;
390
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000391 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000392 &str, &errors))
393 return NULL;
394
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000395 str = PyUnicode_FromObject(str);
396 if (str == NULL)
397 return NULL;
398 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
399 PyUnicode_GET_SIZE(str),
400 errors),
401 PyUnicode_GET_SIZE(str));
402 Py_DECREF(str);
403 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000404}
405
406/* This version provides access to the byteorder parameter of the
407 builtin UTF-16 codecs as optional third argument. It defaults to 0
408 which means: use the native byte order and prepend the data with a
409 BOM mark.
410
411*/
412
413static PyObject *
414utf_16_encode(PyObject *self,
415 PyObject *args)
416{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000417 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000418 const char *errors = NULL;
419 int byteorder = 0;
420
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000421 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000422 &str, &errors, &byteorder))
423 return NULL;
424
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000425 str = PyUnicode_FromObject(str);
426 if (str == NULL)
427 return NULL;
428 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
429 PyUnicode_GET_SIZE(str),
430 errors,
431 byteorder),
432 PyUnicode_GET_SIZE(str));
433 Py_DECREF(str);
434 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000435}
436
437static PyObject *
438utf_16_le_encode(PyObject *self,
439 PyObject *args)
440{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000441 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000442 const char *errors = NULL;
443
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000444 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000445 &str, &errors))
446 return NULL;
447
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000448 str = PyUnicode_FromObject(str);
449 if (str == NULL)
450 return NULL;
451 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000452 PyUnicode_GET_SIZE(str),
453 errors,
454 -1),
455 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000456 Py_DECREF(str);
457 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000458}
459
460static PyObject *
461utf_16_be_encode(PyObject *self,
462 PyObject *args)
463{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000464 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000465 const char *errors = NULL;
466
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000467 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000468 &str, &errors))
469 return NULL;
470
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000471 str = PyUnicode_FromObject(str);
472 if (str == NULL)
473 return NULL;
474 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
475 PyUnicode_GET_SIZE(str),
476 errors,
477 +1),
478 PyUnicode_GET_SIZE(str));
479 Py_DECREF(str);
480 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000481}
482
483static PyObject *
484unicode_escape_encode(PyObject *self,
485 PyObject *args)
486{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000487 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000488 const char *errors = NULL;
489
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000490 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000491 &str, &errors))
492 return NULL;
493
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000494 str = PyUnicode_FromObject(str);
495 if (str == NULL)
496 return NULL;
497 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
498 PyUnicode_GET_SIZE(str)),
499 PyUnicode_GET_SIZE(str));
500 Py_DECREF(str);
501 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000502}
503
504static PyObject *
505raw_unicode_escape_encode(PyObject *self,
506 PyObject *args)
507{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000508 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000509 const char *errors = NULL;
510
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000511 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000512 &str, &errors))
513 return NULL;
514
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000515 str = PyUnicode_FromObject(str);
516 if (str == NULL)
517 return NULL;
518 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000519 PyUnicode_AS_UNICODE(str),
520 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000521 PyUnicode_GET_SIZE(str));
522 Py_DECREF(str);
523 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000524}
525
526static PyObject *
527latin_1_encode(PyObject *self,
528 PyObject *args)
529{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000530 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000531 const char *errors = NULL;
532
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000533 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000534 &str, &errors))
535 return NULL;
536
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000537 str = PyUnicode_FromObject(str);
538 if (str == NULL)
539 return NULL;
540 v = codec_tuple(PyUnicode_EncodeLatin1(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000541 PyUnicode_AS_UNICODE(str),
542 PyUnicode_GET_SIZE(str),
543 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000544 PyUnicode_GET_SIZE(str));
545 Py_DECREF(str);
546 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000547}
548
549static PyObject *
550ascii_encode(PyObject *self,
551 PyObject *args)
552{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000553 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000554 const char *errors = NULL;
555
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000556 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000557 &str, &errors))
558 return NULL;
559
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000560 str = PyUnicode_FromObject(str);
561 if (str == NULL)
562 return NULL;
563 v = codec_tuple(PyUnicode_EncodeASCII(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000564 PyUnicode_AS_UNICODE(str),
565 PyUnicode_GET_SIZE(str),
566 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000567 PyUnicode_GET_SIZE(str));
568 Py_DECREF(str);
569 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000570}
571
572static PyObject *
573charmap_encode(PyObject *self,
574 PyObject *args)
575{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000576 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000577 const char *errors = NULL;
578 PyObject *mapping = NULL;
579
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000580 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000581 &str, &errors, &mapping))
582 return NULL;
583 if (mapping == Py_None)
584 mapping = NULL;
585
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000586 str = PyUnicode_FromObject(str);
587 if (str == NULL)
588 return NULL;
589 v = codec_tuple(PyUnicode_EncodeCharmap(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000590 PyUnicode_AS_UNICODE(str),
591 PyUnicode_GET_SIZE(str),
592 mapping,
593 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000594 PyUnicode_GET_SIZE(str));
595 Py_DECREF(str);
596 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000597}
598
Fredrik Lundh30831632001-06-26 15:11:00 +0000599#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000600
601static PyObject *
602mbcs_encode(PyObject *self,
603 PyObject *args)
604{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000605 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000606 const char *errors = NULL;
607
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000608 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000609 &str, &errors))
610 return NULL;
611
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000612 str = PyUnicode_FromObject(str);
613 if (str == NULL)
614 return NULL;
615 v = codec_tuple(PyUnicode_EncodeMBCS(
Guido van Rossum24bdb042000-03-28 20:29:59 +0000616 PyUnicode_AS_UNICODE(str),
617 PyUnicode_GET_SIZE(str),
618 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000619 PyUnicode_GET_SIZE(str));
620 Py_DECREF(str);
621 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000622}
623
624#endif /* MS_WIN32 */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000625#endif /* Py_USING_UNICODE */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000626
Guido van Rossume2d67f92000-03-10 23:09:23 +0000627/* --- Module API --------------------------------------------------------- */
628
629static PyMethodDef _codecs_functions[] = {
630 {"register", codecregister, 1},
631 {"lookup", codeclookup, 1},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000632#ifdef Py_USING_UNICODE
Guido van Rossume2d67f92000-03-10 23:09:23 +0000633 {"utf_8_encode", utf_8_encode, 1},
634 {"utf_8_decode", utf_8_decode, 1},
635 {"utf_16_encode", utf_16_encode, 1},
636 {"utf_16_le_encode", utf_16_le_encode, 1},
637 {"utf_16_be_encode", utf_16_be_encode, 1},
638 {"utf_16_decode", utf_16_decode, 1},
639 {"utf_16_le_decode", utf_16_le_decode, 1},
640 {"utf_16_be_decode", utf_16_be_decode, 1},
641 {"utf_16_ex_decode", utf_16_ex_decode, 1},
642 {"unicode_escape_encode", unicode_escape_encode, 1},
643 {"unicode_escape_decode", unicode_escape_decode, 1},
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000644 {"unicode_internal_encode", unicode_internal_encode, 1},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000645 {"unicode_internal_decode", unicode_internal_decode, 1},
646 {"raw_unicode_escape_encode", raw_unicode_escape_encode, 1},
647 {"raw_unicode_escape_decode", raw_unicode_escape_decode, 1},
648 {"latin_1_encode", latin_1_encode, 1},
649 {"latin_1_decode", latin_1_decode, 1},
650 {"ascii_encode", ascii_encode, 1},
651 {"ascii_decode", ascii_decode, 1},
652 {"charmap_encode", charmap_encode, 1},
653 {"charmap_decode", charmap_decode, 1},
654 {"readbuffer_encode", readbuffer_encode, 1},
655 {"charbuffer_encode", charbuffer_encode, 1},
Fredrik Lundh30831632001-06-26 15:11:00 +0000656#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000657 {"mbcs_encode", mbcs_encode, 1},
658 {"mbcs_decode", mbcs_decode, 1},
659#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000660#endif /* Py_USING_UNICODE */
Guido van Rossume2d67f92000-03-10 23:09:23 +0000661 {NULL, NULL} /* sentinel */
662};
663
664DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000665init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000666{
667 Py_InitModule("_codecs", _codecs_functions);
668}