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