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