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