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