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 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 14 | /* --- Codec Registry ----------------------------------------------------- */ |
| 15 | |
| 16 | /* Import the standard encodings package which will register the first |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 17 | codec search function. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 18 | |
| 19 | This is done in a lazy way so that the Unicode implementation does |
| 20 | not downgrade startup time of scripts not needing it. |
| 21 | |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 22 | ImportErrors are silently ignored by this function. Only one try is |
| 23 | made. |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 24 | |
| 25 | */ |
| 26 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 27 | static int _PyCodecRegistry_Init(void); /* Forward */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 29 | int PyCodec_Register(PyObject *search_function) |
| 30 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 31 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 32 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 33 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 34 | if (search_function == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | PyErr_BadArgument(); |
| 36 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 37 | } |
| 38 | if (!PyCallable_Check(search_function)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | PyErr_SetString(PyExc_TypeError, "argument must be callable"); |
| 40 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 41 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 42 | return PyList_Append(interp->codec_search_path, search_function); |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 43 | |
| 44 | onError: |
| 45 | return -1; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 48 | /* Convert a string to a normalized Python string: all characters are |
| 49 | converted to lower case, spaces are replaced with underscores. */ |
| 50 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 51 | static |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 52 | PyObject *normalizestring(const char *string) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | 3383113 | 2000-06-29 14:50:15 +0000 | [diff] [blame] | 54 | register size_t i; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 55 | size_t len = strlen(string); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 56 | char *p; |
| 57 | PyObject *v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 59 | if (len > PY_SSIZE_T_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | PyErr_SetString(PyExc_OverflowError, "string is too large"); |
| 61 | return NULL; |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 62 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 63 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 64 | v = PyString_FromStringAndSize(NULL, len); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 65 | if (v == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 66 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 67 | p = PyString_AS_STRING(v); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 68 | for (i = 0; i < len; i++) { |
| 69 | register char ch = string[i]; |
| 70 | if (ch == ' ') |
| 71 | ch = '-'; |
| 72 | else |
Neal Norwitz | 237bf40 | 2006-04-28 05:28:30 +0000 | [diff] [blame] | 73 | ch = tolower(Py_CHARMASK(ch)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 74 | p[i] = ch; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 75 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 76 | return v; |
| 77 | } |
| 78 | |
| 79 | /* Lookup the given encoding and return a tuple providing the codec |
| 80 | facilities. |
| 81 | |
| 82 | The encoding string is looked up converted to all lower-case |
| 83 | characters. This makes encodings looked up through this mechanism |
| 84 | effectively case-insensitive. |
| 85 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 86 | 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] | 87 | |
| 88 | As side effect, this tries to load the encodings package, if not |
| 89 | yet done. This is part of the lazy load strategy for the encodings |
| 90 | package. |
| 91 | |
| 92 | */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 93 | |
| 94 | PyObject *_PyCodec_Lookup(const char *encoding) |
| 95 | { |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 96 | PyInterpreterState *interp; |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 97 | PyObject *result, *args = NULL, *v; |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 98 | Py_ssize_t i, len; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 99 | |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 100 | if (encoding == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 101 | PyErr_BadArgument(); |
| 102 | goto onError; |
Fred Drake | 766de83 | 2000-05-09 19:55:59 +0000 | [diff] [blame] | 103 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 104 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 105 | interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 106 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 107 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 109 | /* Convert the encoding to a normalized Python string: all |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 110 | characters are converted to lower case, spaces and hyphens are |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 111 | replaced with underscores. */ |
| 112 | v = normalizestring(encoding); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 113 | if (v == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 114 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 115 | PyString_InternInPlace(&v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 116 | |
| 117 | /* First, try to lookup the name in the registry dictionary */ |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 118 | result = PyDict_GetItem(interp->codec_search_cache, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 119 | if (result != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 120 | Py_INCREF(result); |
| 121 | Py_DECREF(v); |
| 122 | return result; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 123 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 125 | /* Next, scan the search functions in order of registration */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 126 | args = PyTuple_New(1); |
| 127 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 128 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 129 | PyTuple_SET_ITEM(args,0,v); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 130 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 131 | len = PyList_Size(interp->codec_search_path); |
Guido van Rossum | 5ba3c84 | 2000-03-24 20:52:23 +0000 | [diff] [blame] | 132 | if (len < 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 133 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 134 | if (len == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 135 | PyErr_SetString(PyExc_LookupError, |
| 136 | "no codec search functions registered: " |
| 137 | "can't find encoding"); |
| 138 | goto onError; |
Guido van Rossum | b95de4f | 2000-03-31 17:25:23 +0000 | [diff] [blame] | 139 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 140 | |
| 141 | for (i = 0; i < len; i++) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 142 | PyObject *func; |
| 143 | |
| 144 | func = PyList_GetItem(interp->codec_search_path, i); |
| 145 | if (func == NULL) |
| 146 | goto onError; |
| 147 | result = PyEval_CallObject(func, args); |
| 148 | if (result == NULL) |
| 149 | goto onError; |
| 150 | if (result == Py_None) { |
| 151 | Py_DECREF(result); |
| 152 | continue; |
| 153 | } |
| 154 | if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { |
| 155 | PyErr_SetString(PyExc_TypeError, |
| 156 | "codec search functions must return 4-tuples"); |
| 157 | Py_DECREF(result); |
| 158 | goto onError; |
| 159 | } |
| 160 | break; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 161 | } |
| 162 | if (i == len) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 163 | /* XXX Perhaps we should cache misses too ? */ |
| 164 | PyErr_Format(PyExc_LookupError, |
Martin v. Löwis | eb42b02 | 2002-09-26 16:01:24 +0000 | [diff] [blame] | 165 | "unknown encoding: %s", encoding); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 166 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Cache and return the result */ |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 170 | PyDict_SetItem(interp->codec_search_cache, v, result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 171 | Py_DECREF(args); |
| 172 | return result; |
| 173 | |
| 174 | onError: |
| 175 | Py_XDECREF(args); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | PyObject *args_tuple(PyObject *object, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 181 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 182 | { |
| 183 | PyObject *args; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 185 | args = PyTuple_New(1 + (errors != NULL)); |
| 186 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 187 | return NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 188 | Py_INCREF(object); |
| 189 | PyTuple_SET_ITEM(args,0,object); |
| 190 | if (errors) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 191 | PyObject *v; |
| 192 | |
| 193 | v = PyString_FromString(errors); |
| 194 | if (v == NULL) { |
| 195 | Py_DECREF(args); |
| 196 | return NULL; |
| 197 | } |
| 198 | PyTuple_SET_ITEM(args, 1, v); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 199 | } |
| 200 | return args; |
| 201 | } |
| 202 | |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 203 | /* Helper function to get a codec item */ |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 204 | |
| 205 | static |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 206 | PyObject *codec_getitem(const char *encoding, int index) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 207 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 208 | PyObject *codecs; |
| 209 | PyObject *v; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 210 | |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 211 | codecs = _PyCodec_Lookup(encoding); |
| 212 | if (codecs == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 213 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 214 | v = PyTuple_GET_ITEM(codecs, index); |
| 215 | Py_DECREF(codecs); |
| 216 | Py_INCREF(v); |
| 217 | return v; |
| 218 | } |
| 219 | |
| 220 | /* Helper function to create an incremental codec. */ |
| 221 | |
| 222 | static |
| 223 | PyObject *codec_getincrementalcodec(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 224 | const char *errors, |
| 225 | const char *attrname) |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 226 | { |
| 227 | PyObject *codecs, *ret, *inccodec; |
| 228 | |
| 229 | codecs = _PyCodec_Lookup(encoding); |
| 230 | if (codecs == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 232 | inccodec = PyObject_GetAttrString(codecs, attrname); |
Walter Dörwald | ba8e180 | 2006-03-18 14:05:43 +0000 | [diff] [blame] | 233 | Py_DECREF(codecs); |
| 234 | if (inccodec == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 235 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 236 | if (errors) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 237 | ret = PyObject_CallFunction(inccodec, "s", errors); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 238 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 239 | ret = PyObject_CallFunction(inccodec, NULL); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 240 | Py_DECREF(inccodec); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | /* Helper function to create a stream codec. */ |
| 245 | |
| 246 | static |
| 247 | PyObject *codec_getstreamcodec(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 248 | PyObject *stream, |
| 249 | const char *errors, |
| 250 | const int index) |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 251 | { |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 252 | PyObject *codecs, *streamcodec, *codeccls; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 253 | |
| 254 | codecs = _PyCodec_Lookup(encoding); |
| 255 | if (codecs == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 256 | return NULL; |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 257 | |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 258 | codeccls = PyTuple_GET_ITEM(codecs, index); |
| 259 | if (errors != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 260 | streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); |
Hye-Shik Chang | e6a1cb9 | 2006-06-23 21:16:18 +0000 | [diff] [blame] | 261 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | streamcodec = PyObject_CallFunction(codeccls, "O", stream); |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 263 | Py_DECREF(codecs); |
| 264 | return streamcodec; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 267 | /* Convenience APIs to query the Codec registry. |
| 268 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 269 | All APIs return a codec object with incremented refcount. |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 271 | */ |
| 272 | |
| 273 | PyObject *PyCodec_Encoder(const char *encoding) |
| 274 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 275 | return codec_getitem(encoding, 0); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | PyObject *PyCodec_Decoder(const char *encoding) |
| 279 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 280 | return codec_getitem(encoding, 1); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 283 | PyObject *PyCodec_IncrementalEncoder(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | const char *errors) |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 285 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 286 | return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | PyObject *PyCodec_IncrementalDecoder(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | const char *errors) |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 291 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 292 | return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 295 | PyObject *PyCodec_StreamReader(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 296 | PyObject *stream, |
| 297 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 298 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 299 | return codec_getstreamcodec(encoding, stream, errors, 2); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | PyObject *PyCodec_StreamWriter(const char *encoding, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 303 | PyObject *stream, |
| 304 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 305 | { |
Walter Dörwald | d53850a | 2006-03-16 21:46:40 +0000 | [diff] [blame] | 306 | return codec_getstreamcodec(encoding, stream, errors, 3); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* Encode an object (e.g. an Unicode object) using the given encoding |
| 310 | and return the resulting encoded object (usually a Python string). |
| 311 | |
| 312 | errors is passed to the encoder factory as argument if non-NULL. */ |
| 313 | |
| 314 | PyObject *PyCodec_Encode(PyObject *object, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | const char *encoding, |
| 316 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 317 | { |
| 318 | PyObject *encoder = NULL; |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 319 | PyObject *args = NULL, *result = NULL; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 320 | PyObject *v; |
| 321 | |
| 322 | encoder = PyCodec_Encoder(encoding); |
| 323 | if (encoder == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 324 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 325 | |
| 326 | args = args_tuple(object, errors); |
| 327 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 328 | goto onError; |
| 329 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 330 | result = PyEval_CallObject(encoder,args); |
| 331 | if (result == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 332 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | if (!PyTuple_Check(result) || |
| 335 | PyTuple_GET_SIZE(result) != 2) { |
| 336 | PyErr_SetString(PyExc_TypeError, |
| 337 | "encoder must return a tuple (object,integer)"); |
| 338 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 339 | } |
| 340 | v = PyTuple_GET_ITEM(result,0); |
| 341 | Py_INCREF(v); |
| 342 | /* We don't check or use the second (integer) entry. */ |
| 343 | |
| 344 | Py_DECREF(args); |
| 345 | Py_DECREF(encoder); |
| 346 | Py_DECREF(result); |
| 347 | return v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 349 | onError: |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 350 | Py_XDECREF(result); |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 351 | Py_XDECREF(args); |
| 352 | Py_XDECREF(encoder); |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | /* Decode an object (usually a Python string) using the given encoding |
| 357 | and return an equivalent object (e.g. an Unicode object). |
| 358 | |
| 359 | errors is passed to the decoder factory as argument if non-NULL. */ |
| 360 | |
| 361 | PyObject *PyCodec_Decode(PyObject *object, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 362 | const char *encoding, |
| 363 | const char *errors) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 364 | { |
| 365 | PyObject *decoder = NULL; |
| 366 | PyObject *args = NULL, *result = NULL; |
| 367 | PyObject *v; |
| 368 | |
| 369 | decoder = PyCodec_Decoder(encoding); |
| 370 | if (decoder == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 371 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 372 | |
| 373 | args = args_tuple(object, errors); |
| 374 | if (args == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 375 | goto onError; |
| 376 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 377 | result = PyEval_CallObject(decoder,args); |
| 378 | if (result == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 379 | goto onError; |
| 380 | if (!PyTuple_Check(result) || |
| 381 | PyTuple_GET_SIZE(result) != 2) { |
| 382 | PyErr_SetString(PyExc_TypeError, |
| 383 | "decoder must return a tuple (object,integer)"); |
| 384 | goto onError; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 385 | } |
| 386 | v = PyTuple_GET_ITEM(result,0); |
| 387 | Py_INCREF(v); |
| 388 | /* We don't check or use the second (integer) entry. */ |
| 389 | |
| 390 | Py_DECREF(args); |
| 391 | Py_DECREF(decoder); |
| 392 | Py_DECREF(result); |
| 393 | return v; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 395 | onError: |
| 396 | Py_XDECREF(args); |
| 397 | Py_XDECREF(decoder); |
| 398 | Py_XDECREF(result); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 402 | /* Register the error handling callback function error under the name |
| 403 | name. This function will be called by the codec when it encounters |
| 404 | an unencodable characters/undecodable bytes and doesn't know the |
| 405 | callback name, when name is specified as the error parameter |
| 406 | in the call to the encode/decode function. |
| 407 | Return 0 on success, -1 on error */ |
| 408 | int PyCodec_RegisterError(const char *name, PyObject *error) |
| 409 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 410 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 411 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 412 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 413 | if (!PyCallable_Check(error)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 414 | PyErr_SetString(PyExc_TypeError, "handler must be callable"); |
| 415 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 416 | } |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 417 | return PyDict_SetItemString(interp->codec_error_registry, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 418 | (char *)name, error); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Lookup the error handling callback function registered under the |
| 422 | name error. As a special case NULL can be passed, in which case |
| 423 | the error handling callback for strict encoding will be returned. */ |
| 424 | PyObject *PyCodec_LookupError(const char *name) |
| 425 | { |
| 426 | PyObject *handler = NULL; |
| 427 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 428 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 429 | if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 430 | return NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 431 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 432 | if (name==NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 433 | name = "strict"; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 434 | handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 435 | if (!handler) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 436 | PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 437 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 438 | Py_INCREF(handler); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 439 | return handler; |
| 440 | } |
| 441 | |
| 442 | static void wrong_exception_type(PyObject *exc) |
| 443 | { |
| 444 | PyObject *type = PyObject_GetAttrString(exc, "__class__"); |
| 445 | if (type != NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 446 | PyObject *name = PyObject_GetAttrString(type, "__name__"); |
| 447 | Py_DECREF(type); |
| 448 | if (name != NULL) { |
| 449 | PyObject *string = PyObject_Str(name); |
| 450 | Py_DECREF(name); |
| 451 | if (string != NULL) { |
| 452 | PyErr_Format(PyExc_TypeError, |
| 453 | "don't know how to handle %.400s in error callback", |
| 454 | PyString_AS_STRING(string)); |
| 455 | Py_DECREF(string); |
| 456 | } |
| 457 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | PyObject *PyCodec_StrictErrors(PyObject *exc) |
| 462 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 463 | if (PyExceptionInstance_Check(exc)) |
| 464 | PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 465 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 466 | PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 471 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 472 | PyObject *PyCodec_IgnoreErrors(PyObject *exc) |
| 473 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 474 | Py_ssize_t end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 475 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 476 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 477 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 478 | } |
| 479 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 480 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 481 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 482 | } |
| 483 | else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 484 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 485 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 486 | } |
| 487 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 488 | wrong_exception_type(exc); |
| 489 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 490 | } |
| 491 | /* ouch: passing NULL, 0, pos gives None instead of u'' */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 492 | return Py_BuildValue("(u#n)", &end, 0, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
| 496 | PyObject *PyCodec_ReplaceErrors(PyObject *exc) |
| 497 | { |
| 498 | PyObject *restuple; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 499 | Py_ssize_t start; |
| 500 | Py_ssize_t end; |
| 501 | Py_ssize_t i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 502 | |
| 503 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 504 | PyObject *res; |
| 505 | Py_UNICODE *p; |
| 506 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 507 | return NULL; |
| 508 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 509 | return NULL; |
| 510 | res = PyUnicode_FromUnicode(NULL, end-start); |
| 511 | if (res == NULL) |
| 512 | return NULL; |
| 513 | for (p = PyUnicode_AS_UNICODE(res), i = start; |
| 514 | i<end; ++p, ++i) |
| 515 | *p = '?'; |
| 516 | restuple = Py_BuildValue("(On)", res, end); |
| 517 | Py_DECREF(res); |
| 518 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 519 | } |
| 520 | else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; |
| 522 | if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
| 523 | return NULL; |
| 524 | return Py_BuildValue("(u#n)", &res, 1, end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 525 | } |
| 526 | else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 527 | PyObject *res; |
| 528 | Py_UNICODE *p; |
| 529 | if (PyUnicodeTranslateError_GetStart(exc, &start)) |
| 530 | return NULL; |
| 531 | if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
| 532 | return NULL; |
| 533 | res = PyUnicode_FromUnicode(NULL, end-start); |
| 534 | if (res == NULL) |
| 535 | return NULL; |
| 536 | for (p = PyUnicode_AS_UNICODE(res), i = start; |
| 537 | i<end; ++p, ++i) |
| 538 | *p = Py_UNICODE_REPLACEMENT_CHARACTER; |
| 539 | restuple = Py_BuildValue("(On)", res, end); |
| 540 | Py_DECREF(res); |
| 541 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 542 | } |
| 543 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 544 | wrong_exception_type(exc); |
| 545 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
| 550 | { |
| 551 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | PyObject *restuple; |
| 553 | PyObject *object; |
| 554 | Py_ssize_t start; |
| 555 | Py_ssize_t end; |
| 556 | PyObject *res; |
| 557 | Py_UNICODE *p; |
| 558 | Py_UNICODE *startp; |
| 559 | Py_UNICODE *outp; |
| 560 | int ressize; |
| 561 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 562 | return NULL; |
| 563 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 564 | return NULL; |
| 565 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 566 | return NULL; |
| 567 | startp = PyUnicode_AS_UNICODE(object); |
| 568 | for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
| 569 | if (*p<10) |
| 570 | ressize += 2+1+1; |
| 571 | else if (*p<100) |
| 572 | ressize += 2+2+1; |
| 573 | else if (*p<1000) |
| 574 | ressize += 2+3+1; |
| 575 | else if (*p<10000) |
| 576 | ressize += 2+4+1; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 577 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | else |
| 579 | ressize += 2+5+1; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 580 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 581 | else if (*p<100000) |
| 582 | ressize += 2+5+1; |
| 583 | else if (*p<1000000) |
| 584 | ressize += 2+6+1; |
| 585 | else |
| 586 | ressize += 2+7+1; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 587 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | } |
| 589 | /* allocate replacement */ |
| 590 | res = PyUnicode_FromUnicode(NULL, ressize); |
| 591 | if (res == NULL) { |
| 592 | Py_DECREF(object); |
| 593 | return NULL; |
| 594 | } |
| 595 | /* generate replacement */ |
| 596 | for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); |
| 597 | p < startp+end; ++p) { |
| 598 | Py_UNICODE c = *p; |
| 599 | int digits; |
| 600 | int base; |
| 601 | *outp++ = '&'; |
| 602 | *outp++ = '#'; |
| 603 | if (*p<10) { |
| 604 | digits = 1; |
| 605 | base = 1; |
| 606 | } |
| 607 | else if (*p<100) { |
| 608 | digits = 2; |
| 609 | base = 10; |
| 610 | } |
| 611 | else if (*p<1000) { |
| 612 | digits = 3; |
| 613 | base = 100; |
| 614 | } |
| 615 | else if (*p<10000) { |
| 616 | digits = 4; |
| 617 | base = 1000; |
| 618 | } |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 619 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 620 | else { |
| 621 | digits = 5; |
| 622 | base = 10000; |
| 623 | } |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 624 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 625 | else if (*p<100000) { |
| 626 | digits = 5; |
| 627 | base = 10000; |
| 628 | } |
| 629 | else if (*p<1000000) { |
| 630 | digits = 6; |
| 631 | base = 100000; |
| 632 | } |
| 633 | else { |
| 634 | digits = 7; |
| 635 | base = 1000000; |
| 636 | } |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 637 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 638 | while (digits-->0) { |
| 639 | *outp++ = '0' + c/base; |
| 640 | c %= base; |
| 641 | base /= 10; |
| 642 | } |
| 643 | *outp++ = ';'; |
| 644 | } |
| 645 | restuple = Py_BuildValue("(On)", res, end); |
| 646 | Py_DECREF(res); |
| 647 | Py_DECREF(object); |
| 648 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 649 | } |
| 650 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 651 | wrong_exception_type(exc); |
| 652 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | static Py_UNICODE hexdigits[] = { |
| 657 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 658 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 659 | }; |
| 660 | |
| 661 | PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) |
| 662 | { |
| 663 | if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 664 | PyObject *restuple; |
| 665 | PyObject *object; |
| 666 | Py_ssize_t start; |
| 667 | Py_ssize_t end; |
| 668 | PyObject *res; |
| 669 | Py_UNICODE *p; |
| 670 | Py_UNICODE *startp; |
| 671 | Py_UNICODE *outp; |
| 672 | int ressize; |
| 673 | if (PyUnicodeEncodeError_GetStart(exc, &start)) |
| 674 | return NULL; |
| 675 | if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
| 676 | return NULL; |
| 677 | if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
| 678 | return NULL; |
| 679 | startp = PyUnicode_AS_UNICODE(object); |
| 680 | for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 681 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | if (*p >= 0x00010000) |
| 683 | ressize += 1+1+8; |
| 684 | else |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 685 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 686 | if (*p >= 0x100) { |
| 687 | ressize += 1+1+4; |
| 688 | } |
| 689 | else |
| 690 | ressize += 1+1+2; |
| 691 | } |
| 692 | res = PyUnicode_FromUnicode(NULL, ressize); |
| 693 | if (res==NULL) |
| 694 | return NULL; |
| 695 | for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); |
| 696 | p < startp+end; ++p) { |
| 697 | Py_UNICODE c = *p; |
| 698 | *outp++ = '\\'; |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 699 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | if (c >= 0x00010000) { |
| 701 | *outp++ = 'U'; |
| 702 | *outp++ = hexdigits[(c>>28)&0xf]; |
| 703 | *outp++ = hexdigits[(c>>24)&0xf]; |
| 704 | *outp++ = hexdigits[(c>>20)&0xf]; |
| 705 | *outp++ = hexdigits[(c>>16)&0xf]; |
| 706 | *outp++ = hexdigits[(c>>12)&0xf]; |
| 707 | *outp++ = hexdigits[(c>>8)&0xf]; |
| 708 | } |
| 709 | else |
Hye-Shik Chang | 7db07e6 | 2003-12-29 01:36:01 +0000 | [diff] [blame] | 710 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 711 | if (c >= 0x100) { |
| 712 | *outp++ = 'u'; |
| 713 | *outp++ = hexdigits[(c>>12)&0xf]; |
| 714 | *outp++ = hexdigits[(c>>8)&0xf]; |
| 715 | } |
| 716 | else |
| 717 | *outp++ = 'x'; |
| 718 | *outp++ = hexdigits[(c>>4)&0xf]; |
| 719 | *outp++ = hexdigits[c&0xf]; |
| 720 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 722 | restuple = Py_BuildValue("(On)", res, end); |
| 723 | Py_DECREF(res); |
| 724 | Py_DECREF(object); |
| 725 | return restuple; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 726 | } |
| 727 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 728 | wrong_exception_type(exc); |
| 729 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 732 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 733 | |
| 734 | static PyObject *strict_errors(PyObject *self, PyObject *exc) |
| 735 | { |
| 736 | return PyCodec_StrictErrors(exc); |
| 737 | } |
| 738 | |
| 739 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 740 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 741 | static PyObject *ignore_errors(PyObject *self, PyObject *exc) |
| 742 | { |
| 743 | return PyCodec_IgnoreErrors(exc); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | static PyObject *replace_errors(PyObject *self, PyObject *exc) |
| 748 | { |
| 749 | return PyCodec_ReplaceErrors(exc); |
| 750 | } |
| 751 | |
| 752 | |
| 753 | static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) |
| 754 | { |
| 755 | return PyCodec_XMLCharRefReplaceErrors(exc); |
| 756 | } |
| 757 | |
| 758 | |
| 759 | static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) |
| 760 | { |
| 761 | return PyCodec_BackslashReplaceErrors(exc); |
| 762 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 763 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 764 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 765 | static int _PyCodecRegistry_Init(void) |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 766 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 767 | static struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 768 | char *name; |
| 769 | PyMethodDef def; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 770 | } methods[] = |
| 771 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 772 | { |
| 773 | "strict", |
| 774 | { |
| 775 | "strict_errors", |
| 776 | strict_errors, |
| 777 | METH_O, |
| 778 | PyDoc_STR("Implements the 'strict' error handling, which " |
| 779 | "raises a UnicodeError on coding errors.") |
| 780 | } |
| 781 | }, |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 782 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 783 | { |
| 784 | "ignore", |
| 785 | { |
| 786 | "ignore_errors", |
| 787 | ignore_errors, |
| 788 | METH_O, |
| 789 | PyDoc_STR("Implements the 'ignore' error handling, which " |
| 790 | "ignores malformed data and continues.") |
| 791 | } |
| 792 | }, |
| 793 | { |
| 794 | "replace", |
| 795 | { |
| 796 | "replace_errors", |
| 797 | replace_errors, |
| 798 | METH_O, |
| 799 | PyDoc_STR("Implements the 'replace' error handling, which " |
| 800 | "replaces malformed data with a replacement marker.") |
| 801 | } |
| 802 | }, |
| 803 | { |
| 804 | "xmlcharrefreplace", |
| 805 | { |
| 806 | "xmlcharrefreplace_errors", |
| 807 | xmlcharrefreplace_errors, |
| 808 | METH_O, |
| 809 | PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " |
| 810 | "which replaces an unencodable character with the " |
| 811 | "appropriate XML character reference.") |
| 812 | } |
| 813 | }, |
| 814 | { |
| 815 | "backslashreplace", |
| 816 | { |
| 817 | "backslashreplace_errors", |
| 818 | backslashreplace_errors, |
| 819 | METH_O, |
| 820 | PyDoc_STR("Implements the 'backslashreplace' error handling, " |
| 821 | "which replaces an unencodable character with a " |
| 822 | "backslashed escape sequence.") |
| 823 | } |
| 824 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 825 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 826 | }; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 827 | |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 828 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 829 | PyObject *mod; |
Neal Norwitz | 739a8f8 | 2004-07-08 01:55:58 +0000 | [diff] [blame] | 830 | unsigned i; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 831 | |
| 832 | if (interp->codec_search_path != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 833 | return 0; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 834 | |
| 835 | interp->codec_search_path = PyList_New(0); |
| 836 | interp->codec_search_cache = PyDict_New(); |
| 837 | interp->codec_error_registry = PyDict_New(); |
| 838 | |
| 839 | if (interp->codec_error_registry) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 840 | for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { |
| 841 | PyObject *func = PyCFunction_New(&methods[i].def, NULL); |
| 842 | int res; |
| 843 | if (!func) |
| 844 | Py_FatalError("can't initialize codec error registry"); |
| 845 | res = PyCodec_RegisterError(methods[i].name, func); |
| 846 | Py_DECREF(func); |
| 847 | if (res) |
| 848 | Py_FatalError("can't initialize codec error registry"); |
| 849 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 850 | } |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 851 | |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 852 | if (interp->codec_search_path == NULL || |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 853 | interp->codec_search_cache == NULL || |
| 854 | interp->codec_error_registry == NULL) |
| 855 | Py_FatalError("can't initialize codec registry"); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 856 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 857 | mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 858 | if (mod == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 859 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 860 | /* Ignore ImportErrors... this is done so that |
| 861 | distributions can disable the encodings package. Note |
| 862 | that other errors are not masked, e.g. SystemErrors |
| 863 | raised to inform the user of an error in the Python |
| 864 | configuration are still reported back to the user. */ |
| 865 | PyErr_Clear(); |
| 866 | return 0; |
| 867 | } |
| 868 | return -1; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 869 | } |
| 870 | Py_DECREF(mod); |
| 871 | return 0; |
Guido van Rossum | feee4b9 | 2000-03-10 22:57:27 +0000 | [diff] [blame] | 872 | } |