blob: 29b068687c770142bd3a97d943d71b7636dbb234 [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 *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000127utf_7_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_7_decode",
135 &data, &size, &errors))
136 return NULL;
137
138 return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors),
139 size);
140}
141
142static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000143utf_8_decode(PyObject *self,
144 PyObject *args)
145{
146 const char *data;
147 int size;
148 const char *errors = NULL;
149
150 if (!PyArg_ParseTuple(args, "t#|z:utf_8_decode",
151 &data, &size, &errors))
152 return NULL;
153
154 return codec_tuple(PyUnicode_DecodeUTF8(data, size, errors),
155 size);
156}
157
158static PyObject *
159utf_16_decode(PyObject *self,
160 PyObject *args)
161{
162 const char *data;
163 int size;
164 const char *errors = NULL;
165 int byteorder = 0;
166
167 if (!PyArg_ParseTuple(args, "t#|z:utf_16_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_le_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_le_decode",
184 &data, &size, &errors))
185 return NULL;
186 return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
187 size);
188}
189
190static PyObject *
191utf_16_be_decode(PyObject *self,
192 PyObject *args)
193{
194 const char *data;
195 int size;
196 const char *errors = NULL;
197 int byteorder = 1;
198
199 if (!PyArg_ParseTuple(args, "t#|z:utf_16_be_decode",
200 &data, &size, &errors))
201 return NULL;
202 return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
203 size);
204}
205
206/* This non-standard version also provides access to the byteorder
207 parameter of the builtin UTF-16 codec.
208
209 It returns a tuple (unicode, bytesread, byteorder) with byteorder
210 being the value in effect at the end of data.
211
212*/
213
214static PyObject *
215utf_16_ex_decode(PyObject *self,
216 PyObject *args)
217{
218 const char *data;
219 int size;
220 const char *errors = NULL;
221 int byteorder = 0;
222 PyObject *unicode, *tuple;
223
224 if (!PyArg_ParseTuple(args, "t#|zi:utf_16_ex_decode",
225 &data, &size, &errors, &byteorder))
226 return NULL;
227
228 unicode = PyUnicode_DecodeUTF16(data, size, errors, &byteorder);
229 if (unicode == NULL)
230 return NULL;
231 tuple = Py_BuildValue("Oii", unicode, size, byteorder);
232 Py_DECREF(unicode);
233 return tuple;
234}
235
236static PyObject *
237unicode_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:unicode_escape_decode",
245 &data, &size, &errors))
246 return NULL;
247
248 return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
249 size);
250}
251
252static PyObject *
253raw_unicode_escape_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:raw_unicode_escape_decode",
261 &data, &size, &errors))
262 return NULL;
263
264 return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
265 size);
266}
267
268static PyObject *
269latin_1_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:latin_1_decode",
277 &data, &size, &errors))
278 return NULL;
279
280 return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
281 size);
282}
283
284static PyObject *
285ascii_decode(PyObject *self,
286 PyObject *args)
287{
288 const char *data;
289 int size;
290 const char *errors = NULL;
291
292 if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
293 &data, &size, &errors))
294 return NULL;
295
296 return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
297 size);
298}
299
300static PyObject *
301charmap_decode(PyObject *self,
302 PyObject *args)
303{
304 const char *data;
305 int size;
306 const char *errors = NULL;
307 PyObject *mapping = NULL;
308
309 if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
310 &data, &size, &errors, &mapping))
311 return NULL;
312 if (mapping == Py_None)
313 mapping = NULL;
314
315 return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
316 size);
317}
318
Fredrik Lundh30831632001-06-26 15:11:00 +0000319#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000320
321static PyObject *
322mbcs_decode(PyObject *self,
323 PyObject *args)
324{
325 const char *data;
326 int size;
327 const char *errors = NULL;
328
329 if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode",
330 &data, &size, &errors))
331 return NULL;
332
333 return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors),
334 size);
335}
336
337#endif /* MS_WIN32 */
338
Guido van Rossume2d67f92000-03-10 23:09:23 +0000339/* --- Encoder ------------------------------------------------------------ */
340
341static PyObject *
342readbuffer_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, "s#|z:readbuffer_encode",
350 &data, &size, &errors))
351 return NULL;
352
353 return codec_tuple(PyString_FromStringAndSize(data, size),
354 size);
355}
356
357static PyObject *
358charbuffer_encode(PyObject *self,
359 PyObject *args)
360{
361 const char *data;
362 int size;
363 const char *errors = NULL;
364
365 if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
366 &data, &size, &errors))
367 return NULL;
368
369 return codec_tuple(PyString_FromStringAndSize(data, size),
370 size);
371}
372
373static PyObject *
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000374unicode_internal_encode(PyObject *self,
375 PyObject *args)
376{
377 PyObject *obj;
378 const char *errors = NULL;
379 const char *data;
380 int size;
381
382 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
383 &obj, &errors))
384 return NULL;
385
386 if (PyUnicode_Check(obj)) {
387 data = PyUnicode_AS_DATA(obj);
388 size = PyUnicode_GET_DATA_SIZE(obj);
389 return codec_tuple(PyString_FromStringAndSize(data, size),
390 size);
391 }
392 else {
393 if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
394 return NULL;
395 return codec_tuple(PyString_FromStringAndSize(data, size),
396 size);
397 }
398}
399
400static PyObject *
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000401utf_7_encode(PyObject *self,
402 PyObject *args)
403{
404 PyObject *str, *v;
405 const char *errors = NULL;
406
407 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
408 &str, &errors))
409 return NULL;
410
411 str = PyUnicode_FromObject(str);
412 if (str == NULL)
413 return NULL;
414 v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
415 PyUnicode_GET_SIZE(str),
416 0,
417 0,
418 errors),
419 PyUnicode_GET_SIZE(str));
420 Py_DECREF(str);
421 return v;
422}
423
424static PyObject *
Guido van Rossume2d67f92000-03-10 23:09:23 +0000425utf_8_encode(PyObject *self,
426 PyObject *args)
427{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000428 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000429 const char *errors = NULL;
430
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000431 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000432 &str, &errors))
433 return NULL;
434
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000435 str = PyUnicode_FromObject(str);
436 if (str == NULL)
437 return NULL;
438 v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
439 PyUnicode_GET_SIZE(str),
440 errors),
441 PyUnicode_GET_SIZE(str));
442 Py_DECREF(str);
443 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000444}
445
446/* This version provides access to the byteorder parameter of the
447 builtin UTF-16 codecs as optional third argument. It defaults to 0
448 which means: use the native byte order and prepend the data with a
449 BOM mark.
450
451*/
452
453static PyObject *
454utf_16_encode(PyObject *self,
455 PyObject *args)
456{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000457 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000458 const char *errors = NULL;
459 int byteorder = 0;
460
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000461 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000462 &str, &errors, &byteorder))
463 return NULL;
464
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000465 str = PyUnicode_FromObject(str);
466 if (str == NULL)
467 return NULL;
468 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
469 PyUnicode_GET_SIZE(str),
470 errors,
471 byteorder),
472 PyUnicode_GET_SIZE(str));
473 Py_DECREF(str);
474 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000475}
476
477static PyObject *
478utf_16_le_encode(PyObject *self,
479 PyObject *args)
480{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000481 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000482 const char *errors = NULL;
483
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000484 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000485 &str, &errors))
486 return NULL;
487
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000488 str = PyUnicode_FromObject(str);
489 if (str == NULL)
490 return NULL;
491 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
Guido van Rossume2d67f92000-03-10 23:09:23 +0000492 PyUnicode_GET_SIZE(str),
493 errors,
494 -1),
495 PyUnicode_GET_SIZE(str));
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000496 Py_DECREF(str);
497 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000498}
499
500static PyObject *
501utf_16_be_encode(PyObject *self,
502 PyObject *args)
503{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000504 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000505 const char *errors = NULL;
506
Marc-André Lemburg4157dd52001-06-17 18:32:36 +0000507 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000508 &str, &errors))
509 return NULL;
510
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000511 str = PyUnicode_FromObject(str);
512 if (str == NULL)
513 return NULL;
514 v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
515 PyUnicode_GET_SIZE(str),
516 errors,
517 +1),
518 PyUnicode_GET_SIZE(str));
519 Py_DECREF(str);
520 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000521}
522
523static PyObject *
524unicode_escape_encode(PyObject *self,
525 PyObject *args)
526{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000527 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000528 const char *errors = NULL;
529
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000530 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000531 &str, &errors))
532 return NULL;
533
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000534 str = PyUnicode_FromObject(str);
535 if (str == NULL)
536 return NULL;
537 v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
538 PyUnicode_GET_SIZE(str)),
539 PyUnicode_GET_SIZE(str));
540 Py_DECREF(str);
541 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000542}
543
544static PyObject *
545raw_unicode_escape_encode(PyObject *self,
546 PyObject *args)
547{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000548 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000549 const char *errors = NULL;
550
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000551 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000552 &str, &errors))
553 return NULL;
554
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000555 str = PyUnicode_FromObject(str);
556 if (str == NULL)
557 return NULL;
558 v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000559 PyUnicode_AS_UNICODE(str),
560 PyUnicode_GET_SIZE(str)),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000561 PyUnicode_GET_SIZE(str));
562 Py_DECREF(str);
563 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000564}
565
566static PyObject *
567latin_1_encode(PyObject *self,
568 PyObject *args)
569{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000570 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000571 const char *errors = NULL;
572
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000573 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000574 &str, &errors))
575 return NULL;
576
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000577 str = PyUnicode_FromObject(str);
578 if (str == NULL)
579 return NULL;
580 v = codec_tuple(PyUnicode_EncodeLatin1(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000581 PyUnicode_AS_UNICODE(str),
582 PyUnicode_GET_SIZE(str),
583 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000584 PyUnicode_GET_SIZE(str));
585 Py_DECREF(str);
586 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000587}
588
589static PyObject *
590ascii_encode(PyObject *self,
591 PyObject *args)
592{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000593 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000594 const char *errors = NULL;
595
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000596 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000597 &str, &errors))
598 return NULL;
599
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000600 str = PyUnicode_FromObject(str);
601 if (str == NULL)
602 return NULL;
603 v = codec_tuple(PyUnicode_EncodeASCII(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000604 PyUnicode_AS_UNICODE(str),
605 PyUnicode_GET_SIZE(str),
606 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000607 PyUnicode_GET_SIZE(str));
608 Py_DECREF(str);
609 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000610}
611
612static PyObject *
613charmap_encode(PyObject *self,
614 PyObject *args)
615{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000616 PyObject *str, *v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000617 const char *errors = NULL;
618 PyObject *mapping = NULL;
619
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000620 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
Guido van Rossume2d67f92000-03-10 23:09:23 +0000621 &str, &errors, &mapping))
622 return NULL;
623 if (mapping == Py_None)
624 mapping = NULL;
625
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000626 str = PyUnicode_FromObject(str);
627 if (str == NULL)
628 return NULL;
629 v = codec_tuple(PyUnicode_EncodeCharmap(
Guido van Rossume2d67f92000-03-10 23:09:23 +0000630 PyUnicode_AS_UNICODE(str),
631 PyUnicode_GET_SIZE(str),
632 mapping,
633 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000634 PyUnicode_GET_SIZE(str));
635 Py_DECREF(str);
636 return v;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000637}
638
Fredrik Lundh30831632001-06-26 15:11:00 +0000639#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000640
641static PyObject *
642mbcs_encode(PyObject *self,
643 PyObject *args)
644{
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000645 PyObject *str, *v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000646 const char *errors = NULL;
647
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000648 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum24bdb042000-03-28 20:29:59 +0000649 &str, &errors))
650 return NULL;
651
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000652 str = PyUnicode_FromObject(str);
653 if (str == NULL)
654 return NULL;
655 v = codec_tuple(PyUnicode_EncodeMBCS(
Guido van Rossum24bdb042000-03-28 20:29:59 +0000656 PyUnicode_AS_UNICODE(str),
657 PyUnicode_GET_SIZE(str),
658 errors),
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +0000659 PyUnicode_GET_SIZE(str));
660 Py_DECREF(str);
661 return v;
Guido van Rossum24bdb042000-03-28 20:29:59 +0000662}
663
664#endif /* MS_WIN32 */
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000665#endif /* Py_USING_UNICODE */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000666
Guido van Rossume2d67f92000-03-10 23:09:23 +0000667/* --- Module API --------------------------------------------------------- */
668
669static PyMethodDef _codecs_functions[] = {
670 {"register", codecregister, 1},
671 {"lookup", codeclookup, 1},
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000672#ifdef Py_USING_UNICODE
Guido van Rossume2d67f92000-03-10 23:09:23 +0000673 {"utf_8_encode", utf_8_encode, 1},
674 {"utf_8_decode", utf_8_decode, 1},
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000675 {"utf_7_encode", utf_7_encode, 1},
676 {"utf_7_decode", utf_7_decode, 1},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000677 {"utf_16_encode", utf_16_encode, 1},
678 {"utf_16_le_encode", utf_16_le_encode, 1},
679 {"utf_16_be_encode", utf_16_be_encode, 1},
680 {"utf_16_decode", utf_16_decode, 1},
681 {"utf_16_le_decode", utf_16_le_decode, 1},
682 {"utf_16_be_decode", utf_16_be_decode, 1},
683 {"utf_16_ex_decode", utf_16_ex_decode, 1},
684 {"unicode_escape_encode", unicode_escape_encode, 1},
685 {"unicode_escape_decode", unicode_escape_decode, 1},
Marc-André Lemburgb425f5e2000-09-21 21:09:45 +0000686 {"unicode_internal_encode", unicode_internal_encode, 1},
Guido van Rossume2d67f92000-03-10 23:09:23 +0000687 {"unicode_internal_decode", unicode_internal_decode, 1},
688 {"raw_unicode_escape_encode", raw_unicode_escape_encode, 1},
689 {"raw_unicode_escape_decode", raw_unicode_escape_decode, 1},
690 {"latin_1_encode", latin_1_encode, 1},
691 {"latin_1_decode", latin_1_decode, 1},
692 {"ascii_encode", ascii_encode, 1},
693 {"ascii_decode", ascii_decode, 1},
694 {"charmap_encode", charmap_encode, 1},
695 {"charmap_decode", charmap_decode, 1},
696 {"readbuffer_encode", readbuffer_encode, 1},
697 {"charbuffer_encode", charbuffer_encode, 1},
Fredrik Lundh30831632001-06-26 15:11:00 +0000698#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Guido van Rossum24bdb042000-03-28 20:29:59 +0000699 {"mbcs_encode", mbcs_encode, 1},
700 {"mbcs_decode", mbcs_decode, 1},
701#endif
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000702#endif /* Py_USING_UNICODE */
Guido van Rossume2d67f92000-03-10 23:09:23 +0000703 {NULL, NULL} /* sentinel */
704};
705
706DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000707init_codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000708{
709 Py_InitModule("_codecs", _codecs_functions);
710}