Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
| 3 | Python Codec Registry and support functions |
| 4 | |
| 5 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 6 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 7 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 8 | |
| 9 | ------------------------------------------------------------------------ */ |
| 10 | |
| 11 | #include "Python.h" |
| 12 | #include <ctype.h> |
| 13 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 14 | /* --- Codec Registry ----------------------------------------------------- */ |
| 15 | |
| 16 | /* Import the standard encodings package which will register the first |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 17 | codec search function. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 18 | |
| 19 | This is done in a lazy way so that the Unicode implementation does |
| 20 | not downgrade startup time of scripts not needing it. |
| 21 | |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 22 | ImportErrors are silently ignored by this function. Only one try is |
| 23 | made. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 24 | |
| 25 | */ |
| 26 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 27 | static int _PyCodecRegistry_Init(void); /* Forward */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 29 | int PyCodec_Register(PyObject *search_function) |
| 30 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 31 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 32 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 33 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 34 | if (search_function == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | PyErr_BadArgument(); |
| 36 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 37 | } |
| 38 | if (!PyCallable_Check(search_function)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | PyErr_SetString(PyExc_TypeError, "argument must be callable"); |
| 40 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 41 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 42 | return PyList_Append(interp->codec_search_path, search_function); |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 43 | |
| 44 | onError: |
| 45 | return -1; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 48 | /* Convert a string to a normalized Python string: all characters are |
| 49 | converted to lower case, spaces are replaced with underscores. */ |
| 50 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 51 | static |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 52 | PyObject *normalizestring(const char *string) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | 3383113 | 2000-06-29 14:50:15 +0000 | [diff] [blame] | 54 | register size_t i; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 55 | size_t len = strlen(string); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 56 | char *p; |
| 57 | PyObject *v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 59 | if (len > PY_SSIZE_T_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | PyErr_SetString(PyExc_OverflowError, "string is too large"); |
| 61 | return NULL; |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 62 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 63 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 64 | v = PyString_FromStringAndSize(NULL, len); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 65 | if (v == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 66 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 67 | p = PyString_AS_STRING(v); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 68 | for (i = 0; i < len; i++) { |
| 69 | register char ch = string[i]; |
| 70 | if (ch == ' ') |
| 71 | ch = '-'; |
| 72 | else |
Antoine Pitrou | 4cfae02 | 2011-07-24 02:51:01 +0200 | [diff] [blame] | 73 | ch = Py_TOLOWER(Py_CHARMASK(ch)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 74 | p[i] = ch; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 75 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 76 | return v; |
| 77 | } |
| 78 | |
| 79 | /* Lookup the given encoding and return a tuple providing the codec |
| 80 | facilities. |
| 81 | |
| 82 | The encoding string is looked up converted to all lower-case |
| 83 | characters. This makes encodings looked up through this mechanism |
| 84 | effectively case-insensitive. |
| 85 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 86 | If no codec is found, a LookupError is set and NULL returned. |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 87 | |
| 88 | As side effect, this tries to load the encodings package, if not |
| 89 | yet done. This is part of the lazy load strategy for the encodings |
| 90 | package. |
| 91 | |
| 92 | */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 93 | |
| 94 | PyObject *_PyCodec_Lookup(const char *encoding) |
| 95 | { |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 96 | PyInterpreterState *interp; |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 97 | PyObject *result, *args = NULL, *v; |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 98 | Py_ssize_t i, len; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 99 | |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 100 | if (encoding == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 101 | PyErr_BadArgument(); |
| 102 | goto onError; |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 103 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 104 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 105 | interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 106 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 107 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 109 | /* Convert the encoding to a normalized Python string: all |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 110 | characters are converted to lower case, spaces and hyphens are |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 111 | replaced with underscores. */ |
| 112 | v = normalizestring(encoding); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 113 | if (v == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 114 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 115 | PyString_InternInPlace(&v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 116 | |
| 117 | /* First, try to lookup the name in the registry dictionary */ |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 118 | result = PyDict_GetItem(interp->codec_search_cache, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 119 | if (result != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 120 | Py_INCREF(result); |
| 121 | Py_DECREF(v); |
| 122 | return result; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 123 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 125 | /* Next, scan the search functions in order of registration */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 126 | args = PyTuple_New(1); |
| 127 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 128 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 129 | PyTuple_SET_ITEM(args,0,v); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 130 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 131 | len = PyList_Size(interp->codec_search_path); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 132 | if (len < 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 133 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 134 | if (len == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 135 | PyErr_SetString(PyExc_LookupError, |
| 136 | "no codec search functions registered: " |
| 137 | "can't find encoding"); |
| 138 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 139 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 140 | |
| 141 | for (i = 0; i < len; i++) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 142 | PyObject *func; |
| 143 | |
| 144 | func = PyList_GetItem(interp->codec_search_path, i); |
| 145 | if (func == NULL) |
| 146 | goto onError; |
| 147 | result = PyEval_CallObject(func, args); |
| 148 | if (result == NULL) |
| 149 | goto onError; |
| 150 | if (result == Py_None) { |
| 151 | Py_DECREF(result); |
| 152 | continue; |
| 153 | } |
| 154 | if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { |
| 155 | PyErr_SetString(PyExc_TypeError, |
| 156 | "codec search functions must return 4-tuples"); |
| 157 | Py_DECREF(result); |
| 158 | goto onError; |
| 159 | } |
| 160 | break; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 161 | } |
| 162 | if (i == len) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 163 | /* XXX Perhaps we should cache misses too ? */ |
| 164 | PyErr_Format(PyExc_LookupError, |
Martin v. Löwis | eb42b02 | 2002-09-26 16:01:24 +0000 | [diff] [blame] | 165 | "unknown encoding: %s", encoding); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 166 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Cache and return the result */ |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 170 | PyDict_SetItem(interp->codec_search_cache, v, result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 171 | Py_DECREF(args); |
| 172 | return result; |
| 173 | |
| 174 | onError: |
| 175 | Py_XDECREF(args); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | PyObject *args_tuple(PyObject *object, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 181 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 182 | { |
| 183 | PyObject *args; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 185 | args = PyTuple_New(1 + (errors != NULL)); |
| 186 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 187 | return NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 188 | Py_INCREF(object); |
| 189 | PyTuple_SET_ITEM(args,0,object); |
| 190 | if (errors) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 191 | PyObject *v; |
| 192 | |
| 193 | v = PyString_FromString(errors); |
| 194 | if (v == NULL) { |
| 195 | Py_DECREF(args); |
| 196 | return NULL; |
| 197 | } |
| 198 | PyTuple_SET_ITEM(args, 1, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 199 | } |
| 200 | return args; |
| 201 | } |
| 202 | |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 203 | /* Helper function to get a codec item */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 204 | |
| 205 | static |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 206 | PyObject *codec_getitem(const char *encoding, int index) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 207 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 208 | PyObject *codecs; |
| 209 | PyObject *v; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 210 | |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 211 | codecs = _PyCodec_Lookup(encoding); |
| 212 | if (codecs == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 213 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 214 | v = PyTuple_GET_ITEM(codecs, index); |
| 215 | Py_DECREF(codecs); |
| 216 | Py_INCREF(v); |
| 217 | return v; |
| 218 | } |
| 219 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 220 | /* Helper functions to create an incremental codec. */ |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 221 | static |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 222 | PyObject *codec_makeincrementalcodec(PyObject *codec_info, |
| 223 | const char *errors, |
| 224 | const char *attrname) |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 225 | { |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 226 | PyObject *ret, *inccodec; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 227 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 228 | inccodec = PyObject_GetAttrString(codec_info, attrname); |
Walter Dörwald | ba8e180 | 2006-03-18 14:05:43 +0000 | [diff] [blame] | 229 | if (inccodec == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 230 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 231 | if (errors) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 232 | ret = PyObject_CallFunction(inccodec, "s", errors); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 233 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 234 | ret = PyObject_CallFunction(inccodec, NULL); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 235 | Py_DECREF(inccodec); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 236 | return ret; |
| 237 | } |
| 238 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 239 | static |
| 240 | PyObject *codec_getincrementalcodec(const char *encoding, |
| 241 | const char *errors, |
| 242 | const char *attrname) |
| 243 | { |
| 244 | PyObject *codec_info, *ret; |
| 245 | |
| 246 | codec_info = _PyCodec_Lookup(encoding); |
| 247 | if (codec_info == NULL) |
| 248 | return NULL; |
| 249 | ret = codec_makeincrementalcodec(codec_info, errors, attrname); |
| 250 | Py_DECREF(codec_info); |
| 251 | return ret; |
| 252 | } |
| 253 | |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 254 | /* Helper function to create a stream codec. */ |
| 255 | |
| 256 | static |
| 257 | PyObject *codec_getstreamcodec(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 258 | PyObject *stream, |
| 259 | const char *errors, |
| 260 | const int index) |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 261 | { |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 262 | PyObject *codecs, *streamcodec, *codeccls; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 263 | |
| 264 | codecs = _PyCodec_Lookup(encoding); |
| 265 | if (codecs == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 266 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 267 | |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 268 | codeccls = PyTuple_GET_ITEM(codecs, index); |
| 269 | if (errors != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 270 | streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 271 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 272 | streamcodec = PyObject_CallFunction(codeccls, "O", stream); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 273 | Py_DECREF(codecs); |
| 274 | return streamcodec; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 277 | /* Helpers to work with the result of _PyCodec_Lookup |
| 278 | |
| 279 | */ |
| 280 | PyObject *_PyCodecInfo_GetIncrementalDecoder(PyObject *codec_info, |
| 281 | const char *errors) |
| 282 | { |
| 283 | return codec_makeincrementalcodec(codec_info, errors, |
| 284 | "incrementaldecoder"); |
| 285 | } |
| 286 | |
| 287 | PyObject *_PyCodecInfo_GetIncrementalEncoder(PyObject *codec_info, |
| 288 | const char *errors) |
| 289 | { |
| 290 | return codec_makeincrementalcodec(codec_info, errors, |
| 291 | "incrementalencoder"); |
| 292 | } |
| 293 | |
| 294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | /* Convenience APIs to query the Codec registry. |
| 296 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 297 | All APIs return a codec object with incremented refcount. |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 299 | */ |
| 300 | |
| 301 | PyObject *PyCodec_Encoder(const char *encoding) |
| 302 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 303 | return codec_getitem(encoding, 0); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | PyObject *PyCodec_Decoder(const char *encoding) |
| 307 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 308 | return codec_getitem(encoding, 1); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 311 | PyObject *PyCodec_IncrementalEncoder(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 312 | const char *errors) |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 313 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 314 | return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | PyObject *PyCodec_IncrementalDecoder(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 318 | const char *errors) |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 319 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 320 | return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 323 | PyObject *PyCodec_StreamReader(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 324 | PyObject *stream, |
| 325 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 326 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 327 | return codec_getstreamcodec(encoding, stream, errors, 2); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | PyObject *PyCodec_StreamWriter(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 331 | PyObject *stream, |
| 332 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 333 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 334 | return codec_getstreamcodec(encoding, stream, errors, 3); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Encode an object (e.g. an Unicode object) using the given encoding |
| 338 | and return the resulting encoded object (usually a Python string). |
| 339 | |
| 340 | errors is passed to the encoder factory as argument if non-NULL. */ |
| 341 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 342 | static PyObject * |
| 343 | _PyCodec_EncodeInternal(PyObject *object, |
| 344 | PyObject *encoder, |
| 345 | const char *encoding, |
| 346 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 347 | { |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 348 | PyObject *args = NULL, *result = NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 349 | PyObject *v; |
| 350 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 351 | args = args_tuple(object, errors); |
| 352 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 353 | goto onError; |
| 354 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 355 | result = PyEval_CallObject(encoder,args); |
| 356 | if (result == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 357 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 359 | if (!PyTuple_Check(result) || |
| 360 | PyTuple_GET_SIZE(result) != 2) { |
| 361 | PyErr_SetString(PyExc_TypeError, |
| 362 | "encoder must return a tuple (object,integer)"); |
| 363 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 364 | } |
| 365 | v = PyTuple_GET_ITEM(result,0); |
| 366 | Py_INCREF(v); |
| 367 | /* We don't check or use the second (integer) entry. */ |
| 368 | |
| 369 | Py_DECREF(args); |
| 370 | Py_DECREF(encoder); |
| 371 | Py_DECREF(result); |
| 372 | return v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 374 | onError: |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 375 | Py_XDECREF(result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 376 | Py_XDECREF(args); |
| 377 | Py_XDECREF(encoder); |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | /* Decode an object (usually a Python string) using the given encoding |
| 382 | and return an equivalent object (e.g. an Unicode object). |
| 383 | |
| 384 | errors is passed to the decoder factory as argument if non-NULL. */ |
| 385 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 386 | static PyObject * |
| 387 | _PyCodec_DecodeInternal(PyObject *object, |
| 388 | PyObject *decoder, |
| 389 | const char *encoding, |
| 390 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 391 | { |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 392 | PyObject *args = NULL, *result = NULL; |
| 393 | PyObject *v; |
| 394 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 395 | args = args_tuple(object, errors); |
| 396 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 397 | goto onError; |
| 398 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 399 | result = PyEval_CallObject(decoder,args); |
| 400 | if (result == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 401 | goto onError; |
| 402 | if (!PyTuple_Check(result) || |
| 403 | PyTuple_GET_SIZE(result) != 2) { |
| 404 | PyErr_SetString(PyExc_TypeError, |
| 405 | "decoder must return a tuple (object,integer)"); |
| 406 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 407 | } |
| 408 | v = PyTuple_GET_ITEM(result,0); |
| 409 | Py_INCREF(v); |
| 410 | /* We don't check or use the second (integer) entry. */ |
| 411 | |
| 412 | Py_DECREF(args); |
| 413 | Py_DECREF(decoder); |
| 414 | Py_DECREF(result); |
| 415 | return v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 417 | onError: |
| 418 | Py_XDECREF(args); |
| 419 | Py_XDECREF(decoder); |
| 420 | Py_XDECREF(result); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 424 | /* Generic encoding/decoding API */ |
| 425 | PyObject *PyCodec_Encode(PyObject *object, |
| 426 | const char *encoding, |
| 427 | const char *errors) |
| 428 | { |
| 429 | PyObject *encoder; |
| 430 | |
| 431 | encoder = PyCodec_Encoder(encoding); |
| 432 | if (encoder == NULL) |
| 433 | return NULL; |
| 434 | |
| 435 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 436 | } |
| 437 | |
| 438 | PyObject *PyCodec_Decode(PyObject *object, |
| 439 | const char *encoding, |
| 440 | const char *errors) |
| 441 | { |
| 442 | PyObject *decoder; |
| 443 | |
| 444 | decoder = PyCodec_Decoder(encoding); |
| 445 | if (decoder == NULL) |
| 446 | return NULL; |
| 447 | |
| 448 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 449 | } |
| 450 | |
| 451 | /* Text encoding/decoding API */ |
| 452 | PyObject * _PyCodec_LookupTextEncoding(const char *encoding, |
| 453 | const char *alternate_command) |
| 454 | { |
| 455 | PyObject *codec; |
| 456 | PyObject *attr; |
| 457 | int is_text_codec; |
| 458 | |
| 459 | codec = _PyCodec_Lookup(encoding); |
| 460 | if (codec == NULL) |
| 461 | return NULL; |
| 462 | |
| 463 | /* Backwards compatibility: assume any raw tuple describes a text |
| 464 | * encoding, and the same for anything lacking the private |
| 465 | * attribute. |
| 466 | */ |
| 467 | if (Py_Py3kWarningFlag && !PyTuple_CheckExact(codec)) { |
| 468 | attr = PyObject_GetAttrString(codec, "_is_text_encoding"); |
| 469 | if (attr == NULL) { |
| 470 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 471 | goto onError; |
| 472 | PyErr_Clear(); |
| 473 | } else { |
| 474 | is_text_codec = PyObject_IsTrue(attr); |
| 475 | Py_DECREF(attr); |
| 476 | if (is_text_codec < 0) |
| 477 | goto onError; |
| 478 | if (!is_text_codec) { |
| 479 | PyObject *msg = PyString_FromFormat( |
| 480 | "'%.400s' is not a text encoding; " |
| 481 | "use %s to handle arbitrary codecs", |
| 482 | encoding, alternate_command); |
| 483 | if (msg == NULL) |
| 484 | goto onError; |
| 485 | if (PyErr_WarnPy3k(PyString_AS_STRING(msg), 1) < 0) { |
| 486 | Py_DECREF(msg); |
| 487 | goto onError; |
| 488 | } |
| 489 | Py_DECREF(msg); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* This appears to be a valid text encoding */ |
| 495 | return codec; |
| 496 | |
| 497 | onError: |
| 498 | Py_DECREF(codec); |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | static |
| 504 | PyObject *codec_getitem_checked(const char *encoding, |
| 505 | const char *alternate_command, |
| 506 | int index) |
| 507 | { |
| 508 | PyObject *codec; |
| 509 | PyObject *v; |
| 510 | |
| 511 | codec = _PyCodec_LookupTextEncoding(encoding, alternate_command); |
| 512 | if (codec == NULL) |
| 513 | return NULL; |
| 514 | |
| 515 | v = PyTuple_GET_ITEM(codec, index); |
| 516 | Py_INCREF(v); |
| 517 | Py_DECREF(codec); |
| 518 | return v; |
| 519 | } |
| 520 | |
| 521 | static PyObject * _PyCodec_TextEncoder(const char *encoding) |
| 522 | { |
| 523 | return codec_getitem_checked(encoding, "codecs.encode()", 0); |
| 524 | } |
| 525 | |
| 526 | static PyObject * _PyCodec_TextDecoder(const char *encoding) |
| 527 | { |
| 528 | return codec_getitem_checked(encoding, "codecs.decode()", 1); |
| 529 | } |
| 530 | |
| 531 | PyObject *_PyCodec_EncodeText(PyObject *object, |
| 532 | const char *encoding, |
| 533 | const char *errors) |
| 534 | { |
| 535 | PyObject *encoder; |
| 536 | |
| 537 | encoder = _PyCodec_TextEncoder(encoding); |
| 538 | if (encoder == NULL) |
| 539 | return NULL; |
| 540 | |
| 541 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 542 | } |
| 543 | |
| 544 | PyObject *_PyCodec_DecodeText(PyObject *object, |
| 545 | const char *encoding, |
| 546 | const char *errors) |
| 547 | { |
| 548 | PyObject *decoder; |
| 549 | |
| 550 | decoder = _PyCodec_TextDecoder(encoding); |
| 551 | if (decoder == NULL) |
| 552 | return NULL; |
| 553 | |
| 554 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 555 | } |
| 556 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 557 | /* Register the error handling callback function error under the name |
| 558 | name. This function will be called by the codec when it encounters |
| 559 | an unencodable characters/undecodable bytes and doesn't know the |
| 560 | callback name, when name is specified as the error parameter |
| 561 | in the call to the encode/decode function. |
| 562 | Return 0 on success, -1 on error */ |
| 563 | int PyCodec_RegisterError(const char *name, PyObject *error) |
| 564 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 565 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 566 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 568 | if (!PyCallable_Check(error)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | PyErr_SetString(PyExc_TypeError, "handler must be callable"); |
| 570 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 571 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 572 | return PyDict_SetItemString(interp->codec_error_registry, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 573 | (char *)name, error); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Lookup the error handling callback function registered under the |
| 577 | name error. As a special case NULL can be passed, in which case |
| 578 | the error handling callback for strict encoding will be returned. */ |
| 579 | PyObject *PyCodec_LookupError(const char *name) |
| 580 | { |
| 581 | PyObject *handler = NULL; |
| 582 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 583 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 584 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | return NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 586 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 587 | if (name==NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | name = "strict"; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 589 | handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 590 | if (!handler) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 592 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 593 | Py_INCREF(handler); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 594 | return handler; |
| 595 | } |
| 596 | |
| 597 | static void wrong_exception_type(PyObject *exc) |
| 598 | { |
| 599 | PyObject *type = PyObject_GetAttrString(exc, "__class__"); |
| 600 | if (type != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 601 | PyObject *name = PyObject_GetAttrString(type, "__name__"); |
| 602 | Py_DECREF(type); |
| 603 | if (name != NULL) { |
| 604 | PyObject *string = PyObject_Str(name); |
| 605 | Py_DECREF(name); |
| 606 | if (string != NULL) { |
| 607 | PyErr_Format(PyExc_TypeError, |
| 608 | "don't know how to handle %.400s in error callback", |
| 609 | PyString_AS_STRING(string)); |
| 610 | Py_DECREF(string); |
| 611 | } |
| 612 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | PyObject *PyCodec_StrictErrors(PyObject *exc) |
| 617 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 618 | if (PyExceptionInstance_Check(exc)) |
| 619 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 620 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 621 | PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 626 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 627 | PyObject *PyCodec_IgnoreErrors(PyObject *exc) |
| 628 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 629 | Py_ssize_t end; |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 630 | |
| 631 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 632 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 633 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 634 | } |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 635 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 636 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 637 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 638 | } |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 639 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 640 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 641 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 642 | } |
| 643 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | wrong_exception_type(exc); |
| 645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 646 | } |
| 647 | /* ouch: passing NULL, 0, pos gives None instead of u'' */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 648 | return Py_BuildValue("(u#n)", &end, 0, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | |
| 652 | PyObject *PyCodec_ReplaceErrors(PyObject *exc) |
| 653 | { |
| 654 | PyObject *restuple; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 655 | Py_ssize_t start; |
| 656 | Py_ssize_t end; |
| 657 | Py_ssize_t i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 658 | |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 659 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 660 | PyObject *res; |
| 661 | Py_UNICODE *p; |
| 662 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 663 | return NULL; |
| 664 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 665 | return NULL; |
| 666 | res = PyUnicode_FromUnicode(NULL, end-start); |
| 667 | if (res == NULL) |
| 668 | return NULL; |
| 669 | for (p = PyUnicode_AS_UNICODE(res), i = start; |
| 670 | i<end; ++p, ++i) |
| 671 | *p = '?'; |
| 672 | restuple = Py_BuildValue("(On)", res, end); |
| 673 | Py_DECREF(res); |
| 674 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 675 | } |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 676 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 677 | Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; |
| 678 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 679 | return NULL; |
Serhiy Storchaka | a9885e9 | 2013-08-20 20:08:53 +0300 | [diff] [blame] | 680 | return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 681 | } |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 682 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | PyObject *res; |
| 684 | Py_UNICODE *p; |
| 685 | if (PyUnicodeTranslateError_GetStart(exc, &start)) |
| 686 | return NULL; |
| 687 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 688 | return NULL; |
| 689 | res = PyUnicode_FromUnicode(NULL, end-start); |
| 690 | if (res == NULL) |
| 691 | return NULL; |
| 692 | for (p = PyUnicode_AS_UNICODE(res), i = start; |
| 693 | i<end; ++p, ++i) |
| 694 | *p = Py_UNICODE_REPLACEMENT_CHARACTER; |
| 695 | restuple = Py_BuildValue("(On)", res, end); |
| 696 | Py_DECREF(res); |
| 697 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 698 | } |
| 699 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | wrong_exception_type(exc); |
| 701 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
| 706 | { |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 707 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | PyObject *restuple; |
| 709 | PyObject *object; |
| 710 | Py_ssize_t start; |
| 711 | Py_ssize_t end; |
| 712 | PyObject *res; |
| 713 | Py_UNICODE *p; |
| 714 | Py_UNICODE *startp; |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 715 | Py_UNICODE *e; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 716 | Py_UNICODE *outp; |
Serhiy Storchaka | d524922 | 2014-10-04 14:14:41 +0300 | [diff] [blame] | 717 | Py_ssize_t ressize; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 718 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 719 | return NULL; |
| 720 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 721 | return NULL; |
| 722 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 723 | return NULL; |
| 724 | startp = PyUnicode_AS_UNICODE(object); |
Serhiy Storchaka | d524922 | 2014-10-04 14:14:41 +0300 | [diff] [blame] | 725 | if (end - start > PY_SSIZE_T_MAX / (2+7+1)) { |
| 726 | end = start + PY_SSIZE_T_MAX / (2+7+1); |
| 727 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | fb7c380 | 2014-10-04 14:51:44 +0300 | [diff] [blame] | 728 | if (0xD800 <= startp[end - 1] && startp[end - 1] <= 0xDBFF) |
Serhiy Storchaka | d524922 | 2014-10-04 14:14:41 +0300 | [diff] [blame] | 729 | end--; |
| 730 | #endif |
| 731 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 732 | e = startp + end; |
| 733 | for (p = startp+start, ressize = 0; p < e;) { |
| 734 | Py_UCS4 ch = *p++; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 735 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 736 | if ((0xD800 <= ch && ch <= 0xDBFF) && |
| 737 | (p < e) && |
| 738 | (0xDC00 <= *p && *p <= 0xDFFF)) { |
| 739 | ch = ((((ch & 0x03FF) << 10) | |
| 740 | ((Py_UCS4)*p++ & 0x03FF)) + 0x10000); |
| 741 | } |
| 742 | #endif |
| 743 | if (ch < 10) |
| 744 | ressize += 2+1+1; |
| 745 | else if (ch < 100) |
| 746 | ressize += 2+2+1; |
| 747 | else if (ch < 1000) |
| 748 | ressize += 2+3+1; |
| 749 | else if (ch < 10000) |
| 750 | ressize += 2+4+1; |
| 751 | else if (ch < 100000) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 752 | ressize += 2+5+1; |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 753 | else if (ch < 1000000) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 754 | ressize += 2+6+1; |
| 755 | else |
| 756 | ressize += 2+7+1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 757 | } |
| 758 | /* allocate replacement */ |
| 759 | res = PyUnicode_FromUnicode(NULL, ressize); |
| 760 | if (res == NULL) { |
| 761 | Py_DECREF(object); |
| 762 | return NULL; |
| 763 | } |
| 764 | /* generate replacement */ |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 765 | for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); p < e;) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 766 | int digits; |
| 767 | int base; |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 768 | Py_UCS4 ch = *p++; |
| 769 | #ifndef Py_UNICODE_WIDE |
| 770 | if ((0xD800 <= ch && ch <= 0xDBFF) && |
| 771 | (p < startp+end) && |
| 772 | (0xDC00 <= *p && *p <= 0xDFFF)) { |
| 773 | ch = ((((ch & 0x03FF) << 10) | |
| 774 | ((Py_UCS4)*p++ & 0x03FF)) + 0x10000); |
| 775 | } |
| 776 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 777 | *outp++ = '&'; |
| 778 | *outp++ = '#'; |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 779 | if (ch < 10) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 780 | digits = 1; |
| 781 | base = 1; |
| 782 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 783 | else if (ch < 100) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 784 | digits = 2; |
| 785 | base = 10; |
| 786 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 787 | else if (ch < 1000) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | digits = 3; |
| 789 | base = 100; |
| 790 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 791 | else if (ch < 10000) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 792 | digits = 4; |
| 793 | base = 1000; |
| 794 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 795 | else if (ch < 100000) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 796 | digits = 5; |
| 797 | base = 10000; |
| 798 | } |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 799 | else if (ch < 1000000) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 800 | digits = 6; |
| 801 | base = 100000; |
| 802 | } |
| 803 | else { |
| 804 | digits = 7; |
| 805 | base = 1000000; |
| 806 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 807 | while (digits-->0) { |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 808 | *outp++ = '0' + ch/base; |
| 809 | ch %= base; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 810 | base /= 10; |
| 811 | } |
| 812 | *outp++ = ';'; |
| 813 | } |
| 814 | restuple = Py_BuildValue("(On)", res, end); |
| 815 | Py_DECREF(res); |
| 816 | Py_DECREF(object); |
| 817 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 818 | } |
| 819 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 820 | wrong_exception_type(exc); |
| 821 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | static Py_UNICODE hexdigits[] = { |
| 826 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 827 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 828 | }; |
| 829 | |
| 830 | PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) |
| 831 | { |
Serhiy Storchaka | 14e10a1 | 2015-05-18 16:08:38 +0300 | [diff] [blame] | 832 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 833 | PyObject *restuple; |
| 834 | PyObject *object; |
| 835 | Py_ssize_t start; |
| 836 | Py_ssize_t end; |
| 837 | PyObject *res; |
| 838 | Py_UNICODE *p; |
| 839 | Py_UNICODE *startp; |
| 840 | Py_UNICODE *outp; |
Serhiy Storchaka | d524922 | 2014-10-04 14:14:41 +0300 | [diff] [blame] | 841 | Py_ssize_t ressize; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 843 | return NULL; |
| 844 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 845 | return NULL; |
| 846 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 847 | return NULL; |
Serhiy Storchaka | d524922 | 2014-10-04 14:14:41 +0300 | [diff] [blame] | 848 | if (end - start > PY_SSIZE_T_MAX / (1+1+8)) |
| 849 | end = start + PY_SSIZE_T_MAX / (1+1+8); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 850 | startp = PyUnicode_AS_UNICODE(object); |
| 851 | for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 852 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 853 | if (*p >= 0x00010000) |
| 854 | ressize += 1+1+8; |
| 855 | else |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 856 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 857 | if (*p >= 0x100) { |
| 858 | ressize += 1+1+4; |
| 859 | } |
| 860 | else |
| 861 | ressize += 1+1+2; |
| 862 | } |
| 863 | res = PyUnicode_FromUnicode(NULL, ressize); |
Serhiy Storchaka | 7d96a09 | 2014-09-23 19:58:57 +0300 | [diff] [blame] | 864 | if (res == NULL) { |
| 865 | Py_DECREF(object); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 866 | return NULL; |
Serhiy Storchaka | 7d96a09 | 2014-09-23 19:58:57 +0300 | [diff] [blame] | 867 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 868 | for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); |
| 869 | p < startp+end; ++p) { |
| 870 | Py_UNICODE c = *p; |
| 871 | *outp++ = '\\'; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 872 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 873 | if (c >= 0x00010000) { |
| 874 | *outp++ = 'U'; |
| 875 | *outp++ = hexdigits[(c>>28)&0xf]; |
| 876 | *outp++ = hexdigits[(c>>24)&0xf]; |
| 877 | *outp++ = hexdigits[(c>>20)&0xf]; |
| 878 | *outp++ = hexdigits[(c>>16)&0xf]; |
| 879 | *outp++ = hexdigits[(c>>12)&0xf]; |
| 880 | *outp++ = hexdigits[(c>>8)&0xf]; |
| 881 | } |
| 882 | else |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 883 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 884 | if (c >= 0x100) { |
| 885 | *outp++ = 'u'; |
| 886 | *outp++ = hexdigits[(c>>12)&0xf]; |
| 887 | *outp++ = hexdigits[(c>>8)&0xf]; |
| 888 | } |
| 889 | else |
| 890 | *outp++ = 'x'; |
| 891 | *outp++ = hexdigits[(c>>4)&0xf]; |
| 892 | *outp++ = hexdigits[c&0xf]; |
| 893 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 895 | restuple = Py_BuildValue("(On)", res, end); |
| 896 | Py_DECREF(res); |
| 897 | Py_DECREF(object); |
| 898 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 899 | } |
| 900 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 901 | wrong_exception_type(exc); |
| 902 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 905 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 906 | |
| 907 | static PyObject *strict_errors(PyObject *self, PyObject *exc) |
| 908 | { |
| 909 | return PyCodec_StrictErrors(exc); |
| 910 | } |
| 911 | |
| 912 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 913 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 914 | static PyObject *ignore_errors(PyObject *self, PyObject *exc) |
| 915 | { |
| 916 | return PyCodec_IgnoreErrors(exc); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | static PyObject *replace_errors(PyObject *self, PyObject *exc) |
| 921 | { |
| 922 | return PyCodec_ReplaceErrors(exc); |
| 923 | } |
| 924 | |
| 925 | |
| 926 | static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) |
| 927 | { |
| 928 | return PyCodec_XMLCharRefReplaceErrors(exc); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) |
| 933 | { |
| 934 | return PyCodec_BackslashReplaceErrors(exc); |
| 935 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 936 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 937 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 938 | static int _PyCodecRegistry_Init(void) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 939 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 940 | static struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | char *name; |
| 942 | PyMethodDef def; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 943 | } methods[] = |
| 944 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 945 | { |
| 946 | "strict", |
| 947 | { |
| 948 | "strict_errors", |
| 949 | strict_errors, |
| 950 | METH_O, |
| 951 | PyDoc_STR("Implements the 'strict' error handling, which " |
| 952 | "raises a UnicodeError on coding errors.") |
| 953 | } |
| 954 | }, |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 955 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 956 | { |
| 957 | "ignore", |
| 958 | { |
| 959 | "ignore_errors", |
| 960 | ignore_errors, |
| 961 | METH_O, |
| 962 | PyDoc_STR("Implements the 'ignore' error handling, which " |
| 963 | "ignores malformed data and continues.") |
| 964 | } |
| 965 | }, |
| 966 | { |
| 967 | "replace", |
| 968 | { |
| 969 | "replace_errors", |
| 970 | replace_errors, |
| 971 | METH_O, |
| 972 | PyDoc_STR("Implements the 'replace' error handling, which " |
| 973 | "replaces malformed data with a replacement marker.") |
| 974 | } |
| 975 | }, |
| 976 | { |
| 977 | "xmlcharrefreplace", |
| 978 | { |
| 979 | "xmlcharrefreplace_errors", |
| 980 | xmlcharrefreplace_errors, |
| 981 | METH_O, |
| 982 | PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " |
| 983 | "which replaces an unencodable character with the " |
| 984 | "appropriate XML character reference.") |
| 985 | } |
| 986 | }, |
| 987 | { |
| 988 | "backslashreplace", |
| 989 | { |
| 990 | "backslashreplace_errors", |
| 991 | backslashreplace_errors, |
| 992 | METH_O, |
| 993 | PyDoc_STR("Implements the 'backslashreplace' error handling, " |
| 994 | "which replaces an unencodable character with a " |
| 995 | "backslashed escape sequence.") |
| 996 | } |
| 997 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 998 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 999 | }; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1000 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 1001 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1002 | PyObject *mod; |
Neal Norwitz | 739a8f8 | 2004-07-08 01:55:58 +0000 | [diff] [blame] | 1003 | unsigned i; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (interp->codec_search_path != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1006 | return 0; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1007 | |
| 1008 | interp->codec_search_path = PyList_New(0); |
| 1009 | interp->codec_search_cache = PyDict_New(); |
| 1010 | interp->codec_error_registry = PyDict_New(); |
| 1011 | |
| 1012 | if (interp->codec_error_registry) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1013 | for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { |
| 1014 | PyObject *func = PyCFunction_New(&methods[i].def, NULL); |
| 1015 | int res; |
| 1016 | if (!func) |
| 1017 | Py_FatalError("can't initialize codec error registry"); |
| 1018 | res = PyCodec_RegisterError(methods[i].name, func); |
| 1019 | Py_DECREF(func); |
| 1020 | if (res) |
| 1021 | Py_FatalError("can't initialize codec error registry"); |
| 1022 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1023 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1024 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1025 | if (interp->codec_search_path == NULL || |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1026 | interp->codec_search_cache == NULL || |
| 1027 | interp->codec_error_registry == NULL) |
| 1028 | Py_FatalError("can't initialize codec registry"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1029 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1030 | mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1031 | if (mod == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1032 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1033 | /* Ignore ImportErrors... this is done so that |
| 1034 | distributions can disable the encodings package. Note |
| 1035 | that other errors are not masked, e.g. SystemErrors |
| 1036 | raised to inform the user of an error in the Python |
| 1037 | configuration are still reported back to the user. */ |
| 1038 | PyErr_Clear(); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | return -1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1042 | } |
| 1043 | Py_DECREF(mod); |
| 1044 | return 0; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1045 | } |