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