+++ /dev/null
-/* ------------------------------------------------------------------------\r
-\r
- _codecs -- Provides access to the codec registry and the builtin\r
- codecs.\r
-\r
- This module should never be imported directly. The standard library\r
- module "codecs" wraps this builtin module for use within Python.\r
-\r
- The codec registry is accessible via:\r
-\r
- register(search_function) -> None\r
-\r
- lookup(encoding) -> CodecInfo object\r
-\r
- The builtin Unicode codecs use the following interface:\r
-\r
- <encoding>_encode(Unicode_object[,errors='strict']) ->\r
- (string object, bytes consumed)\r
-\r
- <encoding>_decode(char_buffer_obj[,errors='strict']) ->\r
- (Unicode object, bytes consumed)\r
-\r
- <encoding>_encode() interfaces also accept non-Unicode object as\r
- input. The objects are then converted to Unicode using\r
- PyUnicode_FromObject() prior to applying the conversion.\r
-\r
- These <encoding>s are available: utf_8, unicode_escape,\r
- raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),\r
- mbcs (on win32).\r
-\r
-\r
-Written by Marc-Andre Lemburg (mal@lemburg.com).\r
-\r
-Copyright (c) Corporation for National Research Initiatives.\r
-\r
- ------------------------------------------------------------------------ */\r
-\r
-#define PY_SSIZE_T_CLEAN\r
-#include "Python.h"\r
-\r
-/* --- Registry ----------------------------------------------------------- */\r
-\r
-PyDoc_STRVAR(register__doc__,\r
-"register(search_function)\n\\r
-\n\\r
-Register a codec search function. Search functions are expected to take\n\\r
-one argument, the encoding name in all lower case letters, and return\n\\r
-a tuple of functions (encoder, decoder, stream_reader, stream_writer)\n\\r
-(or a CodecInfo object).");\r
-\r
-static\r
-PyObject *codec_register(PyObject *self, PyObject *search_function)\r
-{\r
- if (PyCodec_Register(search_function))\r
- return NULL;\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(lookup__doc__,\r
-"lookup(encoding) -> CodecInfo\n\\r
-\n\\r
-Looks up a codec tuple in the Python codec registry and returns\n\\r
-a CodecInfo object.");\r
-\r
-static\r
-PyObject *codec_lookup(PyObject *self, PyObject *args)\r
-{\r
- char *encoding;\r
-\r
- if (!PyArg_ParseTuple(args, "s:lookup", &encoding))\r
- return NULL;\r
-\r
- return _PyCodec_Lookup(encoding);\r
-}\r
-\r
-PyDoc_STRVAR(encode__doc__,\r
-"encode(obj, [encoding[,errors]]) -> object\n\\r
-\n\\r
-Encodes obj using the codec registered for encoding. encoding defaults\n\\r
-to the default encoding. errors may be given to set a different error\n\\r
-handling scheme. Default is 'strict' meaning that encoding errors raise\n\\r
-a ValueError. Other possible values are 'ignore', 'replace' and\n\\r
-'xmlcharrefreplace' as well as any other name registered with\n\\r
-codecs.register_error that can handle ValueErrors.");\r
-\r
-static PyObject *\r
-codec_encode(PyObject *self, PyObject *args)\r
-{\r
- const char *encoding = NULL;\r
- const char *errors = NULL;\r
- PyObject *v;\r
-\r
- if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))\r
- return NULL;\r
-\r
-#ifdef Py_USING_UNICODE\r
- if (encoding == NULL)\r
- encoding = PyUnicode_GetDefaultEncoding();\r
-#else\r
- if (encoding == NULL) {\r
- PyErr_SetString(PyExc_ValueError, "no encoding specified");\r
- return NULL;\r
- }\r
-#endif\r
-\r
- /* Encode via the codec registry */\r
- return PyCodec_Encode(v, encoding, errors);\r
-}\r
-\r
-PyDoc_STRVAR(decode__doc__,\r
-"decode(obj, [encoding[,errors]]) -> object\n\\r
-\n\\r
-Decodes obj using the codec registered for encoding. encoding defaults\n\\r
-to the default encoding. errors may be given to set a different error\n\\r
-handling scheme. Default is 'strict' meaning that encoding errors raise\n\\r
-a ValueError. Other possible values are 'ignore' and 'replace'\n\\r
-as well as any other name registered with codecs.register_error that is\n\\r
-able to handle ValueErrors.");\r
-\r
-static PyObject *\r
-codec_decode(PyObject *self, PyObject *args)\r
-{\r
- const char *encoding = NULL;\r
- const char *errors = NULL;\r
- PyObject *v;\r
-\r
- if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))\r
- return NULL;\r
-\r
-#ifdef Py_USING_UNICODE\r
- if (encoding == NULL)\r
- encoding = PyUnicode_GetDefaultEncoding();\r
-#else\r
- if (encoding == NULL) {\r
- PyErr_SetString(PyExc_ValueError, "no encoding specified");\r
- return NULL;\r
- }\r
-#endif\r
-\r
- /* Decode via the codec registry */\r
- return PyCodec_Decode(v, encoding, errors);\r
-}\r
-\r
-/* --- Helpers ------------------------------------------------------------ */\r
-\r
-static\r
-PyObject *codec_tuple(PyObject *unicode,\r
- Py_ssize_t len)\r
-{\r
- PyObject *v;\r
- if (unicode == NULL)\r
- return NULL;\r
- v = Py_BuildValue("On", unicode, len);\r
- Py_DECREF(unicode);\r
- return v;\r
-}\r
-\r
-/* --- String codecs ------------------------------------------------------ */\r
-static PyObject *\r
-escape_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- const char *errors = NULL;\r
- const char *data;\r
- Py_ssize_t size;\r
-\r
- if (!PyArg_ParseTuple(args, "s#|z:escape_decode",\r
- &data, &size, &errors))\r
- return NULL;\r
- return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL),\r
- size);\r
-}\r
-\r
-static PyObject *\r
-escape_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str;\r
- const char *errors = NULL;\r
- char *buf;\r
- Py_ssize_t consumed, len;\r
-\r
- if (!PyArg_ParseTuple(args, "S|z:escape_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- consumed = PyString_GET_SIZE(str);\r
- str = PyString_Repr(str, 0);\r
- if (!str)\r
- return NULL;\r
-\r
- /* The string will be quoted. Unquote, similar to unicode-escape. */\r
- buf = PyString_AS_STRING (str);\r
- len = PyString_GET_SIZE (str);\r
- memmove(buf, buf+1, len-2);\r
- if (_PyString_Resize(&str, len-2) < 0)\r
- return NULL;\r
-\r
- return codec_tuple(str, consumed);\r
-}\r
-\r
-#ifdef Py_USING_UNICODE\r
-/* --- Decoder ------------------------------------------------------------ */\r
-\r
-static PyObject *\r
-unicode_internal_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *obj;\r
- const char *errors = NULL;\r
- const char *data;\r
- Py_ssize_t size;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",\r
- &obj, &errors))\r
- return NULL;\r
-\r
- if (PyUnicode_Check(obj)) {\r
- Py_INCREF(obj);\r
- return codec_tuple(obj, PyUnicode_GET_SIZE(obj));\r
- }\r
- else {\r
- if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))\r
- return NULL;\r
-\r
- return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),\r
- size);\r
- }\r
-}\r
-\r
-static PyObject *\r
-utf_7_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_7_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len;\r
-\r
- decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,\r
- final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_8_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_8_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len;\r
-\r
- decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,\r
- final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_16_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_16_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_16_le_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = -1;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_16_le_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
-\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_16_be_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 1;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_16_be_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
-\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-/* This non-standard version also provides access to the byteorder\r
- parameter of the builtin UTF-16 codec.\r
-\r
- It returns a tuple (unicode, bytesread, byteorder) with byteorder\r
- being the value in effect at the end of data.\r
-\r
-*/\r
-\r
-static PyObject *\r
-utf_16_ex_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
- PyObject *unicode, *tuple;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zii:utf_16_ex_decode",\r
- &pbuf, &errors, &byteorder, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (unicode == NULL)\r
- return NULL;\r
- tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);\r
- Py_DECREF(unicode);\r
- return tuple;\r
-}\r
-\r
-static PyObject *\r
-utf_32_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_32_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_32_le_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = -1;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_32_le_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-static PyObject *\r
-utf_32_be_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 1;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:utf_32_be_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-/* This non-standard version also provides access to the byteorder\r
- parameter of the builtin UTF-32 codec.\r
-\r
- It returns a tuple (unicode, bytesread, byteorder) with byteorder\r
- being the value in effect at the end of data.\r
-\r
-*/\r
-\r
-static PyObject *\r
-utf_32_ex_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
- PyObject *unicode, *tuple;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zii:utf_32_ex_decode",\r
- &pbuf, &errors, &byteorder, &final))\r
- return NULL;\r
- consumed = pbuf.len; /* This is overwritten unless final is true. */\r
- unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,\r
- &byteorder, final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (unicode == NULL)\r
- return NULL;\r
- tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);\r
- Py_DECREF(unicode);\r
- return tuple;\r
-}\r
-\r
-static PyObject *\r
-unicode_escape_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- PyObject *unicode;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",\r
- &pbuf, &errors))\r
- return NULL;\r
-\r
- unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);\r
- PyBuffer_Release(&pbuf);\r
- return codec_tuple(unicode, pbuf.len);\r
-}\r
-\r
-static PyObject *\r
-raw_unicode_escape_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- PyObject *unicode;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",\r
- &pbuf, &errors))\r
- return NULL;\r
-\r
- unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);\r
- PyBuffer_Release(&pbuf);\r
- return codec_tuple(unicode, pbuf.len);\r
-}\r
-\r
-static PyObject *\r
-latin_1_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- PyObject *unicode;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|z:latin_1_decode",\r
- &pbuf, &errors))\r
- return NULL;\r
-\r
- unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);\r
- PyBuffer_Release(&pbuf);\r
- return codec_tuple(unicode, pbuf.len);\r
-}\r
-\r
-static PyObject *\r
-ascii_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- PyObject *unicode;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|z:ascii_decode",\r
- &pbuf, &errors))\r
- return NULL;\r
-\r
- unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);\r
- PyBuffer_Release(&pbuf);\r
- return codec_tuple(unicode, pbuf.len);\r
-}\r
-\r
-static PyObject *\r
-charmap_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- PyObject *unicode;\r
- const char *errors = NULL;\r
- PyObject *mapping = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zO:charmap_decode",\r
- &pbuf, &errors, &mapping))\r
- return NULL;\r
- if (mapping == Py_None)\r
- mapping = NULL;\r
-\r
- unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);\r
- PyBuffer_Release(&pbuf);\r
- return codec_tuple(unicode, pbuf.len);\r
-}\r
-\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
-\r
-static PyObject *\r
-mbcs_decode(PyObject *self,\r
- PyObject *args)\r
-{\r
- Py_buffer pbuf;\r
- const char *errors = NULL;\r
- int final = 0;\r
- Py_ssize_t consumed;\r
- PyObject *decoded = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s*|zi:mbcs_decode",\r
- &pbuf, &errors, &final))\r
- return NULL;\r
- consumed = pbuf.len;\r
-\r
- decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,\r
- final ? NULL : &consumed);\r
- PyBuffer_Release(&pbuf);\r
- if (decoded == NULL)\r
- return NULL;\r
- return codec_tuple(decoded, consumed);\r
-}\r
-\r
-#endif /* MS_WINDOWS */\r
-\r
-/* --- Encoder ------------------------------------------------------------ */\r
-\r
-static PyObject *\r
-readbuffer_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- const char *data;\r
- Py_ssize_t size;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",\r
- &data, &size, &errors))\r
- return NULL;\r
-\r
- return codec_tuple(PyString_FromStringAndSize(data, size),\r
- size);\r
-}\r
-\r
-static PyObject *\r
-charbuffer_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- const char *data;\r
- Py_ssize_t size;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",\r
- &data, &size, &errors))\r
- return NULL;\r
-\r
- return codec_tuple(PyString_FromStringAndSize(data, size),\r
- size);\r
-}\r
-\r
-static PyObject *\r
-unicode_internal_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *obj;\r
- const char *errors = NULL;\r
- const char *data;\r
- Py_ssize_t size;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",\r
- &obj, &errors))\r
- return NULL;\r
-\r
- if (PyUnicode_Check(obj)) {\r
- data = PyUnicode_AS_DATA(obj);\r
- size = PyUnicode_GET_DATA_SIZE(obj);\r
- return codec_tuple(PyString_FromStringAndSize(data, size),\r
- PyUnicode_GET_SIZE(obj));\r
- }\r
- else {\r
- if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))\r
- return NULL;\r
- return codec_tuple(PyString_FromStringAndSize(data, size),\r
- size);\r
- }\r
-}\r
-\r
-static PyObject *\r
-utf_7_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- 0,\r
- 0,\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-utf_8_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-/* This version provides access to the byteorder parameter of the\r
- builtin UTF-16 codecs as optional third argument. It defaults to 0\r
- which means: use the native byte order and prepend the data with a\r
- BOM mark.\r
-\r
-*/\r
-\r
-static PyObject *\r
-utf_16_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
-\r
- if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",\r
- &str, &errors, &byteorder))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- byteorder),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-utf_16_le_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- -1),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-utf_16_be_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- +1),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-/* This version provides access to the byteorder parameter of the\r
- builtin UTF-32 codecs as optional third argument. It defaults to 0\r
- which means: use the native byte order and prepend the data with a\r
- BOM mark.\r
-\r
-*/\r
-\r
-static PyObject *\r
-utf_32_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
- int byteorder = 0;\r
-\r
- if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode",\r
- &str, &errors, &byteorder))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- byteorder),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-utf_32_le_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- -1),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-utf_32_be_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors,\r
- +1),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-unicode_escape_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str)),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-raw_unicode_escape_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(\r
- PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str)),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-latin_1_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeLatin1(\r
- PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-ascii_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:ascii_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeASCII(\r
- PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject *\r
-charmap_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
- PyObject *mapping = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",\r
- &str, &errors, &mapping))\r
- return NULL;\r
- if (mapping == Py_None)\r
- mapping = NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeCharmap(\r
- PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- mapping,\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-static PyObject*\r
-charmap_build(PyObject *self, PyObject *args)\r
-{\r
- PyObject *map;\r
- if (!PyArg_ParseTuple(args, "U:charmap_build", &map))\r
- return NULL;\r
- return PyUnicode_BuildEncodingMap(map);\r
-}\r
-\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
-\r
-static PyObject *\r
-mbcs_encode(PyObject *self,\r
- PyObject *args)\r
-{\r
- PyObject *str, *v;\r
- const char *errors = NULL;\r
-\r
- if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",\r
- &str, &errors))\r
- return NULL;\r
-\r
- str = PyUnicode_FromObject(str);\r
- if (str == NULL)\r
- return NULL;\r
- v = codec_tuple(PyUnicode_EncodeMBCS(\r
- PyUnicode_AS_UNICODE(str),\r
- PyUnicode_GET_SIZE(str),\r
- errors),\r
- PyUnicode_GET_SIZE(str));\r
- Py_DECREF(str);\r
- return v;\r
-}\r
-\r
-#endif /* MS_WINDOWS */\r
-#endif /* Py_USING_UNICODE */\r
-\r
-/* --- Error handler registry --------------------------------------------- */\r
-\r
-PyDoc_STRVAR(register_error__doc__,\r
-"register_error(errors, handler)\n\\r
-\n\\r
-Register the specified error handler under the name\n\\r
-errors. handler must be a callable object, that\n\\r
-will be called with an exception instance containing\n\\r
-information about the location of the encoding/decoding\n\\r
-error and must return a (replacement, new position) tuple.");\r
-\r
-static PyObject *register_error(PyObject *self, PyObject *args)\r
-{\r
- const char *name;\r
- PyObject *handler;\r
-\r
- if (!PyArg_ParseTuple(args, "sO:register_error",\r
- &name, &handler))\r
- return NULL;\r
- if (PyCodec_RegisterError(name, handler))\r
- return NULL;\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(lookup_error__doc__,\r
-"lookup_error(errors) -> handler\n\\r
-\n\\r
-Return the error handler for the specified error handling name\n\\r
-or raise a LookupError, if no handler exists under this name.");\r
-\r
-static PyObject *lookup_error(PyObject *self, PyObject *args)\r
-{\r
- const char *name;\r
-\r
- if (!PyArg_ParseTuple(args, "s:lookup_error",\r
- &name))\r
- return NULL;\r
- return PyCodec_LookupError(name);\r
-}\r
-\r
-/* --- Module API --------------------------------------------------------- */\r
-\r
-static PyMethodDef _codecs_functions[] = {\r
- {"register", codec_register, METH_O,\r
- register__doc__},\r
- {"lookup", codec_lookup, METH_VARARGS,\r
- lookup__doc__},\r
- {"encode", codec_encode, METH_VARARGS,\r
- encode__doc__},\r
- {"decode", codec_decode, METH_VARARGS,\r
- decode__doc__},\r
- {"escape_encode", escape_encode, METH_VARARGS},\r
- {"escape_decode", escape_decode, METH_VARARGS},\r
-#ifdef Py_USING_UNICODE\r
- {"utf_8_encode", utf_8_encode, METH_VARARGS},\r
- {"utf_8_decode", utf_8_decode, METH_VARARGS},\r
- {"utf_7_encode", utf_7_encode, METH_VARARGS},\r
- {"utf_7_decode", utf_7_decode, METH_VARARGS},\r
- {"utf_16_encode", utf_16_encode, METH_VARARGS},\r
- {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},\r
- {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},\r
- {"utf_16_decode", utf_16_decode, METH_VARARGS},\r
- {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},\r
- {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},\r
- {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},\r
- {"utf_32_encode", utf_32_encode, METH_VARARGS},\r
- {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS},\r
- {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS},\r
- {"utf_32_decode", utf_32_decode, METH_VARARGS},\r
- {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS},\r
- {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS},\r
- {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS},\r
- {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},\r
- {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},\r
- {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},\r
- {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},\r
- {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},\r
- {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},\r
- {"latin_1_encode", latin_1_encode, METH_VARARGS},\r
- {"latin_1_decode", latin_1_decode, METH_VARARGS},\r
- {"ascii_encode", ascii_encode, METH_VARARGS},\r
- {"ascii_decode", ascii_decode, METH_VARARGS},\r
- {"charmap_encode", charmap_encode, METH_VARARGS},\r
- {"charmap_decode", charmap_decode, METH_VARARGS},\r
- {"charmap_build", charmap_build, METH_VARARGS},\r
- {"readbuffer_encode", readbuffer_encode, METH_VARARGS},\r
- {"charbuffer_encode", charbuffer_encode, METH_VARARGS},\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
- {"mbcs_encode", mbcs_encode, METH_VARARGS},\r
- {"mbcs_decode", mbcs_decode, METH_VARARGS},\r
-#endif\r
-#endif /* Py_USING_UNICODE */\r
- {"register_error", register_error, METH_VARARGS,\r
- register_error__doc__},\r
- {"lookup_error", lookup_error, METH_VARARGS,\r
- lookup_error__doc__},\r
- {NULL, NULL} /* sentinel */\r
-};\r
-\r
-PyMODINIT_FUNC\r
-init_codecs(void)\r
-{\r
- Py_InitModule("_codecs", _codecs_functions);\r
-}\r