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" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 12 | #include "internal/pystate.h" |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 13 | #include "ucnhash.h" |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 16 | const char *Py_hexdigits = "0123456789abcdef"; |
| 17 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 18 | /* --- Codec Registry ----------------------------------------------------- */ |
| 19 | |
| 20 | /* Import the standard encodings package which will register the first |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 21 | codec search function. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 22 | |
| 23 | This is done in a lazy way so that the Unicode implementation does |
| 24 | not downgrade startup time of scripts not needing it. |
| 25 | |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 26 | ImportErrors are silently ignored by this function. Only one try is |
| 27 | made. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 28 | |
| 29 | */ |
| 30 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 31 | static int _PyCodecRegistry_Init(void); /* Forward */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 33 | int PyCodec_Register(PyObject *search_function) |
| 34 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 35 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 36 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 38 | if (search_function == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | PyErr_BadArgument(); |
| 40 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 41 | } |
| 42 | if (!PyCallable_Check(search_function)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | PyErr_SetString(PyExc_TypeError, "argument must be callable"); |
| 44 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 45 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 46 | return PyList_Append(interp->codec_search_path, search_function); |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 47 | |
| 48 | onError: |
| 49 | return -1; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 52 | /* Convert a string to a normalized Python string: all characters are |
| 53 | converted to lower case, spaces are replaced with underscores. */ |
| 54 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 55 | static |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 56 | PyObject *normalizestring(const char *string) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 58 | size_t i; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 59 | size_t len = strlen(string); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 60 | char *p; |
| 61 | PyObject *v; |
Guido van Rossum | 21431e8 | 2007-10-19 21:48:41 +0000 | [diff] [blame] | 62 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | if (len > PY_SSIZE_T_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | PyErr_SetString(PyExc_OverflowError, "string is too large"); |
| 65 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | } |
Guido van Rossum | 21431e8 | 2007-10-19 21:48:41 +0000 | [diff] [blame] | 67 | |
| 68 | p = PyMem_Malloc(len + 1); |
| 69 | if (p == NULL) |
Victor Stinner | cc35159 | 2013-07-12 00:02:55 +0200 | [diff] [blame] | 70 | return PyErr_NoMemory(); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 71 | for (i = 0; i < len; i++) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 72 | char ch = string[i]; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 73 | if (ch == ' ') |
| 74 | ch = '-'; |
| 75 | else |
Antoine Pitrou | cf9d3c0 | 2011-07-24 02:27:04 +0200 | [diff] [blame] | 76 | ch = Py_TOLOWER(Py_CHARMASK(ch)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | p[i] = ch; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 78 | } |
Guido van Rossum | 21431e8 | 2007-10-19 21:48:41 +0000 | [diff] [blame] | 79 | p[i] = '\0'; |
| 80 | v = PyUnicode_FromString(p); |
| 81 | if (v == NULL) |
| 82 | return NULL; |
| 83 | PyMem_Free(p); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 84 | return v; |
| 85 | } |
| 86 | |
| 87 | /* Lookup the given encoding and return a tuple providing the codec |
| 88 | facilities. |
| 89 | |
| 90 | The encoding string is looked up converted to all lower-case |
| 91 | characters. This makes encodings looked up through this mechanism |
| 92 | effectively case-insensitive. |
| 93 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | As side effect, this tries to load the encodings package, if not |
| 97 | yet done. This is part of the lazy load strategy for the encodings |
| 98 | package. |
| 99 | |
| 100 | */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 101 | |
| 102 | PyObject *_PyCodec_Lookup(const char *encoding) |
| 103 | { |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 104 | PyInterpreterState *interp; |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 105 | PyObject *result, *args = NULL, *v; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 106 | Py_ssize_t i, len; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 107 | |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 108 | if (encoding == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyErr_BadArgument(); |
| 110 | goto onError; |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 111 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 112 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 113 | interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 114 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 117 | /* Convert the encoding to a normalized Python string: all |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 118 | characters are converted to lower case, spaces and hyphens are |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 119 | replaced with underscores. */ |
| 120 | v = normalizestring(encoding); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 121 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | goto onError; |
Guido van Rossum | 21431e8 | 2007-10-19 21:48:41 +0000 | [diff] [blame] | 123 | PyUnicode_InternInPlace(&v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 124 | |
| 125 | /* First, try to lookup the name in the registry dictionary */ |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 126 | result = PyDict_GetItem(interp->codec_search_cache, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 127 | if (result != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | Py_INCREF(result); |
| 129 | Py_DECREF(v); |
| 130 | return result; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 131 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 133 | /* Next, scan the search functions in order of registration */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 134 | args = PyTuple_New(1); |
| 135 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 137 | PyTuple_SET_ITEM(args,0,v); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 138 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 139 | len = PyList_Size(interp->codec_search_path); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 140 | if (len < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 142 | if (len == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PyErr_SetString(PyExc_LookupError, |
| 144 | "no codec search functions registered: " |
| 145 | "can't find encoding"); |
| 146 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 147 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 148 | |
| 149 | for (i = 0; i < len; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | PyObject *func; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | func = PyList_GetItem(interp->codec_search_path, i); |
| 153 | if (func == NULL) |
| 154 | goto onError; |
| 155 | result = PyEval_CallObject(func, args); |
| 156 | if (result == NULL) |
| 157 | goto onError; |
| 158 | if (result == Py_None) { |
| 159 | Py_DECREF(result); |
| 160 | continue; |
| 161 | } |
| 162 | if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { |
| 163 | PyErr_SetString(PyExc_TypeError, |
| 164 | "codec search functions must return 4-tuples"); |
| 165 | Py_DECREF(result); |
| 166 | goto onError; |
| 167 | } |
| 168 | break; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 169 | } |
| 170 | if (i == len) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | /* XXX Perhaps we should cache misses too ? */ |
| 172 | PyErr_Format(PyExc_LookupError, |
Martin v. Löwis | eb42b02 | 2002-09-26 16:01:24 +0000 | [diff] [blame] | 173 | "unknown encoding: %s", encoding); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Cache and return the result */ |
Neal Norwitz | 9edcc2e | 2007-08-11 04:58:26 +0000 | [diff] [blame] | 178 | if (PyDict_SetItem(interp->codec_search_cache, v, result) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | Py_DECREF(result); |
| 180 | goto onError; |
Neal Norwitz | 9edcc2e | 2007-08-11 04:58:26 +0000 | [diff] [blame] | 181 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 182 | Py_DECREF(args); |
| 183 | return result; |
| 184 | |
| 185 | onError: |
| 186 | Py_XDECREF(args); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 190 | int _PyCodec_Forget(const char *encoding) |
| 191 | { |
| 192 | PyInterpreterState *interp; |
| 193 | PyObject *v; |
| 194 | int result; |
| 195 | |
| 196 | interp = PyThreadState_GET()->interp; |
| 197 | if (interp->codec_search_path == NULL) { |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | /* Convert the encoding to a normalized Python string: all |
| 202 | characters are converted to lower case, spaces and hyphens are |
| 203 | replaced with underscores. */ |
| 204 | v = normalizestring(encoding); |
| 205 | if (v == NULL) { |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | /* Drop the named codec from the internal cache */ |
| 210 | result = PyDict_DelItem(interp->codec_search_cache, v); |
| 211 | Py_DECREF(v); |
| 212 | |
| 213 | return result; |
| 214 | } |
| 215 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 216 | /* Codec registry encoding check API. */ |
| 217 | |
| 218 | int PyCodec_KnownEncoding(const char *encoding) |
| 219 | { |
| 220 | PyObject *codecs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 222 | codecs = _PyCodec_Lookup(encoding); |
| 223 | if (!codecs) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | PyErr_Clear(); |
| 225 | return 0; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 226 | } |
| 227 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | Py_DECREF(codecs); |
| 229 | return 1; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 233 | static |
| 234 | PyObject *args_tuple(PyObject *object, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 236 | { |
| 237 | PyObject *args; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 239 | args = PyTuple_New(1 + (errors != NULL)); |
| 240 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | return NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 242 | Py_INCREF(object); |
| 243 | PyTuple_SET_ITEM(args,0,object); |
| 244 | if (errors) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyObject *v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | v = PyUnicode_FromString(errors); |
| 248 | if (v == NULL) { |
| 249 | Py_DECREF(args); |
| 250 | return NULL; |
| 251 | } |
| 252 | PyTuple_SET_ITEM(args, 1, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 253 | } |
| 254 | return args; |
| 255 | } |
| 256 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 257 | /* Helper function to get a codec item */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 258 | |
| 259 | static |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | PyObject *codec_getitem(const char *encoding, int index) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 261 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 262 | PyObject *codecs; |
| 263 | PyObject *v; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 264 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 265 | codecs = _PyCodec_Lookup(encoding); |
| 266 | if (codecs == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 268 | v = PyTuple_GET_ITEM(codecs, index); |
| 269 | Py_DECREF(codecs); |
| 270 | Py_INCREF(v); |
| 271 | return v; |
| 272 | } |
| 273 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 274 | /* Helper functions to create an incremental codec. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 275 | static |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 276 | PyObject *codec_makeincrementalcodec(PyObject *codec_info, |
| 277 | const char *errors, |
| 278 | const char *attrname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 279 | { |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 280 | PyObject *ret, *inccodec; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 282 | inccodec = PyObject_GetAttrString(codec_info, attrname); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | if (inccodec == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 285 | if (errors) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | ret = PyObject_CallFunction(inccodec, "s", errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | else |
Victor Stinner | 4778eab | 2016-12-01 14:51:04 +0100 | [diff] [blame] | 288 | ret = _PyObject_CallNoArg(inccodec); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 289 | Py_DECREF(inccodec); |
| 290 | return ret; |
| 291 | } |
| 292 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 293 | static |
| 294 | PyObject *codec_getincrementalcodec(const char *encoding, |
| 295 | const char *errors, |
| 296 | const char *attrname) |
| 297 | { |
| 298 | PyObject *codec_info, *ret; |
| 299 | |
| 300 | codec_info = _PyCodec_Lookup(encoding); |
| 301 | if (codec_info == NULL) |
| 302 | return NULL; |
| 303 | ret = codec_makeincrementalcodec(codec_info, errors, attrname); |
| 304 | Py_DECREF(codec_info); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 308 | /* Helper function to create a stream codec. */ |
| 309 | |
| 310 | static |
| 311 | PyObject *codec_getstreamcodec(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | PyObject *stream, |
| 313 | const char *errors, |
| 314 | const int index) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 316 | PyObject *codecs, *streamcodec, *codeccls; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 317 | |
| 318 | codecs = _PyCodec_Lookup(encoding); |
| 319 | if (codecs == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 321 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 322 | codeccls = PyTuple_GET_ITEM(codecs, index); |
| 323 | if (errors != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 325 | else |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 326 | streamcodec = PyObject_CallFunctionObjArgs(codeccls, stream, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 327 | Py_DECREF(codecs); |
| 328 | return streamcodec; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 331 | /* Helpers to work with the result of _PyCodec_Lookup |
| 332 | |
| 333 | */ |
| 334 | PyObject *_PyCodecInfo_GetIncrementalDecoder(PyObject *codec_info, |
| 335 | const char *errors) |
| 336 | { |
| 337 | return codec_makeincrementalcodec(codec_info, errors, |
| 338 | "incrementaldecoder"); |
| 339 | } |
| 340 | |
| 341 | PyObject *_PyCodecInfo_GetIncrementalEncoder(PyObject *codec_info, |
| 342 | const char *errors) |
| 343 | { |
| 344 | return codec_makeincrementalcodec(codec_info, errors, |
| 345 | "incrementalencoder"); |
| 346 | } |
| 347 | |
| 348 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 349 | /* Convenience APIs to query the Codec registry. |
| 350 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 351 | All APIs return a codec object with incremented refcount. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 353 | */ |
| 354 | |
| 355 | PyObject *PyCodec_Encoder(const char *encoding) |
| 356 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 357 | return codec_getitem(encoding, 0); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | PyObject *PyCodec_Decoder(const char *encoding) |
| 361 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 362 | return codec_getitem(encoding, 1); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 365 | PyObject *PyCodec_IncrementalEncoder(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | const char *errors) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 367 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | PyObject *PyCodec_IncrementalDecoder(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | const char *errors) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 373 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 377 | PyObject *PyCodec_StreamReader(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | PyObject *stream, |
| 379 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 380 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | return codec_getstreamcodec(encoding, stream, errors, 2); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | PyObject *PyCodec_StreamWriter(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | PyObject *stream, |
| 386 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 387 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 388 | return codec_getstreamcodec(encoding, stream, errors, 3); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 391 | /* Helper that tries to ensure the reported exception chain indicates the |
| 392 | * codec that was invoked to trigger the failure without changing the type |
| 393 | * of the exception raised. |
| 394 | */ |
| 395 | static void |
| 396 | wrap_codec_error(const char *operation, |
| 397 | const char *encoding) |
| 398 | { |
| 399 | /* TrySetFromCause will replace the active exception with a suitably |
| 400 | * updated clone if it can, otherwise it will leave the original |
| 401 | * exception alone. |
| 402 | */ |
| 403 | _PyErr_TrySetFromCause("%s with '%s' codec failed", |
| 404 | operation, encoding); |
| 405 | } |
| 406 | |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 407 | /* Encode an object (e.g. a Unicode object) using the given encoding |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 408 | and return the resulting encoded object (usually a Python string). |
| 409 | |
| 410 | errors is passed to the encoder factory as argument if non-NULL. */ |
| 411 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 412 | static PyObject * |
| 413 | _PyCodec_EncodeInternal(PyObject *object, |
| 414 | PyObject *encoder, |
| 415 | const char *encoding, |
| 416 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 417 | { |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 418 | PyObject *args = NULL, *result = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 419 | PyObject *v = NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 421 | args = args_tuple(object, errors); |
| 422 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 424 | |
| 425 | result = PyEval_CallObject(encoder, args); |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 426 | if (result == NULL) { |
| 427 | wrap_codec_error("encoding", encoding); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | goto onError; |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 429 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 431 | if (!PyTuple_Check(result) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | PyTuple_GET_SIZE(result) != 2) { |
| 433 | PyErr_SetString(PyExc_TypeError, |
| 434 | "encoder must return a tuple (object, integer)"); |
| 435 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 436 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 437 | v = PyTuple_GET_ITEM(result,0); |
| 438 | Py_INCREF(v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 439 | /* We don't check or use the second (integer) entry. */ |
| 440 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 441 | Py_DECREF(args); |
| 442 | Py_DECREF(encoder); |
| 443 | Py_DECREF(result); |
| 444 | return v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 446 | onError: |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 447 | Py_XDECREF(result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 448 | Py_XDECREF(args); |
| 449 | Py_XDECREF(encoder); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 450 | return NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Decode an object (usually a Python string) using the given encoding |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 454 | and return an equivalent object (e.g. a Unicode object). |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 455 | |
| 456 | errors is passed to the decoder factory as argument if non-NULL. */ |
| 457 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 458 | static PyObject * |
| 459 | _PyCodec_DecodeInternal(PyObject *object, |
| 460 | PyObject *decoder, |
| 461 | const char *encoding, |
| 462 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 463 | { |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 464 | PyObject *args = NULL, *result = NULL; |
| 465 | PyObject *v; |
| 466 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 467 | args = args_tuple(object, errors); |
| 468 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 471 | result = PyEval_CallObject(decoder,args); |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 472 | if (result == NULL) { |
| 473 | wrap_codec_error("decoding", encoding); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | goto onError; |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 475 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 476 | if (!PyTuple_Check(result) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyTuple_GET_SIZE(result) != 2) { |
| 478 | PyErr_SetString(PyExc_TypeError, |
| 479 | "decoder must return a tuple (object,integer)"); |
| 480 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 481 | } |
| 482 | v = PyTuple_GET_ITEM(result,0); |
| 483 | Py_INCREF(v); |
| 484 | /* We don't check or use the second (integer) entry. */ |
| 485 | |
| 486 | Py_DECREF(args); |
| 487 | Py_DECREF(decoder); |
| 488 | Py_DECREF(result); |
| 489 | return v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 491 | onError: |
| 492 | Py_XDECREF(args); |
| 493 | Py_XDECREF(decoder); |
| 494 | Py_XDECREF(result); |
| 495 | return NULL; |
| 496 | } |
| 497 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 498 | /* Generic encoding/decoding API */ |
| 499 | PyObject *PyCodec_Encode(PyObject *object, |
| 500 | const char *encoding, |
| 501 | const char *errors) |
| 502 | { |
| 503 | PyObject *encoder; |
| 504 | |
| 505 | encoder = PyCodec_Encoder(encoding); |
| 506 | if (encoder == NULL) |
| 507 | return NULL; |
| 508 | |
| 509 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 510 | } |
| 511 | |
| 512 | PyObject *PyCodec_Decode(PyObject *object, |
| 513 | const char *encoding, |
| 514 | const char *errors) |
| 515 | { |
| 516 | PyObject *decoder; |
| 517 | |
| 518 | decoder = PyCodec_Decoder(encoding); |
| 519 | if (decoder == NULL) |
| 520 | return NULL; |
| 521 | |
| 522 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 523 | } |
| 524 | |
| 525 | /* Text encoding/decoding API */ |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 526 | PyObject * _PyCodec_LookupTextEncoding(const char *encoding, |
| 527 | const char *alternate_command) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 528 | { |
| 529 | _Py_IDENTIFIER(_is_text_encoding); |
| 530 | PyObject *codec; |
| 531 | PyObject *attr; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 532 | int is_text_codec; |
| 533 | |
| 534 | codec = _PyCodec_Lookup(encoding); |
| 535 | if (codec == NULL) |
| 536 | return NULL; |
| 537 | |
| 538 | /* Backwards compatibility: assume any raw tuple describes a text |
| 539 | * encoding, and the same for anything lacking the private |
| 540 | * attribute. |
| 541 | */ |
| 542 | if (!PyTuple_CheckExact(codec)) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 543 | if (_PyObject_LookupAttrId(codec, &PyId__is_text_encoding, &attr) < 0) { |
| 544 | Py_DECREF(codec); |
| 545 | return NULL; |
| 546 | } |
| 547 | if (attr != NULL) { |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 548 | is_text_codec = PyObject_IsTrue(attr); |
| 549 | Py_DECREF(attr); |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 550 | if (is_text_codec <= 0) { |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 551 | Py_DECREF(codec); |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 552 | if (!is_text_codec) |
| 553 | PyErr_Format(PyExc_LookupError, |
| 554 | "'%.400s' is not a text encoding; " |
| 555 | "use %s to handle arbitrary codecs", |
| 556 | encoding, alternate_command); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 562 | /* This appears to be a valid text encoding */ |
| 563 | return codec; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | static |
| 568 | PyObject *codec_getitem_checked(const char *encoding, |
| 569 | const char *alternate_command, |
| 570 | int index) |
| 571 | { |
| 572 | PyObject *codec; |
| 573 | PyObject *v; |
| 574 | |
| 575 | codec = _PyCodec_LookupTextEncoding(encoding, alternate_command); |
| 576 | if (codec == NULL) |
| 577 | return NULL; |
| 578 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 579 | v = PyTuple_GET_ITEM(codec, index); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 580 | Py_INCREF(v); |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 581 | Py_DECREF(codec); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 582 | return v; |
| 583 | } |
| 584 | |
| 585 | static PyObject * _PyCodec_TextEncoder(const char *encoding) |
| 586 | { |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 587 | return codec_getitem_checked(encoding, "codecs.encode()", 0); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static PyObject * _PyCodec_TextDecoder(const char *encoding) |
| 591 | { |
Nick Coghlan | a9b1524 | 2014-02-04 22:11:18 +1000 | [diff] [blame] | 592 | return codec_getitem_checked(encoding, "codecs.decode()", 1); |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | PyObject *_PyCodec_EncodeText(PyObject *object, |
| 596 | const char *encoding, |
| 597 | const char *errors) |
| 598 | { |
| 599 | PyObject *encoder; |
| 600 | |
| 601 | encoder = _PyCodec_TextEncoder(encoding); |
| 602 | if (encoder == NULL) |
| 603 | return NULL; |
| 604 | |
| 605 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 606 | } |
| 607 | |
| 608 | PyObject *_PyCodec_DecodeText(PyObject *object, |
| 609 | const char *encoding, |
| 610 | const char *errors) |
| 611 | { |
| 612 | PyObject *decoder; |
| 613 | |
| 614 | decoder = _PyCodec_TextDecoder(encoding); |
| 615 | if (decoder == NULL) |
| 616 | return NULL; |
| 617 | |
| 618 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 619 | } |
| 620 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 621 | /* Register the error handling callback function error under the name |
| 622 | name. This function will be called by the codec when it encounters |
| 623 | an unencodable characters/undecodable bytes and doesn't know the |
| 624 | callback name, when name is specified as the error parameter |
| 625 | in the call to the encode/decode function. |
| 626 | Return 0 on success, -1 on error */ |
| 627 | int PyCodec_RegisterError(const char *name, PyObject *error) |
| 628 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 629 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 630 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 632 | if (!PyCallable_Check(error)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | PyErr_SetString(PyExc_TypeError, "handler must be callable"); |
| 634 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 635 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 636 | return PyDict_SetItemString(interp->codec_error_registry, |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 637 | name, error); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Lookup the error handling callback function registered under the |
| 641 | name error. As a special case NULL can be passed, in which case |
| 642 | the error handling callback for strict encoding will be returned. */ |
| 643 | PyObject *PyCodec_LookupError(const char *name) |
| 644 | { |
| 645 | PyObject *handler = NULL; |
| 646 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 647 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 648 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | return NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 650 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 651 | if (name==NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | name = "strict"; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 653 | handler = PyDict_GetItemString(interp->codec_error_registry, name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 654 | if (!handler) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 656 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | Py_INCREF(handler); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 658 | return handler; |
| 659 | } |
| 660 | |
| 661 | static void wrong_exception_type(PyObject *exc) |
| 662 | { |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 663 | PyErr_Format(PyExc_TypeError, |
| 664 | "don't know how to handle %.200s in error callback", |
| 665 | exc->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | PyObject *PyCodec_StrictErrors(PyObject *exc) |
| 669 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 670 | if (PyExceptionInstance_Check(exc)) |
| 671 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 672 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | PyObject *PyCodec_IgnoreErrors(PyObject *exc) |
| 679 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 680 | Py_ssize_t end; |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 681 | |
| 682 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 684 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 685 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 686 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 688 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 689 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 690 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 692 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 693 | } |
| 694 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | wrong_exception_type(exc); |
| 696 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 697 | } |
Victor Stinner | ee45009 | 2011-12-01 02:52:11 +0100 | [diff] [blame] | 698 | return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | PyObject *PyCodec_ReplaceErrors(PyObject *exc) |
| 703 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 704 | Py_ssize_t start, end, i, len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 705 | |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 706 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | PyObject *res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 708 | int kind; |
| 709 | void *data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 711 | return NULL; |
| 712 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 713 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 714 | len = end - start; |
| 715 | res = PyUnicode_New(len, '?'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | if (res == NULL) |
| 717 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 718 | kind = PyUnicode_KIND(res); |
| 719 | data = PyUnicode_DATA(res); |
| 720 | for (i = 0; i < len; ++i) |
| 721 | PyUnicode_WRITE(kind, data, i, '?'); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 722 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 723 | return Py_BuildValue("(Nn)", res, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 724 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 725 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 727 | return NULL; |
Victor Stinner | 1a15aba | 2011-10-02 19:00:15 +0200 | [diff] [blame] | 728 | return Py_BuildValue("(Cn)", |
| 729 | (int)Py_UNICODE_REPLACEMENT_CHARACTER, |
| 730 | end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 731 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 732 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | PyObject *res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 734 | int kind; |
| 735 | void *data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | if (PyUnicodeTranslateError_GetStart(exc, &start)) |
| 737 | return NULL; |
| 738 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 739 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 740 | len = end - start; |
| 741 | res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | if (res == NULL) |
| 743 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 744 | kind = PyUnicode_KIND(res); |
| 745 | data = PyUnicode_DATA(res); |
| 746 | for (i=0; i < len; i++) |
| 747 | PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 748 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 749 | return Py_BuildValue("(Nn)", res, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 750 | } |
| 751 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | wrong_exception_type(exc); |
| 753 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
| 758 | { |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 759 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | PyObject *restuple; |
| 761 | PyObject *object; |
Victor Stinner | b31f1bc | 2011-11-04 21:29:10 +0100 | [diff] [blame] | 762 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | Py_ssize_t start; |
| 764 | Py_ssize_t end; |
| 765 | PyObject *res; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 766 | unsigned char *outp; |
Serhiy Storchaka | 2e37409 | 2014-10-04 14:15:49 +0300 | [diff] [blame] | 767 | Py_ssize_t ressize; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 768 | Py_UCS4 ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 770 | return NULL; |
| 771 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 772 | return NULL; |
| 773 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 774 | return NULL; |
Serhiy Storchaka | 2e37409 | 2014-10-04 14:15:49 +0300 | [diff] [blame] | 775 | if (end - start > PY_SSIZE_T_MAX / (2+7+1)) |
| 776 | end = start + PY_SSIZE_T_MAX / (2+7+1); |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 777 | for (i = start, ressize = 0; i < end; ++i) { |
| 778 | /* object is guaranteed to be "ready" */ |
| 779 | ch = PyUnicode_READ_CHAR(object, i); |
| 780 | if (ch<10) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | ressize += 2+1+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 782 | else if (ch<100) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | ressize += 2+2+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 784 | else if (ch<1000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | ressize += 2+3+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 786 | else if (ch<10000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | ressize += 2+4+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 788 | else if (ch<100000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | ressize += 2+5+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 790 | else if (ch<1000000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | ressize += 2+6+1; |
| 792 | else |
| 793 | ressize += 2+7+1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | } |
| 795 | /* allocate replacement */ |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 796 | res = PyUnicode_New(ressize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | if (res == NULL) { |
| 798 | Py_DECREF(object); |
| 799 | return NULL; |
| 800 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 801 | outp = PyUnicode_1BYTE_DATA(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | /* generate replacement */ |
Victor Stinner | b31f1bc | 2011-11-04 21:29:10 +0100 | [diff] [blame] | 803 | for (i = start; i < end; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | int digits; |
| 805 | int base; |
Martin v. Löwis | 8ba7930 | 2011-11-04 12:26:49 +0100 | [diff] [blame] | 806 | ch = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | *outp++ = '&'; |
| 808 | *outp++ = '#'; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 809 | if (ch<10) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | digits = 1; |
| 811 | base = 1; |
| 812 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 813 | else if (ch<100) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | digits = 2; |
| 815 | base = 10; |
| 816 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 817 | else if (ch<1000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | digits = 3; |
| 819 | base = 100; |
| 820 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 821 | else if (ch<10000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | digits = 4; |
| 823 | base = 1000; |
| 824 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 825 | else if (ch<100000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | digits = 5; |
| 827 | base = 10000; |
| 828 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 829 | else if (ch<1000000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | digits = 6; |
| 831 | base = 100000; |
| 832 | } |
| 833 | else { |
| 834 | digits = 7; |
| 835 | base = 1000000; |
| 836 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | while (digits-->0) { |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 838 | *outp++ = '0' + ch/base; |
| 839 | ch %= base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | base /= 10; |
| 841 | } |
| 842 | *outp++ = ';'; |
| 843 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 844 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 845 | restuple = Py_BuildValue("(Nn)", res, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | Py_DECREF(object); |
| 847 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 848 | } |
| 849 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | wrong_exception_type(exc); |
| 851 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 855 | PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) |
| 856 | { |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 857 | PyObject *object; |
| 858 | Py_ssize_t i; |
| 859 | Py_ssize_t start; |
| 860 | Py_ssize_t end; |
| 861 | PyObject *res; |
| 862 | unsigned char *outp; |
| 863 | int ressize; |
| 864 | Py_UCS4 c; |
| 865 | |
Serhiy Storchaka | c0937f7 | 2015-05-18 16:10:40 +0300 | [diff] [blame] | 866 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 867 | const unsigned char *p; |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 868 | if (PyUnicodeDecodeError_GetStart(exc, &start)) |
| 869 | return NULL; |
| 870 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 871 | return NULL; |
| 872 | if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
| 873 | return NULL; |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 874 | p = (const unsigned char*)PyBytes_AS_STRING(object); |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 875 | res = PyUnicode_New(4 * (end - start), 127); |
| 876 | if (res == NULL) { |
| 877 | Py_DECREF(object); |
| 878 | return NULL; |
| 879 | } |
| 880 | outp = PyUnicode_1BYTE_DATA(res); |
| 881 | for (i = start; i < end; i++, outp += 4) { |
| 882 | unsigned char c = p[i]; |
| 883 | outp[0] = '\\'; |
| 884 | outp[1] = 'x'; |
| 885 | outp[2] = Py_hexdigits[(c>>4)&0xf]; |
| 886 | outp[3] = Py_hexdigits[c&0xf]; |
| 887 | } |
| 888 | |
| 889 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 890 | Py_DECREF(object); |
| 891 | return Py_BuildValue("(Nn)", res, end); |
| 892 | } |
Serhiy Storchaka | c0937f7 | 2015-05-18 16:10:40 +0300 | [diff] [blame] | 893 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 895 | return NULL; |
| 896 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 897 | return NULL; |
| 898 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 899 | return NULL; |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 900 | } |
Serhiy Storchaka | c0937f7 | 2015-05-18 16:10:40 +0300 | [diff] [blame] | 901 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 902 | if (PyUnicodeTranslateError_GetStart(exc, &start)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | return NULL; |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 904 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 905 | return NULL; |
| 906 | if (!(object = PyUnicodeTranslateError_GetObject(exc))) |
| 907 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 908 | } |
| 909 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | wrong_exception_type(exc); |
| 911 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 912 | } |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 913 | |
| 914 | if (end - start > PY_SSIZE_T_MAX / (1+1+8)) |
| 915 | end = start + PY_SSIZE_T_MAX / (1+1+8); |
| 916 | for (i = start, ressize = 0; i < end; ++i) { |
| 917 | /* object is guaranteed to be "ready" */ |
| 918 | c = PyUnicode_READ_CHAR(object, i); |
| 919 | if (c >= 0x10000) { |
| 920 | ressize += 1+1+8; |
| 921 | } |
| 922 | else if (c >= 0x100) { |
| 923 | ressize += 1+1+4; |
| 924 | } |
| 925 | else |
| 926 | ressize += 1+1+2; |
| 927 | } |
| 928 | res = PyUnicode_New(ressize, 127); |
| 929 | if (res == NULL) { |
| 930 | Py_DECREF(object); |
| 931 | return NULL; |
| 932 | } |
| 933 | outp = PyUnicode_1BYTE_DATA(res); |
| 934 | for (i = start; i < end; ++i) { |
| 935 | c = PyUnicode_READ_CHAR(object, i); |
| 936 | *outp++ = '\\'; |
| 937 | if (c >= 0x00010000) { |
| 938 | *outp++ = 'U'; |
| 939 | *outp++ = Py_hexdigits[(c>>28)&0xf]; |
| 940 | *outp++ = Py_hexdigits[(c>>24)&0xf]; |
| 941 | *outp++ = Py_hexdigits[(c>>20)&0xf]; |
| 942 | *outp++ = Py_hexdigits[(c>>16)&0xf]; |
| 943 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 944 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
| 945 | } |
| 946 | else if (c >= 0x100) { |
| 947 | *outp++ = 'u'; |
| 948 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 949 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
| 950 | } |
| 951 | else |
| 952 | *outp++ = 'x'; |
| 953 | *outp++ = Py_hexdigits[(c>>4)&0xf]; |
| 954 | *outp++ = Py_hexdigits[c&0xf]; |
| 955 | } |
| 956 | |
| 957 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 958 | Py_DECREF(object); |
| 959 | return Py_BuildValue("(Nn)", res, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 962 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 963 | |
| 964 | PyObject *PyCodec_NameReplaceErrors(PyObject *exc) |
| 965 | { |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 966 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 967 | PyObject *restuple; |
| 968 | PyObject *object; |
| 969 | Py_ssize_t i; |
| 970 | Py_ssize_t start; |
| 971 | Py_ssize_t end; |
| 972 | PyObject *res; |
| 973 | unsigned char *outp; |
Serhiy Storchaka | aacfccc | 2014-11-26 12:11:40 +0200 | [diff] [blame] | 974 | Py_ssize_t ressize; |
| 975 | int replsize; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 976 | Py_UCS4 c; |
| 977 | char buffer[256]; /* NAME_MAXLEN */ |
| 978 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 979 | return NULL; |
| 980 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 981 | return NULL; |
| 982 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 983 | return NULL; |
Victor Stinner | 38b8ae0 | 2015-09-03 16:19:40 +0200 | [diff] [blame] | 984 | if (!ucnhash_CAPI) { |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 985 | /* load the unicode data module */ |
| 986 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 987 | PyUnicodeData_CAPSULE_NAME, 1); |
Victor Stinner | 38b8ae0 | 2015-09-03 16:19:40 +0200 | [diff] [blame] | 988 | if (!ucnhash_CAPI) |
| 989 | return NULL; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 990 | } |
| 991 | for (i = start, ressize = 0; i < end; ++i) { |
| 992 | /* object is guaranteed to be "ready" */ |
| 993 | c = PyUnicode_READ_CHAR(object, i); |
Victor Stinner | 38b8ae0 | 2015-09-03 16:19:40 +0200 | [diff] [blame] | 994 | if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) { |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 995 | replsize = 1+1+1+(int)strlen(buffer)+1; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 996 | } |
| 997 | else if (c >= 0x10000) { |
Serhiy Storchaka | aacfccc | 2014-11-26 12:11:40 +0200 | [diff] [blame] | 998 | replsize = 1+1+8; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 999 | } |
| 1000 | else if (c >= 0x100) { |
Serhiy Storchaka | aacfccc | 2014-11-26 12:11:40 +0200 | [diff] [blame] | 1001 | replsize = 1+1+4; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1002 | } |
| 1003 | else |
Serhiy Storchaka | aacfccc | 2014-11-26 12:11:40 +0200 | [diff] [blame] | 1004 | replsize = 1+1+2; |
| 1005 | if (ressize > PY_SSIZE_T_MAX - replsize) |
| 1006 | break; |
| 1007 | ressize += replsize; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1008 | } |
Serhiy Storchaka | aacfccc | 2014-11-26 12:11:40 +0200 | [diff] [blame] | 1009 | end = i; |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1010 | res = PyUnicode_New(ressize, 127); |
| 1011 | if (res==NULL) |
| 1012 | return NULL; |
| 1013 | for (i = start, outp = PyUnicode_1BYTE_DATA(res); |
| 1014 | i < end; ++i) { |
| 1015 | c = PyUnicode_READ_CHAR(object, i); |
| 1016 | *outp++ = '\\'; |
Victor Stinner | 38b8ae0 | 2015-09-03 16:19:40 +0200 | [diff] [blame] | 1017 | if (ucnhash_CAPI->getname(NULL, c, buffer, sizeof(buffer), 1)) { |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1018 | *outp++ = 'N'; |
| 1019 | *outp++ = '{'; |
| 1020 | strcpy((char *)outp, buffer); |
| 1021 | outp += strlen(buffer); |
| 1022 | *outp++ = '}'; |
| 1023 | continue; |
| 1024 | } |
| 1025 | if (c >= 0x00010000) { |
| 1026 | *outp++ = 'U'; |
| 1027 | *outp++ = Py_hexdigits[(c>>28)&0xf]; |
| 1028 | *outp++ = Py_hexdigits[(c>>24)&0xf]; |
| 1029 | *outp++ = Py_hexdigits[(c>>20)&0xf]; |
| 1030 | *outp++ = Py_hexdigits[(c>>16)&0xf]; |
| 1031 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 1032 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
| 1033 | } |
| 1034 | else if (c >= 0x100) { |
| 1035 | *outp++ = 'u'; |
| 1036 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 1037 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
| 1038 | } |
| 1039 | else |
| 1040 | *outp++ = 'x'; |
| 1041 | *outp++ = Py_hexdigits[(c>>4)&0xf]; |
| 1042 | *outp++ = Py_hexdigits[c&0xf]; |
| 1043 | } |
| 1044 | |
Benjamin Peterson | 3663b58 | 2014-11-26 14:39:54 -0600 | [diff] [blame] | 1045 | assert(outp == PyUnicode_1BYTE_DATA(res) + ressize); |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1046 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1047 | restuple = Py_BuildValue("(Nn)", res, end); |
| 1048 | Py_DECREF(object); |
| 1049 | return restuple; |
| 1050 | } |
| 1051 | else { |
| 1052 | wrong_exception_type(exc); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | } |
| 1056 | |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 1057 | #define ENC_UNKNOWN -1 |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1058 | #define ENC_UTF8 0 |
| 1059 | #define ENC_UTF16BE 1 |
| 1060 | #define ENC_UTF16LE 2 |
| 1061 | #define ENC_UTF32BE 3 |
| 1062 | #define ENC_UTF32LE 4 |
| 1063 | |
| 1064 | static int |
| 1065 | get_standard_encoding(const char *encoding, int *bytelength) |
| 1066 | { |
| 1067 | if (Py_TOLOWER(encoding[0]) == 'u' && |
| 1068 | Py_TOLOWER(encoding[1]) == 't' && |
| 1069 | Py_TOLOWER(encoding[2]) == 'f') { |
| 1070 | encoding += 3; |
| 1071 | if (*encoding == '-' || *encoding == '_' ) |
| 1072 | encoding++; |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 1073 | if (encoding[0] == '8' && encoding[1] == '\0') { |
| 1074 | *bytelength = 3; |
| 1075 | return ENC_UTF8; |
| 1076 | } |
| 1077 | else if (encoding[0] == '1' && encoding[1] == '6') { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1078 | encoding += 2; |
| 1079 | *bytelength = 2; |
| 1080 | if (*encoding == '\0') { |
| 1081 | #ifdef WORDS_BIGENDIAN |
| 1082 | return ENC_UTF16BE; |
| 1083 | #else |
| 1084 | return ENC_UTF16LE; |
| 1085 | #endif |
| 1086 | } |
| 1087 | if (*encoding == '-' || *encoding == '_' ) |
| 1088 | encoding++; |
| 1089 | if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') { |
| 1090 | if (Py_TOLOWER(encoding[0]) == 'b') |
| 1091 | return ENC_UTF16BE; |
| 1092 | if (Py_TOLOWER(encoding[0]) == 'l') |
| 1093 | return ENC_UTF16LE; |
| 1094 | } |
| 1095 | } |
| 1096 | else if (encoding[0] == '3' && encoding[1] == '2') { |
| 1097 | encoding += 2; |
| 1098 | *bytelength = 4; |
| 1099 | if (*encoding == '\0') { |
| 1100 | #ifdef WORDS_BIGENDIAN |
| 1101 | return ENC_UTF32BE; |
| 1102 | #else |
| 1103 | return ENC_UTF32LE; |
| 1104 | #endif |
| 1105 | } |
| 1106 | if (*encoding == '-' || *encoding == '_' ) |
| 1107 | encoding++; |
| 1108 | if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') { |
| 1109 | if (Py_TOLOWER(encoding[0]) == 'b') |
| 1110 | return ENC_UTF32BE; |
| 1111 | if (Py_TOLOWER(encoding[0]) == 'l') |
| 1112 | return ENC_UTF32LE; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
Victor Stinner | 0d4e01c | 2014-05-16 14:46:20 +0200 | [diff] [blame] | 1116 | else if (strcmp(encoding, "CP_UTF8") == 0) { |
| 1117 | *bytelength = 3; |
| 1118 | return ENC_UTF8; |
| 1119 | } |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 1120 | return ENC_UNKNOWN; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Martin v. Löwis | aef3fb0 | 2009-05-02 19:27:30 +0000 | [diff] [blame] | 1123 | /* This handler is declared static until someone demonstrates |
| 1124 | a need to call it directly. */ |
| 1125 | static PyObject * |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1126 | PyCodec_SurrogatePassErrors(PyObject *exc) |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1127 | { |
| 1128 | PyObject *restuple; |
| 1129 | PyObject *object; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1130 | PyObject *encode; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1131 | const char *encoding; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1132 | int code; |
| 1133 | int bytelength; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 1134 | Py_ssize_t i; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1135 | Py_ssize_t start; |
| 1136 | Py_ssize_t end; |
| 1137 | PyObject *res; |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 1138 | |
| 1139 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1140 | unsigned char *outp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 1142 | return NULL; |
| 1143 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 1144 | return NULL; |
| 1145 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 1146 | return NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1147 | if (!(encode = PyUnicodeEncodeError_GetEncoding(exc))) { |
| 1148 | Py_DECREF(object); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | if (!(encoding = PyUnicode_AsUTF8(encode))) { |
| 1152 | Py_DECREF(object); |
| 1153 | Py_DECREF(encode); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | code = get_standard_encoding(encoding, &bytelength); |
| 1157 | Py_DECREF(encode); |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 1158 | if (code == ENC_UNKNOWN) { |
| 1159 | /* Not supported, fail with original exception */ |
| 1160 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1161 | Py_DECREF(object); |
| 1162 | return NULL; |
| 1163 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1164 | |
Serhiy Storchaka | 2e37409 | 2014-10-04 14:15:49 +0300 | [diff] [blame] | 1165 | if (end - start > PY_SSIZE_T_MAX / bytelength) |
| 1166 | end = start + PY_SSIZE_T_MAX / bytelength; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1167 | res = PyBytes_FromStringAndSize(NULL, bytelength*(end-start)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | if (!res) { |
| 1169 | Py_DECREF(object); |
| 1170 | return NULL; |
| 1171 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1172 | outp = (unsigned char*)PyBytes_AsString(res); |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 1173 | for (i = start; i < end; i++) { |
| 1174 | /* object is guaranteed to be "ready" */ |
| 1175 | Py_UCS4 ch = PyUnicode_READ_CHAR(object, i); |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 1176 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* Not a surrogate, fail with original exception */ |
| 1178 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1179 | Py_DECREF(res); |
| 1180 | Py_DECREF(object); |
| 1181 | return NULL; |
| 1182 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1183 | switch (code) { |
| 1184 | case ENC_UTF8: |
| 1185 | *outp++ = (unsigned char)(0xe0 | (ch >> 12)); |
| 1186 | *outp++ = (unsigned char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1187 | *outp++ = (unsigned char)(0x80 | (ch & 0x3f)); |
| 1188 | break; |
| 1189 | case ENC_UTF16LE: |
| 1190 | *outp++ = (unsigned char) ch; |
| 1191 | *outp++ = (unsigned char)(ch >> 8); |
| 1192 | break; |
| 1193 | case ENC_UTF16BE: |
| 1194 | *outp++ = (unsigned char)(ch >> 8); |
| 1195 | *outp++ = (unsigned char) ch; |
| 1196 | break; |
| 1197 | case ENC_UTF32LE: |
| 1198 | *outp++ = (unsigned char) ch; |
| 1199 | *outp++ = (unsigned char)(ch >> 8); |
| 1200 | *outp++ = (unsigned char)(ch >> 16); |
| 1201 | *outp++ = (unsigned char)(ch >> 24); |
| 1202 | break; |
| 1203 | case ENC_UTF32BE: |
| 1204 | *outp++ = (unsigned char)(ch >> 24); |
| 1205 | *outp++ = (unsigned char)(ch >> 16); |
| 1206 | *outp++ = (unsigned char)(ch >> 8); |
| 1207 | *outp++ = (unsigned char) ch; |
| 1208 | break; |
| 1209 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | } |
| 1211 | restuple = Py_BuildValue("(On)", res, end); |
| 1212 | Py_DECREF(res); |
| 1213 | Py_DECREF(object); |
| 1214 | return restuple; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1215 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 1216 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 1217 | const unsigned char *p; |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1218 | Py_UCS4 ch = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | if (PyUnicodeDecodeError_GetStart(exc, &start)) |
| 1220 | return NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1221 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 1222 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
| 1224 | return NULL; |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 1225 | p = (const unsigned char*)PyBytes_AS_STRING(object); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1226 | if (!(encode = PyUnicodeDecodeError_GetEncoding(exc))) { |
| 1227 | Py_DECREF(object); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | if (!(encoding = PyUnicode_AsUTF8(encode))) { |
| 1231 | Py_DECREF(object); |
| 1232 | Py_DECREF(encode); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | code = get_standard_encoding(encoding, &bytelength); |
| 1236 | Py_DECREF(encode); |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 1237 | if (code == ENC_UNKNOWN) { |
| 1238 | /* Not supported, fail with original exception */ |
| 1239 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1240 | Py_DECREF(object); |
| 1241 | return NULL; |
| 1242 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | /* Try decoding a single surrogate character. If |
| 1245 | there are more, let the codec call us again. */ |
| 1246 | p += start; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1247 | if (PyBytes_GET_SIZE(object) - start >= bytelength) { |
| 1248 | switch (code) { |
| 1249 | case ENC_UTF8: |
| 1250 | if ((p[0] & 0xf0) == 0xe0 && |
| 1251 | (p[1] & 0xc0) == 0x80 && |
| 1252 | (p[2] & 0xc0) == 0x80) { |
| 1253 | /* it's a three-byte code */ |
| 1254 | ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f); |
| 1255 | } |
| 1256 | break; |
| 1257 | case ENC_UTF16LE: |
| 1258 | ch = p[1] << 8 | p[0]; |
| 1259 | break; |
| 1260 | case ENC_UTF16BE: |
| 1261 | ch = p[0] << 8 | p[1]; |
| 1262 | break; |
| 1263 | case ENC_UTF32LE: |
| 1264 | ch = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; |
| 1265 | break; |
| 1266 | case ENC_UTF32BE: |
| 1267 | ch = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; |
| 1268 | break; |
| 1269 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | Py_DECREF(object); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1273 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
| 1274 | /* it's not a surrogate - fail */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1276 | return NULL; |
| 1277 | } |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1278 | res = PyUnicode_FromOrdinal(ch); |
| 1279 | if (res == NULL) |
| 1280 | return NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1281 | return Py_BuildValue("(Nn)", res, start + bytelength); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1282 | } |
| 1283 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | wrong_exception_type(exc); |
| 1285 | return NULL; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1289 | static PyObject * |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1290 | PyCodec_SurrogateEscapeErrors(PyObject *exc) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1291 | { |
| 1292 | PyObject *restuple; |
| 1293 | PyObject *object; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 1294 | Py_ssize_t i; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1295 | Py_ssize_t start; |
| 1296 | Py_ssize_t end; |
| 1297 | PyObject *res; |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 1298 | |
| 1299 | if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | char *outp; |
| 1301 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 1302 | return NULL; |
| 1303 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 1304 | return NULL; |
| 1305 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 1306 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | res = PyBytes_FromStringAndSize(NULL, end-start); |
| 1308 | if (!res) { |
| 1309 | Py_DECREF(object); |
| 1310 | return NULL; |
| 1311 | } |
| 1312 | outp = PyBytes_AsString(res); |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 1313 | for (i = start; i < end; i++) { |
| 1314 | /* object is guaranteed to be "ready" */ |
| 1315 | Py_UCS4 ch = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | if (ch < 0xdc80 || ch > 0xdcff) { |
| 1317 | /* Not a UTF-8b surrogate, fail with original exception */ |
| 1318 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1319 | Py_DECREF(res); |
| 1320 | Py_DECREF(object); |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | *outp++ = ch - 0xdc00; |
| 1324 | } |
| 1325 | restuple = Py_BuildValue("(On)", res, end); |
| 1326 | Py_DECREF(res); |
| 1327 | Py_DECREF(object); |
| 1328 | return restuple; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1329 | } |
Serhiy Storchaka | ca7fecb | 2015-05-18 16:08:52 +0300 | [diff] [blame] | 1330 | else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1331 | PyObject *str; |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 1332 | const unsigned char *p; |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1333 | Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | int consumed = 0; |
| 1335 | if (PyUnicodeDecodeError_GetStart(exc, &start)) |
| 1336 | return NULL; |
| 1337 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 1338 | return NULL; |
| 1339 | if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
| 1340 | return NULL; |
Serhiy Storchaka | cb33a01 | 2016-10-23 09:44:50 +0300 | [diff] [blame] | 1341 | p = (const unsigned char*)PyBytes_AS_STRING(object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | while (consumed < 4 && consumed < end-start) { |
| 1343 | /* Refuse to escape ASCII bytes. */ |
| 1344 | if (p[start+consumed] < 128) |
| 1345 | break; |
| 1346 | ch[consumed] = 0xdc00 + p[start+consumed]; |
| 1347 | consumed++; |
| 1348 | } |
| 1349 | Py_DECREF(object); |
| 1350 | if (!consumed) { |
| 1351 | /* codec complained about ASCII byte. */ |
| 1352 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1353 | return NULL; |
| 1354 | } |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1355 | str = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, ch, consumed); |
| 1356 | if (str == NULL) |
| 1357 | return NULL; |
| 1358 | return Py_BuildValue("(Nn)", str, start+consumed); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1359 | } |
| 1360 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | wrong_exception_type(exc); |
| 1362 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1367 | static PyObject *strict_errors(PyObject *self, PyObject *exc) |
| 1368 | { |
| 1369 | return PyCodec_StrictErrors(exc); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | static PyObject *ignore_errors(PyObject *self, PyObject *exc) |
| 1374 | { |
| 1375 | return PyCodec_IgnoreErrors(exc); |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | static PyObject *replace_errors(PyObject *self, PyObject *exc) |
| 1380 | { |
| 1381 | return PyCodec_ReplaceErrors(exc); |
| 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) |
| 1386 | { |
| 1387 | return PyCodec_XMLCharRefReplaceErrors(exc); |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) |
| 1392 | { |
| 1393 | return PyCodec_BackslashReplaceErrors(exc); |
| 1394 | } |
| 1395 | |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1396 | static PyObject *namereplace_errors(PyObject *self, PyObject *exc) |
| 1397 | { |
| 1398 | return PyCodec_NameReplaceErrors(exc); |
| 1399 | } |
| 1400 | |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1401 | static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc) |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1402 | { |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1403 | return PyCodec_SurrogatePassErrors(exc); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1406 | static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1407 | { |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1408 | return PyCodec_SurrogateEscapeErrors(exc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1411 | static int _PyCodecRegistry_Init(void) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1412 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1413 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | char *name; |
| 1415 | PyMethodDef def; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1416 | } methods[] = |
| 1417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | { |
| 1419 | "strict", |
| 1420 | { |
| 1421 | "strict_errors", |
| 1422 | strict_errors, |
| 1423 | METH_O, |
| 1424 | PyDoc_STR("Implements the 'strict' error handling, which " |
| 1425 | "raises a UnicodeError on coding errors.") |
| 1426 | } |
| 1427 | }, |
| 1428 | { |
| 1429 | "ignore", |
| 1430 | { |
| 1431 | "ignore_errors", |
| 1432 | ignore_errors, |
| 1433 | METH_O, |
| 1434 | PyDoc_STR("Implements the 'ignore' error handling, which " |
| 1435 | "ignores malformed data and continues.") |
| 1436 | } |
| 1437 | }, |
| 1438 | { |
| 1439 | "replace", |
| 1440 | { |
| 1441 | "replace_errors", |
| 1442 | replace_errors, |
| 1443 | METH_O, |
| 1444 | PyDoc_STR("Implements the 'replace' error handling, which " |
| 1445 | "replaces malformed data with a replacement marker.") |
| 1446 | } |
| 1447 | }, |
| 1448 | { |
| 1449 | "xmlcharrefreplace", |
| 1450 | { |
| 1451 | "xmlcharrefreplace_errors", |
| 1452 | xmlcharrefreplace_errors, |
| 1453 | METH_O, |
| 1454 | PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " |
| 1455 | "which replaces an unencodable character with the " |
| 1456 | "appropriate XML character reference.") |
| 1457 | } |
| 1458 | }, |
| 1459 | { |
| 1460 | "backslashreplace", |
| 1461 | { |
| 1462 | "backslashreplace_errors", |
| 1463 | backslashreplace_errors, |
| 1464 | METH_O, |
| 1465 | PyDoc_STR("Implements the 'backslashreplace' error handling, " |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 1466 | "which replaces malformed data with a backslashed " |
| 1467 | "escape sequence.") |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | } |
| 1469 | }, |
| 1470 | { |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 1471 | "namereplace", |
| 1472 | { |
| 1473 | "namereplace_errors", |
| 1474 | namereplace_errors, |
| 1475 | METH_O, |
| 1476 | PyDoc_STR("Implements the 'namereplace' error handling, " |
| 1477 | "which replaces an unencodable character with a " |
| 1478 | "\\N{...} escape sequence.") |
| 1479 | } |
| 1480 | }, |
| 1481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | "surrogatepass", |
| 1483 | { |
| 1484 | "surrogatepass", |
| 1485 | surrogatepass_errors, |
| 1486 | METH_O |
| 1487 | } |
| 1488 | }, |
| 1489 | { |
| 1490 | "surrogateescape", |
| 1491 | { |
| 1492 | "surrogateescape", |
| 1493 | surrogateescape_errors, |
| 1494 | METH_O |
| 1495 | } |
| 1496 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1497 | }; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 1499 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1500 | PyObject *mod; |
Neal Norwitz | 739a8f8 | 2004-07-08 01:55:58 +0000 | [diff] [blame] | 1501 | unsigned i; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (interp->codec_search_path != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | return 0; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1505 | |
| 1506 | interp->codec_search_path = PyList_New(0); |
| 1507 | interp->codec_search_cache = PyDict_New(); |
| 1508 | interp->codec_error_registry = PyDict_New(); |
| 1509 | |
| 1510 | if (interp->codec_error_registry) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1511 | for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) { |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 1512 | PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | int res; |
| 1514 | if (!func) |
| 1515 | Py_FatalError("can't initialize codec error registry"); |
| 1516 | res = PyCodec_RegisterError(methods[i].name, func); |
| 1517 | Py_DECREF(func); |
| 1518 | if (res) |
| 1519 | Py_FatalError("can't initialize codec error registry"); |
| 1520 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1521 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1522 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1523 | if (interp->codec_search_path == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | interp->codec_search_cache == NULL || |
| 1525 | interp->codec_error_registry == NULL) |
| 1526 | Py_FatalError("can't initialize codec registry"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1527 | |
Christian Heimes | 819b8bf | 2008-01-03 23:05:47 +0000 | [diff] [blame] | 1528 | mod = PyImport_ImportModuleNoBlock("encodings"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1529 | if (mod == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | return -1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1531 | } |
| 1532 | Py_DECREF(mod); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1533 | interp->codecs_initialized = 1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1534 | return 0; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1535 | } |