1 /* ------------------------------------------------------------------------
3 _codecs -- Provides access to the codec registry and the builtin
6 This module should never be imported directly. The standard library
7 module "codecs" wraps this builtin module for use within Python.
9 The codec registry is accessible via:
11 register(search_function) -> None
13 lookup(encoding) -> CodecInfo object
15 The builtin Unicode codecs use the following interface:
17 <encoding>_encode(Unicode_object[,errors='strict']) ->
18 (string object, bytes consumed)
20 <encoding>_decode(char_buffer_obj[,errors='strict']) ->
21 (Unicode object, bytes consumed)
23 <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.
27 These <encoding>s are available: utf_8, unicode_escape,
28 raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
32 Written by Marc-Andre Lemburg (mal@lemburg.com).
34 Copyright (c) Corporation for National Research Initiatives.
36 ------------------------------------------------------------------------ */
38 #define PY_SSIZE_T_CLEAN
41 /* --- Registry ----------------------------------------------------------- */
43 PyDoc_STRVAR(register__doc__
,
44 "register(search_function)\n\
46 Register a codec search function. Search functions are expected to take\n\
47 one argument, the encoding name in all lower case letters, and return\n\
48 a tuple of functions (encoder, decoder, stream_reader, stream_writer)\n\
49 (or a CodecInfo object).");
52 PyObject
*codec_register(PyObject
*self
, PyObject
*search_function
)
54 if (PyCodec_Register(search_function
))
60 PyDoc_STRVAR(lookup__doc__
,
61 "lookup(encoding) -> CodecInfo\n\
63 Looks up a codec tuple in the Python codec registry and returns\n\
64 a CodecInfo object.");
67 PyObject
*codec_lookup(PyObject
*self
, PyObject
*args
)
71 if (!PyArg_ParseTuple(args
, "s:lookup", &encoding
))
74 return _PyCodec_Lookup(encoding
);
77 PyDoc_STRVAR(encode__doc__
,
78 "encode(obj, [encoding[,errors]]) -> object\n\
80 Encodes obj using the codec registered for encoding. encoding defaults\n\
81 to the default encoding. errors may be given to set a different error\n\
82 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
83 a ValueError. Other possible values are 'ignore', 'replace' and\n\
84 'xmlcharrefreplace' as well as any other name registered with\n\
85 codecs.register_error that can handle ValueErrors.");
88 codec_encode(PyObject
*self
, PyObject
*args
)
90 const char *encoding
= NULL
;
91 const char *errors
= NULL
;
94 if (!PyArg_ParseTuple(args
, "O|ss:encode", &v
, &encoding
, &errors
))
98 encoding
= PyUnicode_GetDefaultEncoding();
100 /* Encode via the codec registry */
101 return PyCodec_Encode(v
, encoding
, errors
);
104 PyDoc_STRVAR(decode__doc__
,
105 "decode(obj, [encoding[,errors]]) -> object\n\
107 Decodes obj using the codec registered for encoding. encoding defaults\n\
108 to the default encoding. errors may be given to set a different error\n\
109 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
110 a ValueError. Other possible values are 'ignore' and 'replace'\n\
111 as well as any other name registered with codecs.register_error that is\n\
112 able to handle ValueErrors.");
115 codec_decode(PyObject
*self
, PyObject
*args
)
117 const char *encoding
= NULL
;
118 const char *errors
= NULL
;
121 if (!PyArg_ParseTuple(args
, "O|ss:decode", &v
, &encoding
, &errors
))
124 if (encoding
== NULL
)
125 encoding
= PyUnicode_GetDefaultEncoding();
127 /* Decode via the codec registry */
128 return PyCodec_Decode(v
, encoding
, errors
);
131 /* --- Helpers ------------------------------------------------------------ */
134 PyObject
*codec_tuple(PyObject
*unicode
,
140 v
= Py_BuildValue("On", unicode
, len
);
145 /* --- String codecs ------------------------------------------------------ */
147 escape_decode(PyObject
*self
,
150 const char *errors
= NULL
;
154 if (!PyArg_ParseTuple(args
, "s#|z:escape_decode",
155 &data
, &size
, &errors
))
157 return codec_tuple(PyBytes_DecodeEscape(data
, size
, errors
, 0, NULL
),
162 escape_encode(PyObject
*self
,
165 static const char *hexdigits
= "0123456789abcdef";
169 const char *errors
= NULL
;
172 if (!PyArg_ParseTuple(args
, "O!|z:escape_encode",
173 &PyBytes_Type
, &str
, &errors
))
176 size
= PyBytes_GET_SIZE(str
);
178 if (newsize
> PY_SSIZE_T_MAX
|| newsize
/ 4 != size
) {
179 PyErr_SetString(PyExc_OverflowError
,
180 "string is too large to encode");
183 v
= PyBytes_FromStringAndSize(NULL
, newsize
);
189 register Py_ssize_t i
;
191 register char *p
= PyBytes_AS_STRING(v
);
193 for (i
= 0; i
< size
; i
++) {
194 /* There's at least enough room for a hex escape */
195 assert(newsize
- (p
- PyBytes_AS_STRING(v
)) >= 4);
196 c
= PyBytes_AS_STRING(str
)[i
];
197 if (c
== '\'' || c
== '\\')
198 *p
++ = '\\', *p
++ = c
;
200 *p
++ = '\\', *p
++ = 't';
202 *p
++ = '\\', *p
++ = 'n';
204 *p
++ = '\\', *p
++ = 'r';
205 else if (c
< ' ' || c
>= 0x7f) {
208 *p
++ = hexdigits
[(c
& 0xf0) >> 4];
209 *p
++ = hexdigits
[c
& 0xf];
215 if (_PyBytes_Resize(&v
, (p
- PyBytes_AS_STRING(v
)))) {
220 return codec_tuple(v
, PyBytes_Size(v
));
223 /* --- Decoder ------------------------------------------------------------ */
226 unicode_internal_decode(PyObject
*self
,
230 const char *errors
= NULL
;
234 if (!PyArg_ParseTuple(args
, "O|z:unicode_internal_decode",
238 if (PyUnicode_Check(obj
)) {
240 return codec_tuple(obj
, PyUnicode_GET_SIZE(obj
));
243 if (PyObject_AsReadBuffer(obj
, (const void **)&data
, &size
))
246 return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data
, size
, errors
),
252 utf_7_decode(PyObject
*self
,
256 const char *errors
= NULL
;
259 PyObject
*decoded
= NULL
;
261 if (!PyArg_ParseTuple(args
, "y*|zi:utf_7_decode",
262 &pbuf
, &errors
, &final
))
266 decoded
= PyUnicode_DecodeUTF7Stateful(pbuf
.buf
, pbuf
.len
, errors
,
267 final
? NULL
: &consumed
);
268 PyBuffer_Release(&pbuf
);
271 return codec_tuple(decoded
, consumed
);
275 utf_8_decode(PyObject
*self
,
279 const char *errors
= NULL
;
282 PyObject
*decoded
= NULL
;
284 if (!PyArg_ParseTuple(args
, "y*|zi:utf_8_decode",
285 &pbuf
, &errors
, &final
))
289 decoded
= PyUnicode_DecodeUTF8Stateful(pbuf
.buf
, pbuf
.len
, errors
,
290 final
? NULL
: &consumed
);
291 PyBuffer_Release(&pbuf
);
294 return codec_tuple(decoded
, consumed
);
298 utf_16_decode(PyObject
*self
,
302 const char *errors
= NULL
;
308 if (!PyArg_ParseTuple(args
, "y*|zi:utf_16_decode",
309 &pbuf
, &errors
, &final
))
311 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
312 decoded
= PyUnicode_DecodeUTF16Stateful(pbuf
.buf
, pbuf
.len
, errors
,
313 &byteorder
, final
? NULL
: &consumed
);
314 PyBuffer_Release(&pbuf
);
317 return codec_tuple(decoded
, consumed
);
321 utf_16_le_decode(PyObject
*self
,
325 const char *errors
= NULL
;
329 PyObject
*decoded
= NULL
;
331 if (!PyArg_ParseTuple(args
, "y*|zi:utf_16_le_decode",
332 &pbuf
, &errors
, &final
))
335 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
336 decoded
= PyUnicode_DecodeUTF16Stateful(pbuf
.buf
, pbuf
.len
, errors
,
337 &byteorder
, final
? NULL
: &consumed
);
338 PyBuffer_Release(&pbuf
);
341 return codec_tuple(decoded
, consumed
);
345 utf_16_be_decode(PyObject
*self
,
349 const char *errors
= NULL
;
353 PyObject
*decoded
= NULL
;
355 if (!PyArg_ParseTuple(args
, "y*|zi:utf_16_be_decode",
356 &pbuf
, &errors
, &final
))
359 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
360 decoded
= PyUnicode_DecodeUTF16Stateful(pbuf
.buf
, pbuf
.len
, errors
,
361 &byteorder
, final
? NULL
: &consumed
);
362 PyBuffer_Release(&pbuf
);
365 return codec_tuple(decoded
, consumed
);
368 /* This non-standard version also provides access to the byteorder
369 parameter of the builtin UTF-16 codec.
371 It returns a tuple (unicode, bytesread, byteorder) with byteorder
372 being the value in effect at the end of data.
377 utf_16_ex_decode(PyObject
*self
,
381 const char *errors
= NULL
;
383 PyObject
*unicode
, *tuple
;
387 if (!PyArg_ParseTuple(args
, "y*|zii:utf_16_ex_decode",
388 &pbuf
, &errors
, &byteorder
, &final
))
390 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
391 unicode
= PyUnicode_DecodeUTF16Stateful(pbuf
.buf
, pbuf
.len
, errors
,
392 &byteorder
, final
? NULL
: &consumed
);
393 PyBuffer_Release(&pbuf
);
396 tuple
= Py_BuildValue("Oni", unicode
, consumed
, byteorder
);
402 utf_32_decode(PyObject
*self
,
406 const char *errors
= NULL
;
412 if (!PyArg_ParseTuple(args
, "y*|zi:utf_32_decode",
413 &pbuf
, &errors
, &final
))
415 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
416 decoded
= PyUnicode_DecodeUTF32Stateful(pbuf
.buf
, pbuf
.len
, errors
,
417 &byteorder
, final
? NULL
: &consumed
);
418 PyBuffer_Release(&pbuf
);
421 return codec_tuple(decoded
, consumed
);
425 utf_32_le_decode(PyObject
*self
,
429 const char *errors
= NULL
;
435 if (!PyArg_ParseTuple(args
, "y*|zi:utf_32_le_decode",
436 &pbuf
, &errors
, &final
))
438 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
439 decoded
= PyUnicode_DecodeUTF32Stateful(pbuf
.buf
, pbuf
.len
, errors
,
440 &byteorder
, final
? NULL
: &consumed
);
441 PyBuffer_Release(&pbuf
);
444 return codec_tuple(decoded
, consumed
);
448 utf_32_be_decode(PyObject
*self
,
452 const char *errors
= NULL
;
458 if (!PyArg_ParseTuple(args
, "y*|zi:utf_32_be_decode",
459 &pbuf
, &errors
, &final
))
461 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
462 decoded
= PyUnicode_DecodeUTF32Stateful(pbuf
.buf
, pbuf
.len
, errors
,
463 &byteorder
, final
? NULL
: &consumed
);
464 PyBuffer_Release(&pbuf
);
467 return codec_tuple(decoded
, consumed
);
470 /* This non-standard version also provides access to the byteorder
471 parameter of the builtin UTF-32 codec.
473 It returns a tuple (unicode, bytesread, byteorder) with byteorder
474 being the value in effect at the end of data.
479 utf_32_ex_decode(PyObject
*self
,
483 const char *errors
= NULL
;
485 PyObject
*unicode
, *tuple
;
489 if (!PyArg_ParseTuple(args
, "y*|zii:utf_32_ex_decode",
490 &pbuf
, &errors
, &byteorder
, &final
))
492 consumed
= pbuf
.len
; /* This is overwritten unless final is true. */
493 unicode
= PyUnicode_DecodeUTF32Stateful(pbuf
.buf
, pbuf
.len
, errors
,
494 &byteorder
, final
? NULL
: &consumed
);
495 PyBuffer_Release(&pbuf
);
498 tuple
= Py_BuildValue("Oni", unicode
, consumed
, byteorder
);
504 unicode_escape_decode(PyObject
*self
,
508 const char *errors
= NULL
;
511 if (!PyArg_ParseTuple(args
, "s*|z:unicode_escape_decode",
515 unicode
= PyUnicode_DecodeUnicodeEscape(pbuf
.buf
, pbuf
.len
, errors
);
516 PyBuffer_Release(&pbuf
);
517 return codec_tuple(unicode
, pbuf
.len
);
521 raw_unicode_escape_decode(PyObject
*self
,
525 const char *errors
= NULL
;
528 if (!PyArg_ParseTuple(args
, "s*|z:raw_unicode_escape_decode",
532 unicode
= PyUnicode_DecodeRawUnicodeEscape(pbuf
.buf
, pbuf
.len
, errors
);
533 PyBuffer_Release(&pbuf
);
534 return codec_tuple(unicode
, pbuf
.len
);
538 latin_1_decode(PyObject
*self
,
543 const char *errors
= NULL
;
545 if (!PyArg_ParseTuple(args
, "y*|z:latin_1_decode",
549 unicode
= PyUnicode_DecodeLatin1(pbuf
.buf
, pbuf
.len
, errors
);
550 PyBuffer_Release(&pbuf
);
551 return codec_tuple(unicode
, pbuf
.len
);
555 ascii_decode(PyObject
*self
,
560 const char *errors
= NULL
;
562 if (!PyArg_ParseTuple(args
, "y*|z:ascii_decode",
566 unicode
= PyUnicode_DecodeASCII(pbuf
.buf
, pbuf
.len
, errors
);
567 PyBuffer_Release(&pbuf
);
568 return codec_tuple(unicode
, pbuf
.len
);
572 charmap_decode(PyObject
*self
,
577 const char *errors
= NULL
;
578 PyObject
*mapping
= NULL
;
580 if (!PyArg_ParseTuple(args
, "y*|zO:charmap_decode",
581 &pbuf
, &errors
, &mapping
))
583 if (mapping
== Py_None
)
586 unicode
= PyUnicode_DecodeCharmap(pbuf
.buf
, pbuf
.len
, mapping
, errors
);
587 PyBuffer_Release(&pbuf
);
588 return codec_tuple(unicode
, pbuf
.len
);
591 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
594 mbcs_decode(PyObject
*self
,
598 const char *errors
= NULL
;
601 PyObject
*decoded
= NULL
;
603 if (!PyArg_ParseTuple(args
, "y*|zi:mbcs_decode",
604 &pbuf
, &errors
, &final
))
608 decoded
= PyUnicode_DecodeMBCSStateful(pbuf
.buf
, pbuf
.len
, errors
,
609 final
? NULL
: &consumed
);
610 PyBuffer_Release(&pbuf
);
613 return codec_tuple(decoded
, consumed
);
616 #endif /* MS_WINDOWS */
618 /* --- Encoder ------------------------------------------------------------ */
621 readbuffer_encode(PyObject
*self
,
627 const char *errors
= NULL
;
630 if (!PyArg_ParseTuple(args
, "s*|z:readbuffer_encode",
636 result
= PyBytes_FromStringAndSize(data
, size
);
637 PyBuffer_Release(&pdata
);
638 return codec_tuple(result
, size
);
642 charbuffer_encode(PyObject
*self
,
647 const char *errors
= NULL
;
649 if (!PyArg_ParseTuple(args
, "t#|z:charbuffer_encode",
650 &data
, &size
, &errors
))
653 return codec_tuple(PyBytes_FromStringAndSize(data
, size
), size
);
657 unicode_internal_encode(PyObject
*self
,
661 const char *errors
= NULL
;
665 if (!PyArg_ParseTuple(args
, "O|z:unicode_internal_encode",
669 if (PyUnicode_Check(obj
)) {
670 data
= PyUnicode_AS_DATA(obj
);
671 size
= PyUnicode_GET_DATA_SIZE(obj
);
672 return codec_tuple(PyBytes_FromStringAndSize(data
, size
),
673 PyUnicode_GET_SIZE(obj
));
676 if (PyObject_AsReadBuffer(obj
, (const void **)&data
, &size
))
678 return codec_tuple(PyBytes_FromStringAndSize(data
, size
), size
);
683 utf_7_encode(PyObject
*self
,
687 const char *errors
= NULL
;
689 if (!PyArg_ParseTuple(args
, "O|z:utf_7_encode",
693 str
= PyUnicode_FromObject(str
);
696 v
= codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str
),
697 PyUnicode_GET_SIZE(str
),
701 PyUnicode_GET_SIZE(str
));
707 utf_8_encode(PyObject
*self
,
711 const char *errors
= NULL
;
713 if (!PyArg_ParseTuple(args
, "O|z:utf_8_encode",
717 str
= PyUnicode_FromObject(str
);
720 v
= codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str
),
721 PyUnicode_GET_SIZE(str
),
723 PyUnicode_GET_SIZE(str
));
728 /* This version provides access to the byteorder parameter of the
729 builtin UTF-16 codecs as optional third argument. It defaults to 0
730 which means: use the native byte order and prepend the data with a
736 utf_16_encode(PyObject
*self
,
740 const char *errors
= NULL
;
743 if (!PyArg_ParseTuple(args
, "O|zi:utf_16_encode",
744 &str
, &errors
, &byteorder
))
747 str
= PyUnicode_FromObject(str
);
750 v
= codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str
),
751 PyUnicode_GET_SIZE(str
),
754 PyUnicode_GET_SIZE(str
));
760 utf_16_le_encode(PyObject
*self
,
764 const char *errors
= NULL
;
766 if (!PyArg_ParseTuple(args
, "O|z:utf_16_le_encode",
770 str
= PyUnicode_FromObject(str
);
773 v
= codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str
),
774 PyUnicode_GET_SIZE(str
),
777 PyUnicode_GET_SIZE(str
));
783 utf_16_be_encode(PyObject
*self
,
787 const char *errors
= NULL
;
789 if (!PyArg_ParseTuple(args
, "O|z:utf_16_be_encode",
793 str
= PyUnicode_FromObject(str
);
796 v
= codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str
),
797 PyUnicode_GET_SIZE(str
),
800 PyUnicode_GET_SIZE(str
));
805 /* This version provides access to the byteorder parameter of the
806 builtin UTF-32 codecs as optional third argument. It defaults to 0
807 which means: use the native byte order and prepend the data with a
813 utf_32_encode(PyObject
*self
,
817 const char *errors
= NULL
;
820 if (!PyArg_ParseTuple(args
, "O|zi:utf_32_encode",
821 &str
, &errors
, &byteorder
))
824 str
= PyUnicode_FromObject(str
);
827 v
= codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str
),
828 PyUnicode_GET_SIZE(str
),
831 PyUnicode_GET_SIZE(str
));
837 utf_32_le_encode(PyObject
*self
,
841 const char *errors
= NULL
;
843 if (!PyArg_ParseTuple(args
, "O|z:utf_32_le_encode",
847 str
= PyUnicode_FromObject(str
);
850 v
= codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str
),
851 PyUnicode_GET_SIZE(str
),
854 PyUnicode_GET_SIZE(str
));
860 utf_32_be_encode(PyObject
*self
,
864 const char *errors
= NULL
;
866 if (!PyArg_ParseTuple(args
, "O|z:utf_32_be_encode",
870 str
= PyUnicode_FromObject(str
);
873 v
= codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str
),
874 PyUnicode_GET_SIZE(str
),
877 PyUnicode_GET_SIZE(str
));
883 unicode_escape_encode(PyObject
*self
,
887 const char *errors
= NULL
;
889 if (!PyArg_ParseTuple(args
, "O|z:unicode_escape_encode",
893 str
= PyUnicode_FromObject(str
);
896 v
= codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str
),
897 PyUnicode_GET_SIZE(str
)),
898 PyUnicode_GET_SIZE(str
));
904 raw_unicode_escape_encode(PyObject
*self
,
908 const char *errors
= NULL
;
910 if (!PyArg_ParseTuple(args
, "O|z:raw_unicode_escape_encode",
914 str
= PyUnicode_FromObject(str
);
917 v
= codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
918 PyUnicode_AS_UNICODE(str
),
919 PyUnicode_GET_SIZE(str
)),
920 PyUnicode_GET_SIZE(str
));
926 latin_1_encode(PyObject
*self
,
930 const char *errors
= NULL
;
932 if (!PyArg_ParseTuple(args
, "O|z:latin_1_encode",
936 str
= PyUnicode_FromObject(str
);
939 v
= codec_tuple(PyUnicode_EncodeLatin1(
940 PyUnicode_AS_UNICODE(str
),
941 PyUnicode_GET_SIZE(str
),
943 PyUnicode_GET_SIZE(str
));
949 ascii_encode(PyObject
*self
,
953 const char *errors
= NULL
;
955 if (!PyArg_ParseTuple(args
, "O|z:ascii_encode",
959 str
= PyUnicode_FromObject(str
);
962 v
= codec_tuple(PyUnicode_EncodeASCII(
963 PyUnicode_AS_UNICODE(str
),
964 PyUnicode_GET_SIZE(str
),
966 PyUnicode_GET_SIZE(str
));
972 charmap_encode(PyObject
*self
,
976 const char *errors
= NULL
;
977 PyObject
*mapping
= NULL
;
979 if (!PyArg_ParseTuple(args
, "O|zO:charmap_encode",
980 &str
, &errors
, &mapping
))
982 if (mapping
== Py_None
)
985 str
= PyUnicode_FromObject(str
);
988 v
= codec_tuple(PyUnicode_EncodeCharmap(
989 PyUnicode_AS_UNICODE(str
),
990 PyUnicode_GET_SIZE(str
),
993 PyUnicode_GET_SIZE(str
));
999 charmap_build(PyObject
*self
, PyObject
*args
)
1002 if (!PyArg_ParseTuple(args
, "U:charmap_build", &map
))
1004 return PyUnicode_BuildEncodingMap(map
);
1007 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
1010 mbcs_encode(PyObject
*self
,
1014 const char *errors
= NULL
;
1016 if (!PyArg_ParseTuple(args
, "O|z:mbcs_encode",
1020 str
= PyUnicode_FromObject(str
);
1023 v
= codec_tuple(PyUnicode_EncodeMBCS(
1024 PyUnicode_AS_UNICODE(str
),
1025 PyUnicode_GET_SIZE(str
),
1027 PyUnicode_GET_SIZE(str
));
1032 #endif /* MS_WINDOWS */
1034 /* --- Error handler registry --------------------------------------------- */
1036 PyDoc_STRVAR(register_error__doc__
,
1037 "register_error(errors, handler)\n\
1039 Register the specified error handler under the name\n\
1040 errors. handler must be a callable object, that\n\
1041 will be called with an exception instance containing\n\
1042 information about the location of the encoding/decoding\n\
1043 error and must return a (replacement, new position) tuple.");
1045 static PyObject
*register_error(PyObject
*self
, PyObject
*args
)
1050 if (!PyArg_ParseTuple(args
, "sO:register_error",
1053 if (PyCodec_RegisterError(name
, handler
))
1058 PyDoc_STRVAR(lookup_error__doc__
,
1059 "lookup_error(errors) -> handler\n\
1061 Return the error handler for the specified error handling name\n\
1062 or raise a LookupError, if no handler exists under this name.");
1064 static PyObject
*lookup_error(PyObject
*self
, PyObject
*args
)
1068 if (!PyArg_ParseTuple(args
, "s:lookup_error",
1071 return PyCodec_LookupError(name
);
1074 /* --- Module API --------------------------------------------------------- */
1076 static PyMethodDef _codecs_functions
[] = {
1077 {"register", codec_register
, METH_O
,
1079 {"lookup", codec_lookup
, METH_VARARGS
,
1081 {"encode", codec_encode
, METH_VARARGS
,
1083 {"decode", codec_decode
, METH_VARARGS
,
1085 {"escape_encode", escape_encode
, METH_VARARGS
},
1086 {"escape_decode", escape_decode
, METH_VARARGS
},
1087 {"utf_8_encode", utf_8_encode
, METH_VARARGS
},
1088 {"utf_8_decode", utf_8_decode
, METH_VARARGS
},
1089 {"utf_7_encode", utf_7_encode
, METH_VARARGS
},
1090 {"utf_7_decode", utf_7_decode
, METH_VARARGS
},
1091 {"utf_16_encode", utf_16_encode
, METH_VARARGS
},
1092 {"utf_16_le_encode", utf_16_le_encode
, METH_VARARGS
},
1093 {"utf_16_be_encode", utf_16_be_encode
, METH_VARARGS
},
1094 {"utf_16_decode", utf_16_decode
, METH_VARARGS
},
1095 {"utf_16_le_decode", utf_16_le_decode
, METH_VARARGS
},
1096 {"utf_16_be_decode", utf_16_be_decode
, METH_VARARGS
},
1097 {"utf_16_ex_decode", utf_16_ex_decode
, METH_VARARGS
},
1098 {"utf_32_encode", utf_32_encode
, METH_VARARGS
},
1099 {"utf_32_le_encode", utf_32_le_encode
, METH_VARARGS
},
1100 {"utf_32_be_encode", utf_32_be_encode
, METH_VARARGS
},
1101 {"utf_32_decode", utf_32_decode
, METH_VARARGS
},
1102 {"utf_32_le_decode", utf_32_le_decode
, METH_VARARGS
},
1103 {"utf_32_be_decode", utf_32_be_decode
, METH_VARARGS
},
1104 {"utf_32_ex_decode", utf_32_ex_decode
, METH_VARARGS
},
1105 {"unicode_escape_encode", unicode_escape_encode
, METH_VARARGS
},
1106 {"unicode_escape_decode", unicode_escape_decode
, METH_VARARGS
},
1107 {"unicode_internal_encode", unicode_internal_encode
, METH_VARARGS
},
1108 {"unicode_internal_decode", unicode_internal_decode
, METH_VARARGS
},
1109 {"raw_unicode_escape_encode", raw_unicode_escape_encode
, METH_VARARGS
},
1110 {"raw_unicode_escape_decode", raw_unicode_escape_decode
, METH_VARARGS
},
1111 {"latin_1_encode", latin_1_encode
, METH_VARARGS
},
1112 {"latin_1_decode", latin_1_decode
, METH_VARARGS
},
1113 {"ascii_encode", ascii_encode
, METH_VARARGS
},
1114 {"ascii_decode", ascii_decode
, METH_VARARGS
},
1115 {"charmap_encode", charmap_encode
, METH_VARARGS
},
1116 {"charmap_decode", charmap_decode
, METH_VARARGS
},
1117 {"charmap_build", charmap_build
, METH_VARARGS
},
1118 {"readbuffer_encode", readbuffer_encode
, METH_VARARGS
},
1119 {"charbuffer_encode", charbuffer_encode
, METH_VARARGS
},
1120 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
1121 {"mbcs_encode", mbcs_encode
, METH_VARARGS
},
1122 {"mbcs_decode", mbcs_decode
, METH_VARARGS
},
1124 {"register_error", register_error
, METH_VARARGS
,
1125 register_error__doc__
},
1126 {"lookup_error", lookup_error
, METH_VARARGS
,
1127 lookup_error__doc__
},
1128 {NULL
, NULL
} /* sentinel */
1131 static struct PyModuleDef codecsmodule
= {
1132 PyModuleDef_HEAD_INIT
,
1144 PyInit__codecs(void)
1146 return PyModule_Create(&codecsmodule
);