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