+++ /dev/null
-/* ------------------------------------------------------------------------\r
-\r
- Python Codec Registry and support functions\r
-\r
-Written by Marc-Andre Lemburg (mal@lemburg.com).\r
-\r
-Copyright (c) Corporation for National Research Initiatives.\r
-\r
- ------------------------------------------------------------------------ */\r
-\r
-#include "Python.h"\r
-#include <ctype.h>\r
-\r
-/* --- Codec Registry ----------------------------------------------------- */\r
-\r
-/* Import the standard encodings package which will register the first\r
- codec search function.\r
-\r
- This is done in a lazy way so that the Unicode implementation does\r
- not downgrade startup time of scripts not needing it.\r
-\r
- ImportErrors are silently ignored by this function. Only one try is\r
- made.\r
-\r
-*/\r
-\r
-static int _PyCodecRegistry_Init(void); /* Forward */\r
-\r
-int PyCodec_Register(PyObject *search_function)\r
-{\r
- PyInterpreterState *interp = PyThreadState_GET()->interp;\r
- if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())\r
- goto onError;\r
- if (search_function == NULL) {\r
- PyErr_BadArgument();\r
- goto onError;\r
- }\r
- if (!PyCallable_Check(search_function)) {\r
- PyErr_SetString(PyExc_TypeError, "argument must be callable");\r
- goto onError;\r
- }\r
- return PyList_Append(interp->codec_search_path, search_function);\r
-\r
- onError:\r
- return -1;\r
-}\r
-\r
-/* Convert a string to a normalized Python string: all characters are\r
- converted to lower case, spaces are replaced with underscores. */\r
-\r
-static\r
-PyObject *normalizestring(const char *string)\r
-{\r
- register size_t i;\r
- size_t len = strlen(string);\r
- char *p;\r
- PyObject *v;\r
-\r
- if (len > PY_SSIZE_T_MAX) {\r
- PyErr_SetString(PyExc_OverflowError, "string is too large");\r
- return NULL;\r
- }\r
-\r
- v = PyString_FromStringAndSize(NULL, len);\r
- if (v == NULL)\r
- return NULL;\r
- p = PyString_AS_STRING(v);\r
- for (i = 0; i < len; i++) {\r
- register char ch = string[i];\r
- if (ch == ' ')\r
- ch = '-';\r
- else\r
- ch = tolower(Py_CHARMASK(ch));\r
- p[i] = ch;\r
- }\r
- return v;\r
-}\r
-\r
-/* Lookup the given encoding and return a tuple providing the codec\r
- facilities.\r
-\r
- The encoding string is looked up converted to all lower-case\r
- characters. This makes encodings looked up through this mechanism\r
- effectively case-insensitive.\r
-\r
- If no codec is found, a LookupError is set and NULL returned.\r
-\r
- As side effect, this tries to load the encodings package, if not\r
- yet done. This is part of the lazy load strategy for the encodings\r
- package.\r
-\r
-*/\r
-\r
-PyObject *_PyCodec_Lookup(const char *encoding)\r
-{\r
- PyInterpreterState *interp;\r
- PyObject *result, *args = NULL, *v;\r
- Py_ssize_t i, len;\r
-\r
- if (encoding == NULL) {\r
- PyErr_BadArgument();\r
- goto onError;\r
- }\r
-\r
- interp = PyThreadState_GET()->interp;\r
- if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())\r
- goto onError;\r
-\r
- /* Convert the encoding to a normalized Python string: all\r
- characters are converted to lower case, spaces and hyphens are\r
- replaced with underscores. */\r
- v = normalizestring(encoding);\r
- if (v == NULL)\r
- goto onError;\r
- PyString_InternInPlace(&v);\r
-\r
- /* First, try to lookup the name in the registry dictionary */\r
- result = PyDict_GetItem(interp->codec_search_cache, v);\r
- if (result != NULL) {\r
- Py_INCREF(result);\r
- Py_DECREF(v);\r
- return result;\r
- }\r
-\r
- /* Next, scan the search functions in order of registration */\r
- args = PyTuple_New(1);\r
- if (args == NULL)\r
- goto onError;\r
- PyTuple_SET_ITEM(args,0,v);\r
-\r
- len = PyList_Size(interp->codec_search_path);\r
- if (len < 0)\r
- goto onError;\r
- if (len == 0) {\r
- PyErr_SetString(PyExc_LookupError,\r
- "no codec search functions registered: "\r
- "can't find encoding");\r
- goto onError;\r
- }\r
-\r
- for (i = 0; i < len; i++) {\r
- PyObject *func;\r
-\r
- func = PyList_GetItem(interp->codec_search_path, i);\r
- if (func == NULL)\r
- goto onError;\r
- result = PyEval_CallObject(func, args);\r
- if (result == NULL)\r
- goto onError;\r
- if (result == Py_None) {\r
- Py_DECREF(result);\r
- continue;\r
- }\r
- if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {\r
- PyErr_SetString(PyExc_TypeError,\r
- "codec search functions must return 4-tuples");\r
- Py_DECREF(result);\r
- goto onError;\r
- }\r
- break;\r
- }\r
- if (i == len) {\r
- /* XXX Perhaps we should cache misses too ? */\r
- PyErr_Format(PyExc_LookupError,\r
- "unknown encoding: %s", encoding);\r
- goto onError;\r
- }\r
-\r
- /* Cache and return the result */\r
- PyDict_SetItem(interp->codec_search_cache, v, result);\r
- Py_DECREF(args);\r
- return result;\r
-\r
- onError:\r
- Py_XDECREF(args);\r
- return NULL;\r
-}\r
-\r
-static\r
-PyObject *args_tuple(PyObject *object,\r
- const char *errors)\r
-{\r
- PyObject *args;\r
-\r
- args = PyTuple_New(1 + (errors != NULL));\r
- if (args == NULL)\r
- return NULL;\r
- Py_INCREF(object);\r
- PyTuple_SET_ITEM(args,0,object);\r
- if (errors) {\r
- PyObject *v;\r
-\r
- v = PyString_FromString(errors);\r
- if (v == NULL) {\r
- Py_DECREF(args);\r
- return NULL;\r
- }\r
- PyTuple_SET_ITEM(args, 1, v);\r
- }\r
- return args;\r
-}\r
-\r
-/* Helper function to get a codec item */\r
-\r
-static\r
-PyObject *codec_getitem(const char *encoding, int index)\r
-{\r
- PyObject *codecs;\r
- PyObject *v;\r
-\r
- codecs = _PyCodec_Lookup(encoding);\r
- if (codecs == NULL)\r
- return NULL;\r
- v = PyTuple_GET_ITEM(codecs, index);\r
- Py_DECREF(codecs);\r
- Py_INCREF(v);\r
- return v;\r
-}\r
-\r
-/* Helper function to create an incremental codec. */\r
-\r
-static\r
-PyObject *codec_getincrementalcodec(const char *encoding,\r
- const char *errors,\r
- const char *attrname)\r
-{\r
- PyObject *codecs, *ret, *inccodec;\r
-\r
- codecs = _PyCodec_Lookup(encoding);\r
- if (codecs == NULL)\r
- return NULL;\r
- inccodec = PyObject_GetAttrString(codecs, attrname);\r
- Py_DECREF(codecs);\r
- if (inccodec == NULL)\r
- return NULL;\r
- if (errors)\r
- ret = PyObject_CallFunction(inccodec, "s", errors);\r
- else\r
- ret = PyObject_CallFunction(inccodec, NULL);\r
- Py_DECREF(inccodec);\r
- return ret;\r
-}\r
-\r
-/* Helper function to create a stream codec. */\r
-\r
-static\r
-PyObject *codec_getstreamcodec(const char *encoding,\r
- PyObject *stream,\r
- const char *errors,\r
- const int index)\r
-{\r
- PyObject *codecs, *streamcodec, *codeccls;\r
-\r
- codecs = _PyCodec_Lookup(encoding);\r
- if (codecs == NULL)\r
- return NULL;\r
-\r
- codeccls = PyTuple_GET_ITEM(codecs, index);\r
- if (errors != NULL)\r
- streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);\r
- else\r
- streamcodec = PyObject_CallFunction(codeccls, "O", stream);\r
- Py_DECREF(codecs);\r
- return streamcodec;\r
-}\r
-\r
-/* Convenience APIs to query the Codec registry.\r
-\r
- All APIs return a codec object with incremented refcount.\r
-\r
- */\r
-\r
-PyObject *PyCodec_Encoder(const char *encoding)\r
-{\r
- return codec_getitem(encoding, 0);\r
-}\r
-\r
-PyObject *PyCodec_Decoder(const char *encoding)\r
-{\r
- return codec_getitem(encoding, 1);\r
-}\r
-\r
-PyObject *PyCodec_IncrementalEncoder(const char *encoding,\r
- const char *errors)\r
-{\r
- return codec_getincrementalcodec(encoding, errors, "incrementalencoder");\r
-}\r
-\r
-PyObject *PyCodec_IncrementalDecoder(const char *encoding,\r
- const char *errors)\r
-{\r
- return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");\r
-}\r
-\r
-PyObject *PyCodec_StreamReader(const char *encoding,\r
- PyObject *stream,\r
- const char *errors)\r
-{\r
- return codec_getstreamcodec(encoding, stream, errors, 2);\r
-}\r
-\r
-PyObject *PyCodec_StreamWriter(const char *encoding,\r
- PyObject *stream,\r
- const char *errors)\r
-{\r
- return codec_getstreamcodec(encoding, stream, errors, 3);\r
-}\r
-\r
-/* Encode an object (e.g. an Unicode object) using the given encoding\r
- and return the resulting encoded object (usually a Python string).\r
-\r
- errors is passed to the encoder factory as argument if non-NULL. */\r
-\r
-PyObject *PyCodec_Encode(PyObject *object,\r
- const char *encoding,\r
- const char *errors)\r
-{\r
- PyObject *encoder = NULL;\r
- PyObject *args = NULL, *result = NULL;\r
- PyObject *v;\r
-\r
- encoder = PyCodec_Encoder(encoding);\r
- if (encoder == NULL)\r
- goto onError;\r
-\r
- args = args_tuple(object, errors);\r
- if (args == NULL)\r
- goto onError;\r
-\r
- result = PyEval_CallObject(encoder,args);\r
- if (result == NULL)\r
- goto onError;\r
-\r
- if (!PyTuple_Check(result) ||\r
- PyTuple_GET_SIZE(result) != 2) {\r
- PyErr_SetString(PyExc_TypeError,\r
- "encoder must return a tuple (object,integer)");\r
- goto onError;\r
- }\r
- v = PyTuple_GET_ITEM(result,0);\r
- Py_INCREF(v);\r
- /* We don't check or use the second (integer) entry. */\r
-\r
- Py_DECREF(args);\r
- Py_DECREF(encoder);\r
- Py_DECREF(result);\r
- return v;\r
-\r
- onError:\r
- Py_XDECREF(result);\r
- Py_XDECREF(args);\r
- Py_XDECREF(encoder);\r
- return NULL;\r
-}\r
-\r
-/* Decode an object (usually a Python string) using the given encoding\r
- and return an equivalent object (e.g. an Unicode object).\r
-\r
- errors is passed to the decoder factory as argument if non-NULL. */\r
-\r
-PyObject *PyCodec_Decode(PyObject *object,\r
- const char *encoding,\r
- const char *errors)\r
-{\r
- PyObject *decoder = NULL;\r
- PyObject *args = NULL, *result = NULL;\r
- PyObject *v;\r
-\r
- decoder = PyCodec_Decoder(encoding);\r
- if (decoder == NULL)\r
- goto onError;\r
-\r
- args = args_tuple(object, errors);\r
- if (args == NULL)\r
- goto onError;\r
-\r
- result = PyEval_CallObject(decoder,args);\r
- if (result == NULL)\r
- goto onError;\r
- if (!PyTuple_Check(result) ||\r
- PyTuple_GET_SIZE(result) != 2) {\r
- PyErr_SetString(PyExc_TypeError,\r
- "decoder must return a tuple (object,integer)");\r
- goto onError;\r
- }\r
- v = PyTuple_GET_ITEM(result,0);\r
- Py_INCREF(v);\r
- /* We don't check or use the second (integer) entry. */\r
-\r
- Py_DECREF(args);\r
- Py_DECREF(decoder);\r
- Py_DECREF(result);\r
- return v;\r
-\r
- onError:\r
- Py_XDECREF(args);\r
- Py_XDECREF(decoder);\r
- Py_XDECREF(result);\r
- return NULL;\r
-}\r
-\r
-/* Register the error handling callback function error under the name\r
- name. This function will be called by the codec when it encounters\r
- an unencodable characters/undecodable bytes and doesn't know the\r
- callback name, when name is specified as the error parameter\r
- in the call to the encode/decode function.\r
- Return 0 on success, -1 on error */\r
-int PyCodec_RegisterError(const char *name, PyObject *error)\r
-{\r
- PyInterpreterState *interp = PyThreadState_GET()->interp;\r
- if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())\r
- return -1;\r
- if (!PyCallable_Check(error)) {\r
- PyErr_SetString(PyExc_TypeError, "handler must be callable");\r
- return -1;\r
- }\r
- return PyDict_SetItemString(interp->codec_error_registry,\r
- (char *)name, error);\r
-}\r
-\r
-/* Lookup the error handling callback function registered under the\r
- name error. As a special case NULL can be passed, in which case\r
- the error handling callback for strict encoding will be returned. */\r
-PyObject *PyCodec_LookupError(const char *name)\r
-{\r
- PyObject *handler = NULL;\r
-\r
- PyInterpreterState *interp = PyThreadState_GET()->interp;\r
- if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())\r
- return NULL;\r
-\r
- if (name==NULL)\r
- name = "strict";\r
- handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);\r
- if (!handler)\r
- PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);\r
- else\r
- Py_INCREF(handler);\r
- return handler;\r
-}\r
-\r
-static void wrong_exception_type(PyObject *exc)\r
-{\r
- PyObject *type = PyObject_GetAttrString(exc, "__class__");\r
- if (type != NULL) {\r
- PyObject *name = PyObject_GetAttrString(type, "__name__");\r
- Py_DECREF(type);\r
- if (name != NULL) {\r
- PyObject *string = PyObject_Str(name);\r
- Py_DECREF(name);\r
- if (string != NULL) {\r
- PyErr_Format(PyExc_TypeError,\r
- "don't know how to handle %.400s in error callback",\r
- PyString_AS_STRING(string));\r
- Py_DECREF(string);\r
- }\r
- }\r
- }\r
-}\r
-\r
-PyObject *PyCodec_StrictErrors(PyObject *exc)\r
-{\r
- if (PyExceptionInstance_Check(exc))\r
- PyErr_SetObject(PyExceptionInstance_Class(exc), exc);\r
- else\r
- PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");\r
- return NULL;\r
-}\r
-\r
-\r
-#ifdef Py_USING_UNICODE\r
-PyObject *PyCodec_IgnoreErrors(PyObject *exc)\r
-{\r
- Py_ssize_t end;\r
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {\r
- if (PyUnicodeEncodeError_GetEnd(exc, &end))\r
- return NULL;\r
- }\r
- else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {\r
- if (PyUnicodeDecodeError_GetEnd(exc, &end))\r
- return NULL;\r
- }\r
- else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {\r
- if (PyUnicodeTranslateError_GetEnd(exc, &end))\r
- return NULL;\r
- }\r
- else {\r
- wrong_exception_type(exc);\r
- return NULL;\r
- }\r
- /* ouch: passing NULL, 0, pos gives None instead of u'' */\r
- return Py_BuildValue("(u#n)", &end, 0, end);\r
-}\r
-\r
-\r
-PyObject *PyCodec_ReplaceErrors(PyObject *exc)\r
-{\r
- PyObject *restuple;\r
- Py_ssize_t start;\r
- Py_ssize_t end;\r
- Py_ssize_t i;\r
-\r
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {\r
- PyObject *res;\r
- Py_UNICODE *p;\r
- if (PyUnicodeEncodeError_GetStart(exc, &start))\r
- return NULL;\r
- if (PyUnicodeEncodeError_GetEnd(exc, &end))\r
- return NULL;\r
- res = PyUnicode_FromUnicode(NULL, end-start);\r
- if (res == NULL)\r
- return NULL;\r
- for (p = PyUnicode_AS_UNICODE(res), i = start;\r
- i<end; ++p, ++i)\r
- *p = '?';\r
- restuple = Py_BuildValue("(On)", res, end);\r
- Py_DECREF(res);\r
- return restuple;\r
- }\r
- else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {\r
- Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;\r
- if (PyUnicodeDecodeError_GetEnd(exc, &end))\r
- return NULL;\r
- return Py_BuildValue("(u#n)", &res, 1, end);\r
- }\r
- else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {\r
- PyObject *res;\r
- Py_UNICODE *p;\r
- if (PyUnicodeTranslateError_GetStart(exc, &start))\r
- return NULL;\r
- if (PyUnicodeTranslateError_GetEnd(exc, &end))\r
- return NULL;\r
- res = PyUnicode_FromUnicode(NULL, end-start);\r
- if (res == NULL)\r
- return NULL;\r
- for (p = PyUnicode_AS_UNICODE(res), i = start;\r
- i<end; ++p, ++i)\r
- *p = Py_UNICODE_REPLACEMENT_CHARACTER;\r
- restuple = Py_BuildValue("(On)", res, end);\r
- Py_DECREF(res);\r
- return restuple;\r
- }\r
- else {\r
- wrong_exception_type(exc);\r
- return NULL;\r
- }\r
-}\r
-\r
-PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)\r
-{\r
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {\r
- PyObject *restuple;\r
- PyObject *object;\r
- Py_ssize_t start;\r
- Py_ssize_t end;\r
- PyObject *res;\r
- Py_UNICODE *p;\r
- Py_UNICODE *startp;\r
- Py_UNICODE *outp;\r
- int ressize;\r
- if (PyUnicodeEncodeError_GetStart(exc, &start))\r
- return NULL;\r
- if (PyUnicodeEncodeError_GetEnd(exc, &end))\r
- return NULL;\r
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))\r
- return NULL;\r
- startp = PyUnicode_AS_UNICODE(object);\r
- for (p = startp+start, ressize = 0; p < startp+end; ++p) {\r
- if (*p<10)\r
- ressize += 2+1+1;\r
- else if (*p<100)\r
- ressize += 2+2+1;\r
- else if (*p<1000)\r
- ressize += 2+3+1;\r
- else if (*p<10000)\r
- ressize += 2+4+1;\r
-#ifndef Py_UNICODE_WIDE\r
- else\r
- ressize += 2+5+1;\r
-#else\r
- else if (*p<100000)\r
- ressize += 2+5+1;\r
- else if (*p<1000000)\r
- ressize += 2+6+1;\r
- else\r
- ressize += 2+7+1;\r
-#endif\r
- }\r
- /* allocate replacement */\r
- res = PyUnicode_FromUnicode(NULL, ressize);\r
- if (res == NULL) {\r
- Py_DECREF(object);\r
- return NULL;\r
- }\r
- /* generate replacement */\r
- for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);\r
- p < startp+end; ++p) {\r
- Py_UNICODE c = *p;\r
- int digits;\r
- int base;\r
- *outp++ = '&';\r
- *outp++ = '#';\r
- if (*p<10) {\r
- digits = 1;\r
- base = 1;\r
- }\r
- else if (*p<100) {\r
- digits = 2;\r
- base = 10;\r
- }\r
- else if (*p<1000) {\r
- digits = 3;\r
- base = 100;\r
- }\r
- else if (*p<10000) {\r
- digits = 4;\r
- base = 1000;\r
- }\r
-#ifndef Py_UNICODE_WIDE\r
- else {\r
- digits = 5;\r
- base = 10000;\r
- }\r
-#else\r
- else if (*p<100000) {\r
- digits = 5;\r
- base = 10000;\r
- }\r
- else if (*p<1000000) {\r
- digits = 6;\r
- base = 100000;\r
- }\r
- else {\r
- digits = 7;\r
- base = 1000000;\r
- }\r
-#endif\r
- while (digits-->0) {\r
- *outp++ = '0' + c/base;\r
- c %= base;\r
- base /= 10;\r
- }\r
- *outp++ = ';';\r
- }\r
- restuple = Py_BuildValue("(On)", res, end);\r
- Py_DECREF(res);\r
- Py_DECREF(object);\r
- return restuple;\r
- }\r
- else {\r
- wrong_exception_type(exc);\r
- return NULL;\r
- }\r
-}\r
-\r
-static Py_UNICODE hexdigits[] = {\r
- '0', '1', '2', '3', '4', '5', '6', '7',\r
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'\r
-};\r
-\r
-PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)\r
-{\r
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {\r
- PyObject *restuple;\r
- PyObject *object;\r
- Py_ssize_t start;\r
- Py_ssize_t end;\r
- PyObject *res;\r
- Py_UNICODE *p;\r
- Py_UNICODE *startp;\r
- Py_UNICODE *outp;\r
- int ressize;\r
- if (PyUnicodeEncodeError_GetStart(exc, &start))\r
- return NULL;\r
- if (PyUnicodeEncodeError_GetEnd(exc, &end))\r
- return NULL;\r
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))\r
- return NULL;\r
- startp = PyUnicode_AS_UNICODE(object);\r
- for (p = startp+start, ressize = 0; p < startp+end; ++p) {\r
-#ifdef Py_UNICODE_WIDE\r
- if (*p >= 0x00010000)\r
- ressize += 1+1+8;\r
- else\r
-#endif\r
- if (*p >= 0x100) {\r
- ressize += 1+1+4;\r
- }\r
- else\r
- ressize += 1+1+2;\r
- }\r
- res = PyUnicode_FromUnicode(NULL, ressize);\r
- if (res==NULL)\r
- return NULL;\r
- for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);\r
- p < startp+end; ++p) {\r
- Py_UNICODE c = *p;\r
- *outp++ = '\\';\r
-#ifdef Py_UNICODE_WIDE\r
- if (c >= 0x00010000) {\r
- *outp++ = 'U';\r
- *outp++ = hexdigits[(c>>28)&0xf];\r
- *outp++ = hexdigits[(c>>24)&0xf];\r
- *outp++ = hexdigits[(c>>20)&0xf];\r
- *outp++ = hexdigits[(c>>16)&0xf];\r
- *outp++ = hexdigits[(c>>12)&0xf];\r
- *outp++ = hexdigits[(c>>8)&0xf];\r
- }\r
- else\r
-#endif\r
- if (c >= 0x100) {\r
- *outp++ = 'u';\r
- *outp++ = hexdigits[(c>>12)&0xf];\r
- *outp++ = hexdigits[(c>>8)&0xf];\r
- }\r
- else\r
- *outp++ = 'x';\r
- *outp++ = hexdigits[(c>>4)&0xf];\r
- *outp++ = hexdigits[c&0xf];\r
- }\r
-\r
- restuple = Py_BuildValue("(On)", res, end);\r
- Py_DECREF(res);\r
- Py_DECREF(object);\r
- return restuple;\r
- }\r
- else {\r
- wrong_exception_type(exc);\r
- return NULL;\r
- }\r
-}\r
-#endif\r
-\r
-static PyObject *strict_errors(PyObject *self, PyObject *exc)\r
-{\r
- return PyCodec_StrictErrors(exc);\r
-}\r
-\r
-\r
-#ifdef Py_USING_UNICODE\r
-static PyObject *ignore_errors(PyObject *self, PyObject *exc)\r
-{\r
- return PyCodec_IgnoreErrors(exc);\r
-}\r
-\r
-\r
-static PyObject *replace_errors(PyObject *self, PyObject *exc)\r
-{\r
- return PyCodec_ReplaceErrors(exc);\r
-}\r
-\r
-\r
-static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)\r
-{\r
- return PyCodec_XMLCharRefReplaceErrors(exc);\r
-}\r
-\r
-\r
-static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)\r
-{\r
- return PyCodec_BackslashReplaceErrors(exc);\r
-}\r
-#endif\r
-\r
-static int _PyCodecRegistry_Init(void)\r
-{\r
- static struct {\r
- char *name;\r
- PyMethodDef def;\r
- } methods[] =\r
- {\r
- {\r
- "strict",\r
- {\r
- "strict_errors",\r
- strict_errors,\r
- METH_O,\r
- PyDoc_STR("Implements the 'strict' error handling, which "\r
- "raises a UnicodeError on coding errors.")\r
- }\r
- },\r
-#ifdef Py_USING_UNICODE\r
- {\r
- "ignore",\r
- {\r
- "ignore_errors",\r
- ignore_errors,\r
- METH_O,\r
- PyDoc_STR("Implements the 'ignore' error handling, which "\r
- "ignores malformed data and continues.")\r
- }\r
- },\r
- {\r
- "replace",\r
- {\r
- "replace_errors",\r
- replace_errors,\r
- METH_O,\r
- PyDoc_STR("Implements the 'replace' error handling, which "\r
- "replaces malformed data with a replacement marker.")\r
- }\r
- },\r
- {\r
- "xmlcharrefreplace",\r
- {\r
- "xmlcharrefreplace_errors",\r
- xmlcharrefreplace_errors,\r
- METH_O,\r
- PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "\r
- "which replaces an unencodable character with the "\r
- "appropriate XML character reference.")\r
- }\r
- },\r
- {\r
- "backslashreplace",\r
- {\r
- "backslashreplace_errors",\r
- backslashreplace_errors,\r
- METH_O,\r
- PyDoc_STR("Implements the 'backslashreplace' error handling, "\r
- "which replaces an unencodable character with a "\r
- "backslashed escape sequence.")\r
- }\r
- }\r
-#endif\r
- };\r
-\r
- PyInterpreterState *interp = PyThreadState_GET()->interp;\r
- PyObject *mod;\r
- unsigned i;\r
-\r
- if (interp->codec_search_path != NULL)\r
- return 0;\r
-\r
- interp->codec_search_path = PyList_New(0);\r
- interp->codec_search_cache = PyDict_New();\r
- interp->codec_error_registry = PyDict_New();\r
-\r
- if (interp->codec_error_registry) {\r
- for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {\r
- PyObject *func = PyCFunction_New(&methods[i].def, NULL);\r
- int res;\r
- if (!func)\r
- Py_FatalError("can't initialize codec error registry");\r
- res = PyCodec_RegisterError(methods[i].name, func);\r
- Py_DECREF(func);\r
- if (res)\r
- Py_FatalError("can't initialize codec error registry");\r
- }\r
- }\r
-\r
- if (interp->codec_search_path == NULL ||\r
- interp->codec_search_cache == NULL ||\r
- interp->codec_error_registry == NULL)\r
- Py_FatalError("can't initialize codec registry");\r
-\r
- mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);\r
- if (mod == NULL) {\r
- if (PyErr_ExceptionMatches(PyExc_ImportError)) {\r
- /* Ignore ImportErrors... this is done so that\r
- distributions can disable the encodings package. Note\r
- that other errors are not masked, e.g. SystemErrors\r
- raised to inform the user of an error in the Python\r
- configuration are still reported back to the user. */\r
- PyErr_Clear();\r
- return 0;\r
- }\r
- return -1;\r
- }\r
- Py_DECREF(mod);\r
- return 0;\r
-}\r