/* ------------------------------------------------------------------------ _codecs -- Provides access to the codec registry and the builtin codecs. This module should never be imported directly. The standard library module "codecs" wraps this builtin module for use within Python. The codec registry is accessible via: register(search_function) -> None lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer) The builtin Unicode codecs use the following interface: <encoding>_encode(Unicode_object[,errors='strict']) -> (string object, bytes consumed) <encoding>_decode(char_buffer_obj[,errors='strict']) -> (Unicode object, bytes consumed) <encoding>_encode() interfaces also accept non-Unicode object as input. The objects are then converted to Unicode using PyUnicode_FromObject() prior to applying the conversion. These <encoding>s are available: utf_8, unicode_escape, raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit), mbcs (on win32). Written by Marc-Andre Lemburg (mal@lemburg.com). Copyright (c) Corporation for National Research Initiatives. ------------------------------------------------------------------------ */ #define PY_SSIZE_T_CLEAN#include "Python.h" /* --- Registry ----------------------------------------------------------- */ PyDoc_STRVAR(register__doc__,"register(search_function)\n\\n\Register a codec search function. Search functions are expected to take\n\one argument, the encoding name in all lower case letters, and return\n\a tuple of functions (encoder, decoder, stream_reader, stream_writer)."); staticPyObject *codec_register(PyObject *self, PyObject *search_function){ if (PyCodec_Register(search_function)) return NULL; Py_RETURN_NONE;} PyDoc_STRVAR(lookup__doc__,"lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)\n\\n\Looks up a codec tuple in the Python codec registry and returns\n\a tuple of functions."); staticPyObject *codec_lookup(PyObject *self, PyObject *args){ char *encoding; if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) return NULL; return _PyCodec_Lookup(encoding);} PyDoc_STRVAR(encode__doc__,"encode(obj, [encoding[,errors]]) -> object\n\\n\Encodes obj using the codec registered for encoding. encoding defaults\n\to the default encoding. errors may be given to set a different error\n\handling scheme. Default is 'strict' meaning that encoding errors raise\n\a ValueError. Other possible values are 'ignore', 'replace' and\n\'xmlcharrefreplace' as well as any other name registered with\n\codecs.register_error that can handle ValueErrors."); static PyObject *codec_encode(PyObject *self, PyObject *args){ const char *encoding = NULL; const char *errors = NULL; PyObject *v; if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors)) return NULL; #ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding();#else if (encoding == NULL) { PyErr_SetString(PyExc_ValueError, "no encoding specified"); return NULL; }#endif /* Encode via the codec registry */ return PyCodec_Encode(v, encoding, errors);} PyDoc_STRVAR(decode__doc__,"decode(obj, [encoding[,errors]]) -> object\n\\n\Decodes obj using the codec registered for encoding. encoding defaults\n\to the default encoding. errors may be given to set a different error\n\handling scheme. Default is 'strict' meaning that encoding errors raise\n\a ValueError. Other possible values are 'ignore' and 'replace'\n\as well as any other name registerd with codecs.register_error that is\n\able to handle ValueErrors."); static PyObject *codec_decode(PyObject *self, PyObject *args){ const char *encoding = NULL; const char *errors = NULL; PyObject *v; if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors)) return NULL; #ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding();#else if (encoding == NULL) { PyErr_SetString(PyExc_ValueError, "no encoding specified"); return NULL; }#endif /* Decode via the codec registry */ return PyCodec_Decode(v, encoding, errors);} /* --- Helpers ------------------------------------------------------------ */ staticPyObject *codec_tuple(PyObject *unicode, Py_ssize_t len){ PyObject *v; if (unicode == NULL) return NULL; v = Py_BuildValue("On", unicode, len); Py_DECREF(unicode); return v;} /* --- String codecs ------------------------------------------------------ */static PyObject *escape_decode(PyObject *self, PyObject *args){ const char *errors = NULL; const char *data; Py_ssize_t size; if (!PyArg_ParseTuple(args, "s#|z:escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL), size);} static PyObject *escape_encode(PyObject *self, PyObject *args){ PyObject *str; const char *errors = NULL; char *buf; Py_ssize_t len; if (!PyArg_ParseTuple(args, "O!|z:escape_encode", &PyString_Type, &str, &errors)) return NULL; str = PyString_Repr(str, 0); if (!str) return NULL; /* The string will be quoted. Unquote, similar to unicode-escape. */ buf = PyString_AS_STRING (str); len = PyString_GET_SIZE (str); memmove(buf, buf+1, len-2); if (_PyString_Resize(&str, len-2) < 0) return NULL; return codec_tuple(str, PyString_Size(str));} #ifdef Py_USING_UNICODE/* --- Decoder ------------------------------------------------------------ */ static PyObject *unicode_internal_decode(PyObject *self, PyObject *args){ PyObject *obj; const char *errors = NULL; const char *data; Py_ssize_t size; if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", &obj, &errors)) return NULL; if (PyUnicode_Check(obj)) { Py_INCREF(obj); return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); } else { if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) return NULL; return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors), size); }} static PyObject *utf_7_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors), size);} static PyObject *utf_8_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed);} static PyObject *utf_16_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 0; int final = 0; Py_ssize_t consumed; PyObject *decoded; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed);} static PyObject *utf_16_le_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = -1; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed); } static PyObject *utf_16_be_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 1; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed);} /* This non-standard version also provides access to the byteorder parameter of the builtin UTF-16 codec. It returns a tuple (unicode, bytesread, byteorder) with byteorder being the value in effect at the end of data. */ static PyObject *utf_16_ex_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 0; PyObject *unicode, *tuple; int final = 0; Py_ssize_t consumed; if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode", &data, &size, &errors, &byteorder, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (unicode == NULL) return NULL; tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); Py_DECREF(unicode); return tuple;} static PyObject *unicode_escape_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), size);} static PyObject *raw_unicode_escape_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), size);} static PyObject *latin_1_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), size);} static PyObject *ascii_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:ascii_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), size);} static PyObject *charmap_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size; const char *errors = NULL; PyObject *mapping = NULL; if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", &data, &size, &errors, &mapping)) return NULL; if (mapping == Py_None) mapping = NULL; return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), size);} #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) static PyObject *mbcs_decode(PyObject *self, PyObject *args){ const char *data; Py_ssize_t size, consumed; const char *errors = NULL; int final = 0; PyObject *decoded; if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode", &data, &size, &errors, &final)) return NULL; decoded = PyUnicode_DecodeMBCSStateful( data, size, errors, final ? NULL : &consumed); if (!decoded) return NULL; return codec_tuple(decoded, final ? size : consumed);} #endif /* MS_WINDOWS */ Lines 1–500 of 934. Load more
/* ------------------------------------------------------------------------ _codecs -- Provides access to the codec registry and the builtin codecs. This module should never be imported directly. The standard library module "codecs" wraps this builtin module for use within Python. The codec registry is accessible via: register(search_function) -> None lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer) The builtin Unicode codecs use the following interface: <encoding>_encode(Unicode_object[,errors='strict']) -> (string object, bytes consumed) <encoding>_decode(char_buffer_obj[,errors='strict']) -> (Unicode object, bytes consumed) <encoding>_encode() interfaces also accept non-Unicode object as input. The objects are then converted to Unicode using PyUnicode_FromObject() prior to applying the conversion. These <encoding>s are available: utf_8, unicode_escape, raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit), mbcs (on win32). Written by Marc-Andre Lemburg (mal@lemburg.com). Copyright (c) Corporation for National Research Initiatives. ------------------------------------------------------------------------ */ #define PY_SSIZE_T_CLEAN #include "Python.h" /* --- Registry ----------------------------------------------------------- */ PyDoc_STRVAR(register__doc__, "register(search_function)\n\ \n\ Register a codec search function. Search functions are expected to take\n\ one argument, the encoding name in all lower case letters, and return\n\ a tuple of functions (encoder, decoder, stream_reader, stream_writer)."); static PyObject *codec_register(PyObject *self, PyObject *search_function) { if (PyCodec_Register(search_function)) return NULL; Py_RETURN_NONE; } PyDoc_STRVAR(lookup__doc__, "lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)\n\ \n\ Looks up a codec tuple in the Python codec registry and returns\n\ a tuple of functions."); static PyObject *codec_lookup(PyObject *self, PyObject *args) { char *encoding; if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) return NULL; return _PyCodec_Lookup(encoding); } PyDoc_STRVAR(encode__doc__, "encode(obj, [encoding[,errors]]) -> object\n\ \n\ Encodes obj using the codec registered for encoding. encoding defaults\n\ to the default encoding. errors may be given to set a different error\n\ handling scheme. Default is 'strict' meaning that encoding errors raise\n\ a ValueError. Other possible values are 'ignore', 'replace' and\n\ 'xmlcharrefreplace' as well as any other name registered with\n\ codecs.register_error that can handle ValueErrors."); static PyObject * codec_encode(PyObject *self, PyObject *args) { const char *encoding = NULL; const char *errors = NULL; PyObject *v; if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors)) return NULL; #ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding(); #else if (encoding == NULL) { PyErr_SetString(PyExc_ValueError, "no encoding specified"); return NULL; } #endif /* Encode via the codec registry */ return PyCodec_Encode(v, encoding, errors); } PyDoc_STRVAR(decode__doc__, "decode(obj, [encoding[,errors]]) -> object\n\ \n\ Decodes obj using the codec registered for encoding. encoding defaults\n\ to the default encoding. errors may be given to set a different error\n\ handling scheme. Default is 'strict' meaning that encoding errors raise\n\ a ValueError. Other possible values are 'ignore' and 'replace'\n\ as well as any other name registerd with codecs.register_error that is\n\ able to handle ValueErrors."); static PyObject * codec_decode(PyObject *self, PyObject *args) { const char *encoding = NULL; const char *errors = NULL; PyObject *v; if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors)) return NULL; #ifdef Py_USING_UNICODE if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding(); #else if (encoding == NULL) { PyErr_SetString(PyExc_ValueError, "no encoding specified"); return NULL; } #endif /* Decode via the codec registry */ return PyCodec_Decode(v, encoding, errors); } /* --- Helpers ------------------------------------------------------------ */ static PyObject *codec_tuple(PyObject *unicode, Py_ssize_t len) { PyObject *v; if (unicode == NULL) return NULL; v = Py_BuildValue("On", unicode, len); Py_DECREF(unicode); return v; } /* --- String codecs ------------------------------------------------------ */ static PyObject * escape_decode(PyObject *self, PyObject *args) { const char *errors = NULL; const char *data; Py_ssize_t size; if (!PyArg_ParseTuple(args, "s#|z:escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyString_DecodeEscape(data, size, errors, 0, NULL), size); } static PyObject * escape_encode(PyObject *self, PyObject *args) { PyObject *str; const char *errors = NULL; char *buf; Py_ssize_t len; if (!PyArg_ParseTuple(args, "O!|z:escape_encode", &PyString_Type, &str, &errors)) return NULL; str = PyString_Repr(str, 0); if (!str) return NULL; /* The string will be quoted. Unquote, similar to unicode-escape. */ buf = PyString_AS_STRING (str); len = PyString_GET_SIZE (str); memmove(buf, buf+1, len-2); if (_PyString_Resize(&str, len-2) < 0) return NULL; return codec_tuple(str, PyString_Size(str)); } #ifdef Py_USING_UNICODE /* --- Decoder ------------------------------------------------------------ */ static PyObject * unicode_internal_decode(PyObject *self, PyObject *args) { PyObject *obj; const char *errors = NULL; const char *data; Py_ssize_t size; if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", &obj, &errors)) return NULL; if (PyUnicode_Check(obj)) { Py_INCREF(obj); return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); } else { if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) return NULL; return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors), size); } } static PyObject * utf_7_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors), size); } static PyObject * utf_8_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed); } static PyObject * utf_16_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 0; int final = 0; Py_ssize_t consumed; PyObject *decoded; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed); } static PyObject * utf_16_le_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = -1; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed); } static PyObject * utf_16_be_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 1; int final = 0; Py_ssize_t consumed; PyObject *decoded = NULL; if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode", &data, &size, &errors, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (decoded == NULL) return NULL; return codec_tuple(decoded, consumed); } /* This non-standard version also provides access to the byteorder parameter of the builtin UTF-16 codec. It returns a tuple (unicode, bytesread, byteorder) with byteorder being the value in effect at the end of data. */ static PyObject * utf_16_ex_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; int byteorder = 0; PyObject *unicode, *tuple; int final = 0; Py_ssize_t consumed; if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode", &data, &size, &errors, &byteorder, &final)) return NULL; if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative argument"); return 0; } consumed = size; /* This is overwritten unless final is true. */ unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, final ? NULL : &consumed); if (unicode == NULL) return NULL; tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); Py_DECREF(unicode); return tuple; } static PyObject * unicode_escape_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), size); } static PyObject * raw_unicode_escape_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), size); } static PyObject * latin_1_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), size); } static PyObject * ascii_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; if (!PyArg_ParseTuple(args, "t#|z:ascii_decode", &data, &size, &errors)) return NULL; return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), size); } static PyObject * charmap_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size; const char *errors = NULL; PyObject *mapping = NULL; if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", &data, &size, &errors, &mapping)) return NULL; if (mapping == Py_None) mapping = NULL; return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), size); } #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) static PyObject * mbcs_decode(PyObject *self, PyObject *args) { const char *data; Py_ssize_t size, consumed; const char *errors = NULL; int final = 0; PyObject *decoded; if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode", &data, &size, &errors, &final)) return NULL; decoded = PyUnicode_DecodeMBCSStateful( data, size, errors, final ? NULL : &consumed); if (!decoded) return NULL; return codec_tuple(decoded, final ? size : consumed); } #endif /* MS_WINDOWS */