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