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 | |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 246 | /* Helper functions to create an incremental codec. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 247 | static |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 248 | PyObject *codec_makeincrementalcodec(PyObject *codec_info, |
| 249 | const char *errors, |
| 250 | const char *attrname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 251 | { |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 252 | PyObject *ret, *inccodec; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 253 | |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 254 | inccodec = PyObject_GetAttrString(codec_info, attrname); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 255 | if (inccodec == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 257 | if (errors) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | ret = PyObject_CallFunction(inccodec, "s", errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 259 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | ret = PyObject_CallFunction(inccodec, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | Py_DECREF(inccodec); |
| 262 | return ret; |
| 263 | } |
| 264 | |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 265 | static |
| 266 | PyObject *codec_getincrementalcodec(const char *encoding, |
| 267 | const char *errors, |
| 268 | const char *attrname) |
| 269 | { |
| 270 | PyObject *codec_info, *ret; |
| 271 | |
| 272 | codec_info = _PyCodec_Lookup(encoding); |
| 273 | if (codec_info == NULL) |
| 274 | return NULL; |
| 275 | ret = codec_makeincrementalcodec(codec_info, errors, attrname); |
| 276 | Py_DECREF(codec_info); |
| 277 | return ret; |
| 278 | } |
| 279 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 280 | /* Helper function to create a stream codec. */ |
| 281 | |
| 282 | static |
| 283 | PyObject *codec_getstreamcodec(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | PyObject *stream, |
| 285 | const char *errors, |
| 286 | const int index) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | PyObject *codecs, *streamcodec, *codeccls; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 289 | |
| 290 | codecs = _PyCodec_Lookup(encoding); |
| 291 | if (codecs == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 293 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | codeccls = PyTuple_GET_ITEM(codecs, index); |
| 295 | if (errors != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 297 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | streamcodec = PyObject_CallFunction(codeccls, "O", stream); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | Py_DECREF(codecs); |
| 300 | return streamcodec; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 303 | /* Helpers to work with the result of _PyCodec_Lookup |
| 304 | |
| 305 | */ |
| 306 | PyObject *_PyCodecInfo_GetIncrementalDecoder(PyObject *codec_info, |
| 307 | const char *errors) |
| 308 | { |
| 309 | return codec_makeincrementalcodec(codec_info, errors, |
| 310 | "incrementaldecoder"); |
| 311 | } |
| 312 | |
| 313 | PyObject *_PyCodecInfo_GetIncrementalEncoder(PyObject *codec_info, |
| 314 | const char *errors) |
| 315 | { |
| 316 | return codec_makeincrementalcodec(codec_info, errors, |
| 317 | "incrementalencoder"); |
| 318 | } |
| 319 | |
| 320 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 321 | /* Convenience APIs to query the Codec registry. |
| 322 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 323 | All APIs return a codec object with incremented refcount. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 325 | */ |
| 326 | |
| 327 | PyObject *PyCodec_Encoder(const char *encoding) |
| 328 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 329 | return codec_getitem(encoding, 0); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | PyObject *PyCodec_Decoder(const char *encoding) |
| 333 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 334 | return codec_getitem(encoding, 1); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 337 | PyObject *PyCodec_IncrementalEncoder(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | const char *errors) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 339 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 340 | return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | PyObject *PyCodec_IncrementalDecoder(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | const char *errors) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 345 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 349 | PyObject *PyCodec_StreamReader(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | PyObject *stream, |
| 351 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 352 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | return codec_getstreamcodec(encoding, stream, errors, 2); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | PyObject *PyCodec_StreamWriter(const char *encoding, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | PyObject *stream, |
| 358 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 359 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | return codec_getstreamcodec(encoding, stream, errors, 3); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* Encode an object (e.g. an Unicode object) using the given encoding |
| 364 | and return the resulting encoded object (usually a Python string). |
| 365 | |
| 366 | errors is passed to the encoder factory as argument if non-NULL. */ |
| 367 | |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 368 | static PyObject * |
| 369 | _PyCodec_EncodeInternal(PyObject *object, |
| 370 | PyObject *encoder, |
| 371 | const char *encoding, |
| 372 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 373 | { |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 374 | PyObject *args = NULL, *result = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 375 | PyObject *v = NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 377 | args = args_tuple(object, errors); |
| 378 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 380 | |
| 381 | result = PyEval_CallObject(encoder, args); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 382 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 384 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 385 | if (!PyTuple_Check(result) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | PyTuple_GET_SIZE(result) != 2) { |
| 387 | PyErr_SetString(PyExc_TypeError, |
| 388 | "encoder must return a tuple (object, integer)"); |
| 389 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 390 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 391 | v = PyTuple_GET_ITEM(result,0); |
| 392 | Py_INCREF(v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 393 | /* We don't check or use the second (integer) entry. */ |
| 394 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 395 | Py_DECREF(args); |
| 396 | Py_DECREF(encoder); |
| 397 | Py_DECREF(result); |
| 398 | return v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 400 | onError: |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 401 | Py_XDECREF(result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 402 | Py_XDECREF(args); |
| 403 | Py_XDECREF(encoder); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 404 | return NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* Decode an object (usually a Python string) using the given encoding |
| 408 | and return an equivalent object (e.g. an Unicode object). |
| 409 | |
| 410 | errors is passed to the decoder factory as argument if non-NULL. */ |
| 411 | |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 412 | static PyObject * |
| 413 | _PyCodec_DecodeInternal(PyObject *object, |
| 414 | PyObject *decoder, |
| 415 | const char *encoding, |
| 416 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 417 | { |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 418 | PyObject *args = NULL, *result = NULL; |
| 419 | PyObject *v; |
| 420 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 421 | args = args_tuple(object, errors); |
| 422 | if (args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 425 | result = PyEval_CallObject(decoder,args); |
| 426 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 428 | if (!PyTuple_Check(result) || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | PyTuple_GET_SIZE(result) != 2) { |
| 430 | PyErr_SetString(PyExc_TypeError, |
| 431 | "decoder must return a tuple (object,integer)"); |
| 432 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 433 | } |
| 434 | v = PyTuple_GET_ITEM(result,0); |
| 435 | Py_INCREF(v); |
| 436 | /* We don't check or use the second (integer) entry. */ |
| 437 | |
| 438 | Py_DECREF(args); |
| 439 | Py_DECREF(decoder); |
| 440 | Py_DECREF(result); |
| 441 | return v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 443 | onError: |
| 444 | Py_XDECREF(args); |
| 445 | Py_XDECREF(decoder); |
| 446 | Py_XDECREF(result); |
| 447 | return NULL; |
| 448 | } |
| 449 | |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 450 | /* Generic encoding/decoding API */ |
| 451 | PyObject *PyCodec_Encode(PyObject *object, |
| 452 | const char *encoding, |
| 453 | const char *errors) |
| 454 | { |
| 455 | PyObject *encoder; |
| 456 | |
| 457 | encoder = PyCodec_Encoder(encoding); |
| 458 | if (encoder == NULL) |
| 459 | return NULL; |
| 460 | |
| 461 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 462 | } |
| 463 | |
| 464 | PyObject *PyCodec_Decode(PyObject *object, |
| 465 | const char *encoding, |
| 466 | const char *errors) |
| 467 | { |
| 468 | PyObject *decoder; |
| 469 | |
| 470 | decoder = PyCodec_Decoder(encoding); |
| 471 | if (decoder == NULL) |
| 472 | return NULL; |
| 473 | |
| 474 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 475 | } |
| 476 | |
| 477 | /* Text encoding/decoding API */ |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 478 | PyObject * _PyCodec_LookupTextEncoding(const char *encoding, |
| 479 | const char *alternate_command) |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 480 | { |
| 481 | _Py_IDENTIFIER(_is_text_encoding); |
| 482 | PyObject *codec; |
| 483 | PyObject *attr; |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 484 | int is_text_codec; |
| 485 | |
| 486 | codec = _PyCodec_Lookup(encoding); |
| 487 | if (codec == NULL) |
| 488 | return NULL; |
| 489 | |
| 490 | /* Backwards compatibility: assume any raw tuple describes a text |
| 491 | * encoding, and the same for anything lacking the private |
| 492 | * attribute. |
| 493 | */ |
| 494 | if (!PyTuple_CheckExact(codec)) { |
| 495 | attr = _PyObject_GetAttrId(codec, &PyId__is_text_encoding); |
| 496 | if (attr == NULL) { |
| 497 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 498 | PyErr_Clear(); |
| 499 | } else { |
| 500 | Py_DECREF(codec); |
| 501 | return NULL; |
| 502 | } |
| 503 | } else { |
| 504 | is_text_codec = PyObject_IsTrue(attr); |
| 505 | Py_DECREF(attr); |
| 506 | if (!is_text_codec) { |
| 507 | Py_DECREF(codec); |
| 508 | PyErr_Format(PyExc_LookupError, |
| 509 | "'%.400s' is not a text encoding; " |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 510 | "use %s to handle arbitrary codecs", |
| 511 | encoding, alternate_command); |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 512 | return NULL; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 517 | /* This appears to be a valid text encoding */ |
| 518 | return codec; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | static |
| 523 | PyObject *codec_getitem_checked(const char *encoding, |
| 524 | const char *alternate_command, |
| 525 | int index) |
| 526 | { |
| 527 | PyObject *codec; |
| 528 | PyObject *v; |
| 529 | |
| 530 | codec = _PyCodec_LookupTextEncoding(encoding, alternate_command); |
| 531 | if (codec == NULL) |
| 532 | return NULL; |
| 533 | |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 534 | v = PyTuple_GET_ITEM(codec, index); |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 535 | Py_INCREF(v); |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 536 | Py_DECREF(codec); |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 537 | return v; |
| 538 | } |
| 539 | |
| 540 | static PyObject * _PyCodec_TextEncoder(const char *encoding) |
| 541 | { |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 542 | return codec_getitem_checked(encoding, "codecs.encode()", 0); |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static PyObject * _PyCodec_TextDecoder(const char *encoding) |
| 546 | { |
Georg Brandl | 2fc8f77 | 2014-03-02 09:18:31 +0100 | [diff] [blame^] | 547 | return codec_getitem_checked(encoding, "codecs.decode()", 1); |
Serhiy Storchaka | 94ee389 | 2014-02-24 14:43:03 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | PyObject *_PyCodec_EncodeText(PyObject *object, |
| 551 | const char *encoding, |
| 552 | const char *errors) |
| 553 | { |
| 554 | PyObject *encoder; |
| 555 | |
| 556 | encoder = _PyCodec_TextEncoder(encoding); |
| 557 | if (encoder == NULL) |
| 558 | return NULL; |
| 559 | |
| 560 | return _PyCodec_EncodeInternal(object, encoder, encoding, errors); |
| 561 | } |
| 562 | |
| 563 | PyObject *_PyCodec_DecodeText(PyObject *object, |
| 564 | const char *encoding, |
| 565 | const char *errors) |
| 566 | { |
| 567 | PyObject *decoder; |
| 568 | |
| 569 | decoder = _PyCodec_TextDecoder(encoding); |
| 570 | if (decoder == NULL) |
| 571 | return NULL; |
| 572 | |
| 573 | return _PyCodec_DecodeInternal(object, decoder, encoding, errors); |
| 574 | } |
| 575 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 576 | /* Register the error handling callback function error under the name |
| 577 | name. This function will be called by the codec when it encounters |
| 578 | an unencodable characters/undecodable bytes and doesn't know the |
| 579 | callback name, when name is specified as the error parameter |
| 580 | in the call to the encode/decode function. |
| 581 | Return 0 on success, -1 on error */ |
| 582 | int PyCodec_RegisterError(const char *name, PyObject *error) |
| 583 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 584 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 585 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 587 | if (!PyCallable_Check(error)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | PyErr_SetString(PyExc_TypeError, "handler must be callable"); |
| 589 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 590 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 591 | return PyDict_SetItemString(interp->codec_error_registry, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | (char *)name, error); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Lookup the error handling callback function registered under the |
| 596 | name error. As a special case NULL can be passed, in which case |
| 597 | the error handling callback for strict encoding will be returned. */ |
| 598 | PyObject *PyCodec_LookupError(const char *name) |
| 599 | { |
| 600 | PyObject *handler = NULL; |
| 601 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 602 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 603 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | return NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 605 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 606 | if (name==NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | name = "strict"; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 608 | handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 609 | if (!handler) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 611 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | Py_INCREF(handler); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 613 | return handler; |
| 614 | } |
| 615 | |
| 616 | static void wrong_exception_type(PyObject *exc) |
| 617 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 618 | _Py_IDENTIFIER(__class__); |
| 619 | _Py_IDENTIFIER(__name__); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 620 | PyObject *type = _PyObject_GetAttrId(exc, &PyId___class__); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 621 | if (type != NULL) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 622 | PyObject *name = _PyObject_GetAttrId(type, &PyId___name__); |
Walter Dörwald | 573c08c | 2007-05-25 15:46:59 +0000 | [diff] [blame] | 623 | Py_DECREF(type); |
| 624 | if (name != NULL) { |
| 625 | PyErr_Format(PyExc_TypeError, |
| 626 | "don't know how to handle %S in error callback", name); |
| 627 | Py_DECREF(name); |
| 628 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | PyObject *PyCodec_StrictErrors(PyObject *exc) |
| 633 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 634 | if (PyExceptionInstance_Check(exc)) |
| 635 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 636 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | PyObject *PyCodec_IgnoreErrors(PyObject *exc) |
| 643 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 644 | Py_ssize_t end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 645 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 647 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 648 | } |
| 649 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 651 | return NULL; |
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 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 655 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 656 | } |
| 657 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | wrong_exception_type(exc); |
| 659 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 660 | } |
Victor Stinner | ee45009 | 2011-12-01 02:52:11 +0100 | [diff] [blame] | 661 | return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
| 665 | PyObject *PyCodec_ReplaceErrors(PyObject *exc) |
| 666 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 667 | Py_ssize_t start, end, i, len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 668 | |
| 669 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | PyObject *res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 671 | int kind; |
| 672 | void *data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 674 | return NULL; |
| 675 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 676 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 677 | len = end - start; |
| 678 | res = PyUnicode_New(len, '?'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | if (res == NULL) |
| 680 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 681 | kind = PyUnicode_KIND(res); |
| 682 | data = PyUnicode_DATA(res); |
| 683 | for (i = 0; i < len; ++i) |
| 684 | PyUnicode_WRITE(kind, data, i, '?'); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 685 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 686 | return Py_BuildValue("(Nn)", res, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 687 | } |
| 688 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 690 | return NULL; |
Victor Stinner | 1a15aba | 2011-10-02 19:00:15 +0200 | [diff] [blame] | 691 | return Py_BuildValue("(Cn)", |
| 692 | (int)Py_UNICODE_REPLACEMENT_CHARACTER, |
| 693 | end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 694 | } |
| 695 | else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | PyObject *res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 697 | int kind; |
| 698 | void *data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | if (PyUnicodeTranslateError_GetStart(exc, &start)) |
| 700 | return NULL; |
| 701 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 702 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | len = end - start; |
| 704 | res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | if (res == NULL) |
| 706 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 707 | kind = PyUnicode_KIND(res); |
| 708 | data = PyUnicode_DATA(res); |
| 709 | for (i=0; i < len; i++) |
| 710 | PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 711 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 712 | return Py_BuildValue("(Nn)", res, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 713 | } |
| 714 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | wrong_exception_type(exc); |
| 716 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
| 720 | PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
| 721 | { |
| 722 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | PyObject *restuple; |
| 724 | PyObject *object; |
Victor Stinner | b31f1bc | 2011-11-04 21:29:10 +0100 | [diff] [blame] | 725 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | Py_ssize_t start; |
| 727 | Py_ssize_t end; |
| 728 | PyObject *res; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 729 | unsigned char *outp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | int ressize; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 731 | Py_UCS4 ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 733 | return NULL; |
| 734 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 735 | return NULL; |
| 736 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 737 | return NULL; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 738 | for (i = start, ressize = 0; i < end; ++i) { |
| 739 | /* object is guaranteed to be "ready" */ |
| 740 | ch = PyUnicode_READ_CHAR(object, i); |
| 741 | if (ch<10) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | ressize += 2+1+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 743 | else if (ch<100) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | ressize += 2+2+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 745 | else if (ch<1000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | ressize += 2+3+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 747 | else if (ch<10000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | ressize += 2+4+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 749 | else if (ch<100000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | ressize += 2+5+1; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 751 | else if (ch<1000000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | ressize += 2+6+1; |
| 753 | else |
| 754 | ressize += 2+7+1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | } |
| 756 | /* allocate replacement */ |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 757 | res = PyUnicode_New(ressize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | if (res == NULL) { |
| 759 | Py_DECREF(object); |
| 760 | return NULL; |
| 761 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 762 | outp = PyUnicode_1BYTE_DATA(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | /* generate replacement */ |
Victor Stinner | b31f1bc | 2011-11-04 21:29:10 +0100 | [diff] [blame] | 764 | for (i = start; i < end; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | int digits; |
| 766 | int base; |
Martin v. Löwis | 8ba7930 | 2011-11-04 12:26:49 +0100 | [diff] [blame] | 767 | ch = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | *outp++ = '&'; |
| 769 | *outp++ = '#'; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 770 | if (ch<10) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | digits = 1; |
| 772 | base = 1; |
| 773 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 774 | else if (ch<100) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | digits = 2; |
| 776 | base = 10; |
| 777 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 778 | else if (ch<1000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | digits = 3; |
| 780 | base = 100; |
| 781 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 782 | else if (ch<10000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | digits = 4; |
| 784 | base = 1000; |
| 785 | } |
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 | digits = 5; |
| 788 | base = 10000; |
| 789 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 790 | else if (ch<1000000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | digits = 6; |
| 792 | base = 100000; |
| 793 | } |
| 794 | else { |
| 795 | digits = 7; |
| 796 | base = 1000000; |
| 797 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | while (digits-->0) { |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 799 | *outp++ = '0' + ch/base; |
| 800 | ch %= base; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | base /= 10; |
| 802 | } |
| 803 | *outp++ = ';'; |
| 804 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 805 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 806 | restuple = Py_BuildValue("(Nn)", res, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | Py_DECREF(object); |
| 808 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 809 | } |
| 810 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | wrong_exception_type(exc); |
| 812 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 816 | PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) |
| 817 | { |
| 818 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | PyObject *restuple; |
| 820 | PyObject *object; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 821 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | Py_ssize_t start; |
| 823 | Py_ssize_t end; |
| 824 | PyObject *res; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 825 | unsigned char *outp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | int ressize; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 827 | Py_UCS4 c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 829 | return NULL; |
| 830 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 831 | return NULL; |
| 832 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 833 | return NULL; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 834 | for (i = start, ressize = 0; i < end; ++i) { |
| 835 | /* object is guaranteed to be "ready" */ |
| 836 | c = PyUnicode_READ_CHAR(object, i); |
| 837 | if (c >= 0x10000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | ressize += 1+1+8; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 839 | } |
| 840 | else if (c >= 0x100) { |
| 841 | ressize += 1+1+4; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | } |
| 843 | else |
| 844 | ressize += 1+1+2; |
| 845 | } |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 846 | res = PyUnicode_New(ressize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | if (res==NULL) |
| 848 | return NULL; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 849 | for (i = start, outp = PyUnicode_1BYTE_DATA(res); |
| 850 | i < end; ++i) { |
| 851 | c = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | *outp++ = '\\'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | if (c >= 0x00010000) { |
| 854 | *outp++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 855 | *outp++ = Py_hexdigits[(c>>28)&0xf]; |
| 856 | *outp++ = Py_hexdigits[(c>>24)&0xf]; |
| 857 | *outp++ = Py_hexdigits[(c>>20)&0xf]; |
| 858 | *outp++ = Py_hexdigits[(c>>16)&0xf]; |
| 859 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 860 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | } |
Antoine Pitrou | e4a1892 | 2010-09-09 20:30:23 +0000 | [diff] [blame] | 862 | else if (c >= 0x100) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | *outp++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 864 | *outp++ = Py_hexdigits[(c>>12)&0xf]; |
| 865 | *outp++ = Py_hexdigits[(c>>8)&0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | } |
| 867 | else |
| 868 | *outp++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 869 | *outp++ = Py_hexdigits[(c>>4)&0xf]; |
| 870 | *outp++ = Py_hexdigits[c&0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 872 | |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 873 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 874 | restuple = Py_BuildValue("(Nn)", res, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | Py_DECREF(object); |
| 876 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 877 | } |
| 878 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | wrong_exception_type(exc); |
| 880 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Martin v. Löwis | aef3fb0 | 2009-05-02 19:27:30 +0000 | [diff] [blame] | 884 | /* This handler is declared static until someone demonstrates |
| 885 | a need to call it directly. */ |
| 886 | static PyObject * |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 887 | PyCodec_SurrogatePassErrors(PyObject *exc) |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 888 | { |
| 889 | PyObject *restuple; |
| 890 | PyObject *object; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 891 | Py_ssize_t i; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 892 | Py_ssize_t start; |
| 893 | Py_ssize_t end; |
| 894 | PyObject *res; |
| 895 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | char *outp; |
| 897 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 898 | return NULL; |
| 899 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 900 | return NULL; |
| 901 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 902 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | res = PyBytes_FromStringAndSize(NULL, 3*(end-start)); |
| 904 | if (!res) { |
| 905 | Py_DECREF(object); |
| 906 | return NULL; |
| 907 | } |
| 908 | outp = PyBytes_AsString(res); |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 909 | for (i = start; i < end; i++) { |
| 910 | /* object is guaranteed to be "ready" */ |
| 911 | Py_UCS4 ch = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | if (ch < 0xd800 || ch > 0xdfff) { |
| 913 | /* Not a surrogate, fail with original exception */ |
| 914 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 915 | Py_DECREF(res); |
| 916 | Py_DECREF(object); |
| 917 | return NULL; |
| 918 | } |
| 919 | *outp++ = (char)(0xe0 | (ch >> 12)); |
| 920 | *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 921 | *outp++ = (char)(0x80 | (ch & 0x3f)); |
| 922 | } |
| 923 | restuple = Py_BuildValue("(On)", res, end); |
| 924 | Py_DECREF(res); |
| 925 | Py_DECREF(object); |
| 926 | return restuple; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 927 | } |
| 928 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | unsigned char *p; |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 930 | Py_UCS4 ch = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | if (PyUnicodeDecodeError_GetStart(exc, &start)) |
| 932 | return NULL; |
| 933 | if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
| 934 | return NULL; |
| 935 | if (!(p = (unsigned char*)PyBytes_AsString(object))) { |
| 936 | Py_DECREF(object); |
| 937 | return NULL; |
| 938 | } |
| 939 | /* Try decoding a single surrogate character. If |
| 940 | there are more, let the codec call us again. */ |
| 941 | p += start; |
Ezio Melotti | 540da76 | 2012-11-03 23:03:39 +0200 | [diff] [blame] | 942 | if (PyBytes_GET_SIZE(object) - start >= 3 && |
| 943 | (p[0] & 0xf0) == 0xe0 && |
| 944 | (p[1] & 0xc0) == 0x80 && |
| 945 | (p[2] & 0xc0) == 0x80) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | /* it's a three-byte code */ |
| 947 | ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f); |
| 948 | if (ch < 0xd800 || ch > 0xdfff) |
| 949 | /* it's not a surrogate - fail */ |
| 950 | ch = 0; |
| 951 | } |
| 952 | Py_DECREF(object); |
| 953 | if (ch == 0) { |
| 954 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 955 | return NULL; |
| 956 | } |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 957 | res = PyUnicode_FromOrdinal(ch); |
| 958 | if (res == NULL) |
| 959 | return NULL; |
| 960 | return Py_BuildValue("(Nn)", res, start+3); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 961 | } |
| 962 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | wrong_exception_type(exc); |
| 964 | return NULL; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 968 | static PyObject * |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 969 | PyCodec_SurrogateEscapeErrors(PyObject *exc) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 970 | { |
| 971 | PyObject *restuple; |
| 972 | PyObject *object; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 973 | Py_ssize_t i; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 974 | Py_ssize_t start; |
| 975 | Py_ssize_t end; |
| 976 | PyObject *res; |
| 977 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | char *outp; |
| 979 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 980 | return NULL; |
| 981 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 982 | return NULL; |
| 983 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 984 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | res = PyBytes_FromStringAndSize(NULL, end-start); |
| 986 | if (!res) { |
| 987 | Py_DECREF(object); |
| 988 | return NULL; |
| 989 | } |
| 990 | outp = PyBytes_AsString(res); |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 991 | for (i = start; i < end; i++) { |
| 992 | /* object is guaranteed to be "ready" */ |
| 993 | Py_UCS4 ch = PyUnicode_READ_CHAR(object, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | if (ch < 0xdc80 || ch > 0xdcff) { |
| 995 | /* Not a UTF-8b surrogate, fail with original exception */ |
| 996 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 997 | Py_DECREF(res); |
| 998 | Py_DECREF(object); |
| 999 | return NULL; |
| 1000 | } |
| 1001 | *outp++ = ch - 0xdc00; |
| 1002 | } |
| 1003 | restuple = Py_BuildValue("(On)", res, end); |
| 1004 | Py_DECREF(res); |
| 1005 | Py_DECREF(object); |
| 1006 | return restuple; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1007 | } |
| 1008 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1009 | PyObject *str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | unsigned char *p; |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1011 | Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | int consumed = 0; |
| 1013 | if (PyUnicodeDecodeError_GetStart(exc, &start)) |
| 1014 | return NULL; |
| 1015 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 1016 | return NULL; |
| 1017 | if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
| 1018 | return NULL; |
| 1019 | if (!(p = (unsigned char*)PyBytes_AsString(object))) { |
| 1020 | Py_DECREF(object); |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | while (consumed < 4 && consumed < end-start) { |
| 1024 | /* Refuse to escape ASCII bytes. */ |
| 1025 | if (p[start+consumed] < 128) |
| 1026 | break; |
| 1027 | ch[consumed] = 0xdc00 + p[start+consumed]; |
| 1028 | consumed++; |
| 1029 | } |
| 1030 | Py_DECREF(object); |
| 1031 | if (!consumed) { |
| 1032 | /* codec complained about ASCII byte. */ |
| 1033 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
| 1034 | return NULL; |
| 1035 | } |
Victor Stinner | c06bb7a | 2011-11-04 21:36:35 +0100 | [diff] [blame] | 1036 | str = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, ch, consumed); |
| 1037 | if (str == NULL) |
| 1038 | return NULL; |
| 1039 | return Py_BuildValue("(Nn)", str, start+consumed); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1040 | } |
| 1041 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | wrong_exception_type(exc); |
| 1043 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1048 | static PyObject *strict_errors(PyObject *self, PyObject *exc) |
| 1049 | { |
| 1050 | return PyCodec_StrictErrors(exc); |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | static PyObject *ignore_errors(PyObject *self, PyObject *exc) |
| 1055 | { |
| 1056 | return PyCodec_IgnoreErrors(exc); |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | static PyObject *replace_errors(PyObject *self, PyObject *exc) |
| 1061 | { |
| 1062 | return PyCodec_ReplaceErrors(exc); |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) |
| 1067 | { |
| 1068 | return PyCodec_XMLCharRefReplaceErrors(exc); |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) |
| 1073 | { |
| 1074 | return PyCodec_BackslashReplaceErrors(exc); |
| 1075 | } |
| 1076 | |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1077 | static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc) |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1078 | { |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1079 | return PyCodec_SurrogatePassErrors(exc); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1082 | static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1083 | { |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1084 | return PyCodec_SurrogateEscapeErrors(exc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1087 | static int _PyCodecRegistry_Init(void) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1088 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1089 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | char *name; |
| 1091 | PyMethodDef def; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1092 | } methods[] = |
| 1093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | { |
| 1095 | "strict", |
| 1096 | { |
| 1097 | "strict_errors", |
| 1098 | strict_errors, |
| 1099 | METH_O, |
| 1100 | PyDoc_STR("Implements the 'strict' error handling, which " |
| 1101 | "raises a UnicodeError on coding errors.") |
| 1102 | } |
| 1103 | }, |
| 1104 | { |
| 1105 | "ignore", |
| 1106 | { |
| 1107 | "ignore_errors", |
| 1108 | ignore_errors, |
| 1109 | METH_O, |
| 1110 | PyDoc_STR("Implements the 'ignore' error handling, which " |
| 1111 | "ignores malformed data and continues.") |
| 1112 | } |
| 1113 | }, |
| 1114 | { |
| 1115 | "replace", |
| 1116 | { |
| 1117 | "replace_errors", |
| 1118 | replace_errors, |
| 1119 | METH_O, |
| 1120 | PyDoc_STR("Implements the 'replace' error handling, which " |
| 1121 | "replaces malformed data with a replacement marker.") |
| 1122 | } |
| 1123 | }, |
| 1124 | { |
| 1125 | "xmlcharrefreplace", |
| 1126 | { |
| 1127 | "xmlcharrefreplace_errors", |
| 1128 | xmlcharrefreplace_errors, |
| 1129 | METH_O, |
| 1130 | PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " |
| 1131 | "which replaces an unencodable character with the " |
| 1132 | "appropriate XML character reference.") |
| 1133 | } |
| 1134 | }, |
| 1135 | { |
| 1136 | "backslashreplace", |
| 1137 | { |
| 1138 | "backslashreplace_errors", |
| 1139 | backslashreplace_errors, |
| 1140 | METH_O, |
| 1141 | PyDoc_STR("Implements the 'backslashreplace' error handling, " |
| 1142 | "which replaces an unencodable character with a " |
| 1143 | "backslashed escape sequence.") |
| 1144 | } |
| 1145 | }, |
| 1146 | { |
| 1147 | "surrogatepass", |
| 1148 | { |
| 1149 | "surrogatepass", |
| 1150 | surrogatepass_errors, |
| 1151 | METH_O |
| 1152 | } |
| 1153 | }, |
| 1154 | { |
| 1155 | "surrogateescape", |
| 1156 | { |
| 1157 | "surrogateescape", |
| 1158 | surrogateescape_errors, |
| 1159 | METH_O |
| 1160 | } |
| 1161 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1162 | }; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1163 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 1164 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1165 | PyObject *mod; |
Neal Norwitz | 739a8f8 | 2004-07-08 01:55:58 +0000 | [diff] [blame] | 1166 | unsigned i; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1167 | |
| 1168 | if (interp->codec_search_path != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | return 0; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1170 | |
| 1171 | interp->codec_search_path = PyList_New(0); |
| 1172 | interp->codec_search_cache = PyDict_New(); |
| 1173 | interp->codec_error_registry = PyDict_New(); |
| 1174 | |
| 1175 | if (interp->codec_error_registry) { |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1176 | for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | PyObject *func = PyCFunction_New(&methods[i].def, NULL); |
| 1178 | int res; |
| 1179 | if (!func) |
| 1180 | Py_FatalError("can't initialize codec error registry"); |
| 1181 | res = PyCodec_RegisterError(methods[i].name, func); |
| 1182 | Py_DECREF(func); |
| 1183 | if (res) |
| 1184 | Py_FatalError("can't initialize codec error registry"); |
| 1185 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1186 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1187 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1188 | if (interp->codec_search_path == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | interp->codec_search_cache == NULL || |
| 1190 | interp->codec_error_registry == NULL) |
| 1191 | Py_FatalError("can't initialize codec registry"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1192 | |
Christian Heimes | 819b8bf | 2008-01-03 23:05:47 +0000 | [diff] [blame] | 1193 | mod = PyImport_ImportModuleNoBlock("encodings"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1194 | if (mod == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | return -1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1196 | } |
| 1197 | Py_DECREF(mod); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1198 | interp->codecs_initialized = 1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 1199 | return 0; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 1200 | } |