Changeset 388 for python/vendor/current/Python/codecs.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Python/codecs.c
r2 r388 15 15 16 16 /* Import the standard encodings package which will register the first 17 codec search function. 17 codec search function. 18 18 19 19 This is done in a lazy way so that the Unicode implementation does … … 31 31 PyInterpreterState *interp = PyThreadState_GET()->interp; 32 32 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) 33 33 goto onError; 34 34 if (search_function == NULL) { 35 36 35 PyErr_BadArgument(); 36 goto onError; 37 37 } 38 38 if (!PyCallable_Check(search_function)) { 39 40 39 PyErr_SetString(PyExc_TypeError, "argument must be callable"); 40 goto onError; 41 41 } 42 42 return PyList_Append(interp->codec_search_path, search_function); … … 56 56 char *p; 57 57 PyObject *v; 58 58 59 59 if (len > PY_SSIZE_T_MAX) { 60 61 62 } 63 60 PyErr_SetString(PyExc_OverflowError, "string is too large"); 61 return NULL; 62 } 63 64 64 v = PyString_FromStringAndSize(NULL, len); 65 65 if (v == NULL) 66 66 return NULL; 67 67 p = PyString_AS_STRING(v); 68 68 for (i = 0; i < len; i++) { … … 71 71 ch = '-'; 72 72 else 73 ch = tolower(Py_CHARMASK(ch));74 73 ch = Py_TOLOWER(Py_CHARMASK(ch)); 74 p[i] = ch; 75 75 } 76 76 return v; … … 84 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 88 As side effect, this tries to load the encodings package, if not … … 99 99 100 100 if (encoding == NULL) { 101 102 101 PyErr_BadArgument(); 102 goto onError; 103 103 } 104 104 105 105 interp = PyThreadState_GET()->interp; 106 106 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) 107 107 goto onError; 108 108 109 109 /* Convert the encoding to a normalized Python string: all … … 112 112 v = normalizestring(encoding); 113 113 if (v == NULL) 114 114 goto onError; 115 115 PyString_InternInPlace(&v); 116 116 … … 118 118 result = PyDict_GetItem(interp->codec_search_cache, v); 119 119 if (result != NULL) { 120 121 122 123 } 124 120 Py_INCREF(result); 121 Py_DECREF(v); 122 return result; 123 } 124 125 125 /* Next, scan the search functions in order of registration */ 126 126 args = PyTuple_New(1); 127 127 if (args == NULL) 128 128 goto onError; 129 129 PyTuple_SET_ITEM(args,0,v); 130 130 131 131 len = PyList_Size(interp->codec_search_path); 132 132 if (len < 0) 133 133 goto onError; 134 134 if (len == 0) { 135 136 137 138 135 PyErr_SetString(PyExc_LookupError, 136 "no codec search functions registered: " 137 "can't find encoding"); 138 goto onError; 139 139 } 140 140 141 141 for (i = 0; i < len; i++) { 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 142 PyObject *func; 143 144 func = PyList_GetItem(interp->codec_search_path, i); 145 if (func == NULL) 146 goto onError; 147 result = PyEval_CallObject(func, args); 148 if (result == NULL) 149 goto onError; 150 if (result == Py_None) { 151 Py_DECREF(result); 152 continue; 153 } 154 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { 155 PyErr_SetString(PyExc_TypeError, 156 "codec search functions must return 4-tuples"); 157 Py_DECREF(result); 158 goto onError; 159 } 160 break; 161 161 } 162 162 if (i == len) { 163 164 163 /* XXX Perhaps we should cache misses too ? */ 164 PyErr_Format(PyExc_LookupError, 165 165 "unknown encoding: %s", encoding); 166 166 goto onError; 167 167 } 168 168 … … 179 179 static 180 180 PyObject *args_tuple(PyObject *object, 181 181 const char *errors) 182 182 { 183 183 PyObject *args; 184 184 185 185 args = PyTuple_New(1 + (errors != NULL)); 186 186 if (args == NULL) 187 187 return NULL; 188 188 Py_INCREF(object); 189 189 PyTuple_SET_ITEM(args,0,object); 190 190 if (errors) { 191 192 193 194 195 196 197 198 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 199 } 200 200 return args; … … 211 211 codecs = _PyCodec_Lookup(encoding); 212 212 if (codecs == NULL) 213 213 return NULL; 214 214 v = PyTuple_GET_ITEM(codecs, index); 215 215 Py_DECREF(codecs); … … 222 222 static 223 223 PyObject *codec_getincrementalcodec(const char *encoding, 224 225 224 const char *errors, 225 const char *attrname) 226 226 { 227 227 PyObject *codecs, *ret, *inccodec; … … 229 229 codecs = _PyCodec_Lookup(encoding); 230 230 if (codecs == NULL) 231 231 return NULL; 232 232 inccodec = PyObject_GetAttrString(codecs, attrname); 233 233 Py_DECREF(codecs); 234 234 if (inccodec == NULL) 235 235 return NULL; 236 236 if (errors) 237 237 ret = PyObject_CallFunction(inccodec, "s", errors); 238 238 else 239 239 ret = PyObject_CallFunction(inccodec, NULL); 240 240 Py_DECREF(inccodec); 241 241 return ret; … … 246 246 static 247 247 PyObject *codec_getstreamcodec(const char *encoding, 248 249 250 248 PyObject *stream, 249 const char *errors, 250 const int index) 251 251 { 252 252 PyObject *codecs, *streamcodec, *codeccls; … … 254 254 codecs = _PyCodec_Lookup(encoding); 255 255 if (codecs == NULL) 256 256 return NULL; 257 257 258 258 codeccls = PyTuple_GET_ITEM(codecs, index); 259 259 if (errors != NULL) 260 260 streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); 261 261 else 262 262 streamcodec = PyObject_CallFunction(codeccls, "O", stream); 263 263 Py_DECREF(codecs); 264 264 return streamcodec; 265 265 } 266 266 267 /* Convenience APIs to query the Codec registry. 268 267 /* Convenience APIs to query the Codec registry. 268 269 269 All APIs return a codec object with incremented refcount. 270 270 271 271 */ 272 272 … … 282 282 283 283 PyObject *PyCodec_IncrementalEncoder(const char *encoding, 284 284 const char *errors) 285 285 { 286 286 return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); … … 288 288 289 289 PyObject *PyCodec_IncrementalDecoder(const char *encoding, 290 290 const char *errors) 291 291 { 292 292 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); … … 294 294 295 295 PyObject *PyCodec_StreamReader(const char *encoding, 296 297 296 PyObject *stream, 297 const char *errors) 298 298 { 299 299 return codec_getstreamcodec(encoding, stream, errors, 2); … … 301 301 302 302 PyObject *PyCodec_StreamWriter(const char *encoding, 303 304 303 PyObject *stream, 304 const char *errors) 305 305 { 306 306 return codec_getstreamcodec(encoding, stream, errors, 3); … … 313 313 314 314 PyObject *PyCodec_Encode(PyObject *object, 315 316 315 const char *encoding, 316 const char *errors) 317 317 { 318 318 PyObject *encoder = NULL; … … 322 322 encoder = PyCodec_Encoder(encoding); 323 323 if (encoder == NULL) 324 324 goto onError; 325 325 326 326 args = args_tuple(object, errors); 327 327 if (args == NULL) 328 329 328 goto onError; 329 330 330 result = PyEval_CallObject(encoder,args); 331 331 if (result == NULL) 332 333 334 if (!PyTuple_Check(result) || 335 336 337 338 332 goto onError; 333 334 if (!PyTuple_Check(result) || 335 PyTuple_GET_SIZE(result) != 2) { 336 PyErr_SetString(PyExc_TypeError, 337 "encoder must return a tuple (object,integer)"); 338 goto onError; 339 339 } 340 340 v = PyTuple_GET_ITEM(result,0); … … 346 346 Py_DECREF(result); 347 347 return v; 348 348 349 349 onError: 350 350 Py_XDECREF(result); … … 360 360 361 361 PyObject *PyCodec_Decode(PyObject *object, 362 363 362 const char *encoding, 363 const char *errors) 364 364 { 365 365 PyObject *decoder = NULL; … … 369 369 decoder = PyCodec_Decoder(encoding); 370 370 if (decoder == NULL) 371 371 goto onError; 372 372 373 373 args = args_tuple(object, errors); 374 374 if (args == NULL) 375 376 375 goto onError; 376 377 377 result = PyEval_CallObject(decoder,args); 378 378 if (result == NULL) 379 380 if (!PyTuple_Check(result) || 381 382 383 384 379 goto onError; 380 if (!PyTuple_Check(result) || 381 PyTuple_GET_SIZE(result) != 2) { 382 PyErr_SetString(PyExc_TypeError, 383 "decoder must return a tuple (object,integer)"); 384 goto onError; 385 385 } 386 386 v = PyTuple_GET_ITEM(result,0); … … 392 392 Py_DECREF(result); 393 393 return v; 394 394 395 395 onError: 396 396 Py_XDECREF(args); … … 410 410 PyInterpreterState *interp = PyThreadState_GET()->interp; 411 411 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) 412 412 return -1; 413 413 if (!PyCallable_Check(error)) { 414 415 414 PyErr_SetString(PyExc_TypeError, "handler must be callable"); 415 return -1; 416 416 } 417 417 return PyDict_SetItemString(interp->codec_error_registry, 418 418 (char *)name, error); 419 419 } 420 420 … … 428 428 PyInterpreterState *interp = PyThreadState_GET()->interp; 429 429 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) 430 430 return NULL; 431 431 432 432 if (name==NULL) 433 433 name = "strict"; 434 434 handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); 435 435 if (!handler) 436 436 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); 437 437 else 438 438 Py_INCREF(handler); 439 439 return handler; 440 440 } … … 444 444 PyObject *type = PyObject_GetAttrString(exc, "__class__"); 445 445 if (type != NULL) { 446 447 448 449 450 451 452 453 454 455 456 457 446 PyObject *name = PyObject_GetAttrString(type, "__name__"); 447 Py_DECREF(type); 448 if (name != NULL) { 449 PyObject *string = PyObject_Str(name); 450 Py_DECREF(name); 451 if (string != NULL) { 452 PyErr_Format(PyExc_TypeError, 453 "don't know how to handle %.400s in error callback", 454 PyString_AS_STRING(string)); 455 Py_DECREF(string); 456 } 457 } 458 458 } 459 459 } … … 464 464 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); 465 465 else 466 466 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); 467 467 return NULL; 468 468 } … … 474 474 Py_ssize_t end; 475 475 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { 476 477 476 if (PyUnicodeEncodeError_GetEnd(exc, &end)) 477 return NULL; 478 478 } 479 479 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { 480 481 480 if (PyUnicodeDecodeError_GetEnd(exc, &end)) 481 return NULL; 482 482 } 483 483 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { 484 485 484 if (PyUnicodeTranslateError_GetEnd(exc, &end)) 485 return NULL; 486 486 } 487 487 else { 488 489 488 wrong_exception_type(exc); 489 return NULL; 490 490 } 491 491 /* ouch: passing NULL, 0, pos gives None instead of u'' */ … … 502 502 503 503 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 504 PyObject *res; 505 Py_UNICODE *p; 506 if (PyUnicodeEncodeError_GetStart(exc, &start)) 507 return NULL; 508 if (PyUnicodeEncodeError_GetEnd(exc, &end)) 509 return NULL; 510 res = PyUnicode_FromUnicode(NULL, end-start); 511 if (res == NULL) 512 return NULL; 513 for (p = PyUnicode_AS_UNICODE(res), i = start; 514 i<end; ++p, ++i) 515 *p = '?'; 516 restuple = Py_BuildValue("(On)", res, end); 517 Py_DECREF(res); 518 return restuple; 519 519 } 520 520 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { 521 522 523 524 return Py_BuildValue("(u#n)", &res,1, end);521 Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; 522 if (PyUnicodeDecodeError_GetEnd(exc, &end)) 523 return NULL; 524 return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end); 525 525 } 526 526 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 527 PyObject *res; 528 Py_UNICODE *p; 529 if (PyUnicodeTranslateError_GetStart(exc, &start)) 530 return NULL; 531 if (PyUnicodeTranslateError_GetEnd(exc, &end)) 532 return NULL; 533 res = PyUnicode_FromUnicode(NULL, end-start); 534 if (res == NULL) 535 return NULL; 536 for (p = PyUnicode_AS_UNICODE(res), i = start; 537 i<end; ++p, ++i) 538 *p = Py_UNICODE_REPLACEMENT_CHARACTER; 539 restuple = Py_BuildValue("(On)", res, end); 540 Py_DECREF(res); 541 return restuple; 542 542 } 543 543 else { 544 545 544 wrong_exception_type(exc); 545 return NULL; 546 546 } 547 547 } … … 550 550 { 551 551 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { 552 PyObject *restuple; 553 PyObject *object; 554 Py_ssize_t start; 555 Py_ssize_t end; 556 PyObject *res; 557 Py_UNICODE *p; 558 Py_UNICODE *startp; 559 Py_UNICODE *outp; 560 int ressize; 561 if (PyUnicodeEncodeError_GetStart(exc, &start)) 562 return NULL; 563 if (PyUnicodeEncodeError_GetEnd(exc, &end)) 564 return NULL; 565 if (!(object = PyUnicodeEncodeError_GetObject(exc))) 566 return NULL; 567 startp = PyUnicode_AS_UNICODE(object); 568 for (p = startp+start, ressize = 0; p < startp+end; ++p) { 569 if (*p<10) 570 ressize += 2+1+1; 571 else if (*p<100) 572 ressize += 2+2+1; 573 else if (*p<1000) 574 ressize += 2+3+1; 575 else if (*p<10000) 576 ressize += 2+4+1; 552 PyObject *restuple; 553 PyObject *object; 554 Py_ssize_t start; 555 Py_ssize_t end; 556 PyObject *res; 557 Py_UNICODE *p; 558 Py_UNICODE *startp; 559 Py_UNICODE *e; 560 Py_UNICODE *outp; 561 int ressize; 562 if (PyUnicodeEncodeError_GetStart(exc, &start)) 563 return NULL; 564 if (PyUnicodeEncodeError_GetEnd(exc, &end)) 565 return NULL; 566 if (!(object = PyUnicodeEncodeError_GetObject(exc))) 567 return NULL; 568 startp = PyUnicode_AS_UNICODE(object); 569 e = startp + end; 570 for (p = startp+start, ressize = 0; p < e;) { 571 Py_UCS4 ch = *p++; 577 572 #ifndef Py_UNICODE_WIDE 578 else 579 ressize += 2+5+1; 580 #else 581 else if (*p<100000) 582 ressize += 2+5+1; 583 else if (*p<1000000) 584 ressize += 2+6+1; 585 else 586 ressize += 2+7+1; 573 if ((0xD800 <= ch && ch <= 0xDBFF) && 574 (p < e) && 575 (0xDC00 <= *p && *p <= 0xDFFF)) { 576 ch = ((((ch & 0x03FF) << 10) | 577 ((Py_UCS4)*p++ & 0x03FF)) + 0x10000); 578 } 587 579 #endif 588 } 589 /* allocate replacement */ 590 res = PyUnicode_FromUnicode(NULL, ressize); 591 if (res == NULL) { 592 Py_DECREF(object); 593 return NULL; 594 } 595 /* generate replacement */ 596 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); 597 p < startp+end; ++p) { 598 Py_UNICODE c = *p; 599 int digits; 600 int base; 601 *outp++ = '&'; 602 *outp++ = '#'; 603 if (*p<10) { 604 digits = 1; 605 base = 1; 606 } 607 else if (*p<100) { 608 digits = 2; 609 base = 10; 610 } 611 else if (*p<1000) { 612 digits = 3; 613 base = 100; 614 } 615 else if (*p<10000) { 616 digits = 4; 617 base = 1000; 618 } 580 if (ch < 10) 581 ressize += 2+1+1; 582 else if (ch < 100) 583 ressize += 2+2+1; 584 else if (ch < 1000) 585 ressize += 2+3+1; 586 else if (ch < 10000) 587 ressize += 2+4+1; 588 else if (ch < 100000) 589 ressize += 2+5+1; 590 else if (ch < 1000000) 591 ressize += 2+6+1; 592 else 593 ressize += 2+7+1; 594 } 595 /* allocate replacement */ 596 res = PyUnicode_FromUnicode(NULL, ressize); 597 if (res == NULL) { 598 Py_DECREF(object); 599 return NULL; 600 } 601 /* generate replacement */ 602 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); p < e;) { 603 int digits; 604 int base; 605 Py_UCS4 ch = *p++; 619 606 #ifndef Py_UNICODE_WIDE 620 else { 621 digits = 5; 622 base = 10000; 623 } 624 #else 625 else if (*p<100000) { 626 digits = 5; 627 base = 10000; 628 } 629 else if (*p<1000000) { 630 digits = 6; 631 base = 100000; 632 } 633 else { 634 digits = 7; 635 base = 1000000; 636 } 607 if ((0xD800 <= ch && ch <= 0xDBFF) && 608 (p < startp+end) && 609 (0xDC00 <= *p && *p <= 0xDFFF)) { 610 ch = ((((ch & 0x03FF) << 10) | 611 ((Py_UCS4)*p++ & 0x03FF)) + 0x10000); 612 } 637 613 #endif 638 while (digits-->0) { 639 *outp++ = '0' + c/base; 640 c %= base; 641 base /= 10; 642 } 643 *outp++ = ';'; 644 } 645 restuple = Py_BuildValue("(On)", res, end); 646 Py_DECREF(res); 647 Py_DECREF(object); 648 return restuple; 614 *outp++ = '&'; 615 *outp++ = '#'; 616 if (ch < 10) { 617 digits = 1; 618 base = 1; 619 } 620 else if (ch < 100) { 621 digits = 2; 622 base = 10; 623 } 624 else if (ch < 1000) { 625 digits = 3; 626 base = 100; 627 } 628 else if (ch < 10000) { 629 digits = 4; 630 base = 1000; 631 } 632 else if (ch < 100000) { 633 digits = 5; 634 base = 10000; 635 } 636 else if (ch < 1000000) { 637 digits = 6; 638 base = 100000; 639 } 640 else { 641 digits = 7; 642 base = 1000000; 643 } 644 while (digits-->0) { 645 *outp++ = '0' + ch/base; 646 ch %= base; 647 base /= 10; 648 } 649 *outp++ = ';'; 650 } 651 restuple = Py_BuildValue("(On)", res, end); 652 Py_DECREF(res); 653 Py_DECREF(object); 654 return restuple; 649 655 } 650 656 else { 651 652 657 wrong_exception_type(exc); 658 return NULL; 653 659 } 654 660 } … … 662 668 { 663 669 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 670 PyObject *restuple; 671 PyObject *object; 672 Py_ssize_t start; 673 Py_ssize_t end; 674 PyObject *res; 675 Py_UNICODE *p; 676 Py_UNICODE *startp; 677 Py_UNICODE *outp; 678 int ressize; 679 if (PyUnicodeEncodeError_GetStart(exc, &start)) 680 return NULL; 681 if (PyUnicodeEncodeError_GetEnd(exc, &end)) 682 return NULL; 683 if (!(object = PyUnicodeEncodeError_GetObject(exc))) 684 return NULL; 685 startp = PyUnicode_AS_UNICODE(object); 686 for (p = startp+start, ressize = 0; p < startp+end; ++p) { 681 687 #ifdef Py_UNICODE_WIDE 682 683 684 688 if (*p >= 0x00010000) 689 ressize += 1+1+8; 690 else 685 691 #endif 686 687 688 689 690 691 692 693 694 695 696 697 698 692 if (*p >= 0x100) { 693 ressize += 1+1+4; 694 } 695 else 696 ressize += 1+1+2; 697 } 698 res = PyUnicode_FromUnicode(NULL, ressize); 699 if (res==NULL) 700 return NULL; 701 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); 702 p < startp+end; ++p) { 703 Py_UNICODE c = *p; 704 *outp++ = '\\'; 699 705 #ifdef Py_UNICODE_WIDE 700 701 702 703 704 705 706 707 708 709 706 if (c >= 0x00010000) { 707 *outp++ = 'U'; 708 *outp++ = hexdigits[(c>>28)&0xf]; 709 *outp++ = hexdigits[(c>>24)&0xf]; 710 *outp++ = hexdigits[(c>>20)&0xf]; 711 *outp++ = hexdigits[(c>>16)&0xf]; 712 *outp++ = hexdigits[(c>>12)&0xf]; 713 *outp++ = hexdigits[(c>>8)&0xf]; 714 } 715 else 710 716 #endif 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 717 if (c >= 0x100) { 718 *outp++ = 'u'; 719 *outp++ = hexdigits[(c>>12)&0xf]; 720 *outp++ = hexdigits[(c>>8)&0xf]; 721 } 722 else 723 *outp++ = 'x'; 724 *outp++ = hexdigits[(c>>4)&0xf]; 725 *outp++ = hexdigits[c&0xf]; 726 } 727 728 restuple = Py_BuildValue("(On)", res, end); 729 Py_DECREF(res); 730 Py_DECREF(object); 731 return restuple; 726 732 } 727 733 else { 728 729 734 wrong_exception_type(exc); 735 return NULL; 730 736 } 731 737 } … … 766 772 { 767 773 static struct { 768 769 774 char *name; 775 PyMethodDef def; 770 776 } methods[] = 771 777 { 772 773 774 775 776 777 778 779 780 781 778 { 779 "strict", 780 { 781 "strict_errors", 782 strict_errors, 783 METH_O, 784 PyDoc_STR("Implements the 'strict' error handling, which " 785 "raises a UnicodeError on coding errors.") 786 } 787 }, 782 788 #ifdef Py_USING_UNICODE 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 789 { 790 "ignore", 791 { 792 "ignore_errors", 793 ignore_errors, 794 METH_O, 795 PyDoc_STR("Implements the 'ignore' error handling, which " 796 "ignores malformed data and continues.") 797 } 798 }, 799 { 800 "replace", 801 { 802 "replace_errors", 803 replace_errors, 804 METH_O, 805 PyDoc_STR("Implements the 'replace' error handling, which " 806 "replaces malformed data with a replacement marker.") 807 } 808 }, 809 { 810 "xmlcharrefreplace", 811 { 812 "xmlcharrefreplace_errors", 813 xmlcharrefreplace_errors, 814 METH_O, 815 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " 816 "which replaces an unencodable character with the " 817 "appropriate XML character reference.") 818 } 819 }, 820 { 821 "backslashreplace", 822 { 823 "backslashreplace_errors", 824 backslashreplace_errors, 825 METH_O, 826 PyDoc_STR("Implements the 'backslashreplace' error handling, " 827 "which replaces an unencodable character with a " 828 "backslashed escape sequence.") 829 } 830 } 825 831 #endif 826 832 }; … … 831 837 832 838 if (interp->codec_search_path != NULL) 833 839 return 0; 834 840 835 841 interp->codec_search_path = PyList_New(0); … … 838 844 839 845 if (interp->codec_error_registry) { 840 841 842 843 844 845 846 847 848 849 846 for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { 847 PyObject *func = PyCFunction_New(&methods[i].def, NULL); 848 int res; 849 if (!func) 850 Py_FatalError("can't initialize codec error registry"); 851 res = PyCodec_RegisterError(methods[i].name, func); 852 Py_DECREF(func); 853 if (res) 854 Py_FatalError("can't initialize codec error registry"); 855 } 850 856 } 851 857 852 858 if (interp->codec_search_path == NULL || 853 854 855 859 interp->codec_search_cache == NULL || 860 interp->codec_error_registry == NULL) 861 Py_FatalError("can't initialize codec registry"); 856 862 857 863 mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); 858 864 if (mod == NULL) { 859 860 861 862 863 864 865 866 867 868 865 if (PyErr_ExceptionMatches(PyExc_ImportError)) { 866 /* Ignore ImportErrors... this is done so that 867 distributions can disable the encodings package. Note 868 that other errors are not masked, e.g. SystemErrors 869 raised to inform the user of an error in the Python 870 configuration are still reported back to the user. */ 871 PyErr_Clear(); 872 return 0; 873 } 874 return -1; 869 875 } 870 876 Py_DECREF(mod);
Note:
See TracChangeset
for help on using the changeset viewer.