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