Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(77089)

Delta Between Two Patch Sets: Objects/exceptions.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 8 months ago
Right Patch Set: Created 7 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl. 2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 * 3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging. 4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */ 5 */
6 6
7 #define PY_SSIZE_T_CLEAN 7 #define PY_SSIZE_T_CLEAN
8 #include <Python.h> 8 #include <Python.h>
9 #include "structmember.h" 9 #include "structmember.h"
10 #include "osdefs.h" 10 #include "osdefs.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 51
52 return (PyObject *)self; 52 return (PyObject *)self;
53 } 53 }
54 54
55 static int 55 static int
56 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) 56 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
57 { 57 {
58 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) 58 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
59 return -1; 59 return -1;
60 60
61 Py_DECREF(self->args); 61 Py_XDECREF(self->args);
62 self->args = args; 62 self->args = args;
63 Py_INCREF(self->args); 63 Py_INCREF(self->args);
64 64
65 return 0; 65 return 0;
66 } 66 }
67 67
68 static int 68 static int
69 BaseException_clear(PyBaseExceptionObject *self) 69 BaseException_clear(PyBaseExceptionObject *self)
70 { 70 {
71 Py_CLEAR(self->dict); 71 Py_CLEAR(self->dict);
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 */ 482 */
483 SimpleExtendsException(PyExc_Exception, TypeError, 483 SimpleExtendsException(PyExc_Exception, TypeError,
484 "Inappropriate argument type."); 484 "Inappropriate argument type.");
485 485
486 486
487 /* 487 /*
488 * StopIteration extends Exception 488 * StopIteration extends Exception
489 */ 489 */
490 490
491 //SimpleExtendsException(PyExc_Exception, StopIteration, 491 //SimpleExtendsException(PyExc_Exception, StopIteration,
492 // "Signal the end from iterator.__next__()."); 492 // "Signal the end from iterator.__next__().");
Z. Jędrzejewski-Szmek 2012/01/12 19:06:52 Remove this comment?
493 493
494 static PyMemberDef StopIteration_members[] = { 494 static PyMemberDef StopIteration_members[] = {
495 {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0, 495 {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0,
496 PyDoc_STR("generator return value")}, 496 PyDoc_STR("generator return value")},
497 {NULL} /* Sentinel */ 497 {NULL} /* Sentinel */
498 }; 498 };
499 499
500 static int 500 static int
501 StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) 501 StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
502 { 502 {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 * called, giving a third argument which is the filename. But, so 645 * called, giving a third argument which is the filename. But, so
646 * that old code using in-place unpacking doesn't break, e.g.: 646 * that old code using in-place unpacking doesn't break, e.g.:
647 * 647 *
648 * except OSError, (errno, strerror): 648 * except OSError, (errno, strerror):
649 * 649 *
650 * we hack args so that it only contains two items. This also 650 * we hack args so that it only contains two items. This also
651 * means we need our own __str__() which prints out the filename 651 * means we need our own __str__() which prints out the filename
652 * when it was supplied. 652 * when it was supplied.
653 */ 653 */
654 654
655 static PyObject * 655 /* This function doesn't cleanup on error, the caller should */
656 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 656 static int
657 { 657 oserror_parse_args(PyObject **p_args,
658 PyOSErrorObject *self = NULL; 658 PyObject **myerrno, PyObject **strerror,
659 PyObject **filename
660 #ifdef MS_WINDOWS
661 , PyObject **winerror
662 #endif
663 )
664 {
659 Py_ssize_t nargs; 665 Py_ssize_t nargs;
660 666 PyObject *args = *p_args;
661 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL; 667
662 PyObject *subslice = NULL;
663 #ifdef MS_WINDOWS
664 PyObject *winerror = NULL;
665 long winerrcode = 0;
666 #endif
667
668 if (!_PyArg_NoKeywords(type->tp_name, kwds))
669 return NULL;
670 Py_INCREF(args);
671 nargs = PyTuple_GET_SIZE(args); 668 nargs = PyTuple_GET_SIZE(args);
672 669
673 #ifdef MS_WINDOWS 670 #ifdef MS_WINDOWS
674 if (nargs >= 2 && nargs <= 4) { 671 if (nargs >= 2 && nargs <= 4) {
675 if (!PyArg_UnpackTuple(args, "OSError", 2, 4, 672 if (!PyArg_UnpackTuple(args, "OSError", 2, 4,
676 &myerrno, &strerror, &filename, &winerror)) 673 myerrno, strerror, filename, winerror))
677 goto error; 674 return -1;
678 if (winerror && PyLong_Check(winerror)) { 675 if (*winerror && PyLong_Check(*winerror)) {
679 long errcode; 676 long errcode, winerrcode;
680 PyObject *newargs; 677 PyObject *newargs;
681 Py_ssize_t i; 678 Py_ssize_t i;
682 679
683 winerrcode = PyLong_AsLong(winerror); 680 winerrcode = PyLong_AsLong(*winerror);
684 if (winerrcode == -1 && PyErr_Occurred()) 681 if (winerrcode == -1 && PyErr_Occurred())
685 goto error; 682 return -1;
686 /* Set errno to the corresponding POSIX errno (overriding 683 /* Set errno to the corresponding POSIX errno (overriding
687 first argument). Windows Socket error codes (>= 10000) 684 first argument). Windows Socket error codes (>= 10000)
688 have the same value as their POSIX counterparts. 685 have the same value as their POSIX counterparts.
689 */ 686 */
690 if (winerrcode < 10000) 687 if (winerrcode < 10000)
691 errcode = winerror_to_errno(winerrcode); 688 errcode = winerror_to_errno(winerrcode);
692 else 689 else
693 errcode = winerrcode; 690 errcode = winerrcode;
694 myerrno = PyLong_FromLong(errcode); 691 *myerrno = PyLong_FromLong(errcode);
695 if (!myerrno) 692 if (!*myerrno)
696 goto error; 693 return -1;
697 newargs = PyTuple_New(nargs); 694 newargs = PyTuple_New(nargs);
698 if (!newargs) 695 if (!newargs)
699 goto error; 696 return -1;
700 PyTuple_SET_ITEM(newargs, 0, myerrno); 697 PyTuple_SET_ITEM(newargs, 0, *myerrno);
701 for (i = 1; i < nargs; i++) { 698 for (i = 1; i < nargs; i++) {
702 PyObject *val = PyTuple_GET_ITEM(args, i); 699 PyObject *val = PyTuple_GET_ITEM(args, i);
703 Py_INCREF(val); 700 Py_INCREF(val);
704 PyTuple_SET_ITEM(newargs, i, val); 701 PyTuple_SET_ITEM(newargs, i, val);
705 } 702 }
706 Py_DECREF(args); 703 Py_DECREF(args);
707 args = newargs; 704 args = *p_args = newargs;
708 } 705 }
709 } 706 }
710 #else 707 #else
711 if (nargs >= 2 && nargs <= 3) { 708 if (nargs >= 2 && nargs <= 3) {
712 if (!PyArg_UnpackTuple(args, "OSError", 2, 3, 709 if (!PyArg_UnpackTuple(args, "OSError", 2, 3,
713 &myerrno, &strerror, &filename)) 710 myerrno, strerror, filename))
714 goto error; 711 return -1;
715 } 712 }
716 #endif 713 #endif
717 if (myerrno && PyLong_Check(myerrno) && 714
718 errnomap && (PyObject *) type == PyExc_OSError) { 715 return 0;
719 PyObject *newtype; 716 }
720 newtype = PyDict_GetItem(errnomap, myerrno); 717
721 if (newtype) { 718 static int
722 assert(PyType_Check(newtype)); 719 oserror_init(PyOSErrorObject *self, PyObject **p_args,
723 type = (PyTypeObject *) newtype; 720 PyObject *myerrno, PyObject *strerror,
724 } 721 PyObject *filename
725 else if (PyErr_Occurred()) 722 #ifdef MS_WINDOWS
726 goto error; 723 , PyObject *winerror
727 } 724 #endif
728 725 )
729 self = (PyOSErrorObject *) type->tp_alloc(type, 0); 726 {
730 if (!self) 727 PyObject *args = *p_args;
731 goto error; 728 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
732
733 self->dict = NULL;
734 self->traceback = self->cause = self->context = NULL;
735 self->written = -1;
736 729
737 /* self->filename will remain Py_None otherwise */ 730 /* self->filename will remain Py_None otherwise */
738 if (filename && filename != Py_None) { 731 if (filename && filename != Py_None) {
739 if ((PyObject *) type == PyExc_BlockingIOError && 732 if (Py_TYPE(self) == (PyTypeObject *) PyExc_BlockingIOError &&
740 PyNumber_Check(filename)) { 733 PyNumber_Check(filename)) {
741 /* BlockingIOError's 3rd argument can be the number of 734 /* BlockingIOError's 3rd argument can be the number of
742 * characters written. 735 * characters written.
743 */ 736 */
744 self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError); 737 self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError);
745 if (self->written == -1 && PyErr_Occurred()) 738 if (self->written == -1 && PyErr_Occurred())
746 goto error; 739 return -1;
747 } 740 }
748 else { 741 else {
749 Py_INCREF(filename); 742 Py_INCREF(filename);
750 self->filename = filename; 743 self->filename = filename;
751 744
752 if (nargs >= 2 && nargs <= 3) { 745 if (nargs >= 2 && nargs <= 3) {
753 /* filename is removed from the args tuple (for compatibility 746 /* filename is removed from the args tuple (for compatibility
754 purposes, see test_exceptions.py) */ 747 purposes, see test_exceptions.py) */
755 subslice = PyTuple_GetSlice(args, 0, 2); 748 PyObject *subslice = PyTuple_GetSlice(args, 0, 2);
756 if (!subslice) 749 if (!subslice)
757 goto error; 750 return -1;
758 751
759 Py_DECREF(args); /* replacing args */ 752 Py_DECREF(args); /* replacing args */
760 args = subslice; 753 *p_args = args = subslice;
761 } 754 }
762 } 755 }
763 } 756 }
757 Py_XINCREF(myerrno);
758 self->myerrno = myerrno;
759
760 Py_XINCREF(strerror);
761 self->strerror = strerror;
762
763 #ifdef MS_WINDOWS
764 Py_XINCREF(winerror);
765 self->winerror = winerror;
766 #endif
764 767
765 /* Steals the reference to args */ 768 /* Steals the reference to args */
766 self->args = args; 769 self->args = args;
767 args = NULL; 770 args = NULL;
768 771
769 Py_XINCREF(myerrno); 772 return 0;
770 self->myerrno = myerrno; 773 }
771 774
772 Py_XINCREF(strerror); 775 static PyObject *
773 self->strerror = strerror; 776 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
774 777 static int
778 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
779
780 static int
781 oserror_use_init(PyTypeObject *type)
782 {
783 /* When __init__ is defined in a OSError subclass, we want any
784 extraneous argument to __new__ to be ignored. The only reasonable
785 solution, given __new__ takes a variable number of arguments,
786 is to defer arg parsing and initialization to __init__.
787
788 But when __new__ is overriden as well, it should call our __new__
789 with the right arguments.
790
791 (see http://bugs.python.org/issue12555#msg148829 )
792 */
793 if (type->tp_init != (initproc) OSError_init &&
794 type->tp_new == (newfunc) OSError_new) {
795 assert((PyObject *) type != PyExc_OSError);
796 return 1;
797 }
798 return 0;
799 }
800
801 static PyObject *
802 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
803 {
804 PyOSErrorObject *self = NULL;
805 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
775 #ifdef MS_WINDOWS 806 #ifdef MS_WINDOWS
776 Py_XINCREF(winerror); 807 PyObject *winerror = NULL;
777 self->winerror = winerror; 808 #endif
778 #endif 809
810 if (!oserror_use_init(type)) {
811 if (!_PyArg_NoKeywords(type->tp_name, kwds))
812 return NULL;
813
814 Py_INCREF(args);
815 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
816 #ifdef MS_WINDOWS
817 , &winerror
818 #endif
819 ))
820 goto error;
821
822 if (myerrno && PyLong_Check(myerrno) &&
823 errnomap && (PyObject *) type == PyExc_OSError) {
824 PyObject *newtype;
825 newtype = PyDict_GetItem(errnomap, myerrno);
826 if (newtype) {
827 assert(PyType_Check(newtype));
828 type = (PyTypeObject *) newtype;
829 }
830 else if (PyErr_Occurred())
831 goto error;
832 }
833 }
834
835 self = (PyOSErrorObject *) type->tp_alloc(type, 0);
836 if (!self)
837 goto error;
838
839 self->dict = NULL;
840 self->traceback = self->cause = self->context = NULL;
841 self->written = -1;
842
843 if (!oserror_use_init(type)) {
844 if (oserror_init(self, &args, myerrno, strerror, filename
845 #ifdef MS_WINDOWS
846 , winerror
847 #endif
848 ))
849 goto error;
850 }
779 851
780 return (PyObject *) self; 852 return (PyObject *) self;
781 853
782 error: 854 error:
783 Py_XDECREF(args); 855 Py_XDECREF(args);
784 Py_XDECREF(self); 856 Py_XDECREF(self);
785 return NULL; 857 return NULL;
786 } 858 }
787 859
788 static int 860 static int
789 OSError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) 861 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
790 { 862 {
791 /* Everything already done in OSError_new */ 863 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
792 return 0; 864 #ifdef MS_WINDOWS
865 PyObject *winerror = NULL;
866 #endif
867
868 if (!oserror_use_init(Py_TYPE(self)))
869 /* Everything already done in OSError_new */
870 return 0;
871
872 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
873 return -1;
874
875 Py_INCREF(args);
876 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
877 #ifdef MS_WINDOWS
878 , &winerror
879 #endif
880 ))
881 goto error;
882
883 if (oserror_init(self, &args, myerrno, strerror, filename
884 #ifdef MS_WINDOWS
885 , winerror
886 #endif
887 ))
888 goto error;
889
890 return 0;
891
892 error:
893 Py_XDECREF(args);
894 return -1;
793 } 895 }
794 896
795 static int 897 static int
796 OSError_clear(PyOSErrorObject *self) 898 OSError_clear(PyOSErrorObject *self)
797 { 899 {
798 Py_CLEAR(self->myerrno); 900 Py_CLEAR(self->myerrno);
799 Py_CLEAR(self->strerror); 901 Py_CLEAR(self->strerror);
800 Py_CLEAR(self->filename); 902 Py_CLEAR(self->filename);
801 #ifdef MS_WINDOWS 903 #ifdef MS_WINDOWS
802 Py_CLEAR(self->winerror); 904 Py_CLEAR(self->winerror);
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
1331 1433
1332 int 1434 int
1333 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) 1435 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1334 { 1436 {
1335 Py_ssize_t size; 1437 Py_ssize_t size;
1336 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1438 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1337 "object"); 1439 "object");
1338 if (!obj) 1440 if (!obj)
1339 return -1; 1441 return -1;
1340 *start = ((PyUnicodeErrorObject *)exc)->start; 1442 *start = ((PyUnicodeErrorObject *)exc)->start;
1341 size = PyUnicode_GET_SIZE(obj); 1443 size = PyUnicode_GET_LENGTH(obj);
1342 if (*start<0) 1444 if (*start<0)
1343 *start = 0; /*XXX check for values <0*/ 1445 *start = 0; /*XXX check for values <0*/
1344 if (*start>=size) 1446 if (*start>=size)
1345 *start = size-1; 1447 *start = size-1;
1346 Py_DECREF(obj); 1448 Py_DECREF(obj);
1347 return 0; 1449 return 0;
1348 } 1450 }
1349 1451
1350 1452
1351 int 1453 int
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1399 1501
1400 int 1502 int
1401 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) 1503 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1402 { 1504 {
1403 Py_ssize_t size; 1505 Py_ssize_t size;
1404 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1506 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1405 "object"); 1507 "object");
1406 if (!obj) 1508 if (!obj)
1407 return -1; 1509 return -1;
1408 *end = ((PyUnicodeErrorObject *)exc)->end; 1510 *end = ((PyUnicodeErrorObject *)exc)->end;
1409 size = PyUnicode_GET_SIZE(obj); 1511 size = PyUnicode_GET_LENGTH(obj);
1410 if (*end<1) 1512 if (*end<1)
1411 *end = 1; 1513 *end = 1;
1412 if (*end>size) 1514 if (*end>size)
1413 *end = size; 1515 *end = size;
1414 Py_DECREF(obj); 1516 Py_DECREF(obj);
1415 return 0; 1517 return 0;
1416 } 1518 }
1417 1519
1418 1520
1419 int 1521 int
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1571 if (!PyArg_ParseTuple(args, "O!O!nnO!", 1673 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1572 &PyUnicode_Type, &err->encoding, 1674 &PyUnicode_Type, &err->encoding,
1573 &PyUnicode_Type, &err->object, 1675 &PyUnicode_Type, &err->object,
1574 &err->start, 1676 &err->start,
1575 &err->end, 1677 &err->end,
1576 &PyUnicode_Type, &err->reason)) { 1678 &PyUnicode_Type, &err->reason)) {
1577 err->encoding = err->object = err->reason = NULL; 1679 err->encoding = err->object = err->reason = NULL;
1578 return -1; 1680 return -1;
1579 } 1681 }
1580 1682
1683 if (PyUnicode_READY(err->object) < -1) {
1684 err->encoding = NULL;
1685 return -1;
1686 }
1687
1581 Py_INCREF(err->encoding); 1688 Py_INCREF(err->encoding);
1582 Py_INCREF(err->object); 1689 Py_INCREF(err->object);
1583 Py_INCREF(err->reason); 1690 Py_INCREF(err->reason);
1584 1691
1585 return 0; 1692 return 0;
1586 } 1693 }
1587 1694
1588 static PyObject * 1695 static PyObject *
1589 UnicodeEncodeError_str(PyObject *self) 1696 UnicodeEncodeError_str(PyObject *self)
1590 { 1697 {
1591 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 1698 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1592 PyObject *result = NULL; 1699 PyObject *result = NULL;
1593 PyObject *reason_str = NULL; 1700 PyObject *reason_str = NULL;
1594 PyObject *encoding_str = NULL; 1701 PyObject *encoding_str = NULL;
1595 1702
1596 /* Get reason and encoding as strings, which they might not be if 1703 /* Get reason and encoding as strings, which they might not be if
1597 they've been modified after we were contructed. */ 1704 they've been modified after we were contructed. */
1598 reason_str = PyObject_Str(uself->reason); 1705 reason_str = PyObject_Str(uself->reason);
1599 if (reason_str == NULL) 1706 if (reason_str == NULL)
1600 goto done; 1707 goto done;
1601 encoding_str = PyObject_Str(uself->encoding); 1708 encoding_str = PyObject_Str(uself->encoding);
1602 if (encoding_str == NULL) 1709 if (encoding_str == NULL)
1603 goto done; 1710 goto done;
1604 1711
1605 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 1712 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1606 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 1713 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1607 const char *fmt; 1714 const char *fmt;
1608 if (badchar <= 0xff) 1715 if (badchar <= 0xff)
1609 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; 1716 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
1610 else if (badchar <= 0xffff) 1717 else if (badchar <= 0xffff)
1611 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; 1718 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
1612 else 1719 else
1613 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; 1720 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
1614 result = PyUnicode_FromFormat( 1721 result = PyUnicode_FromFormat(
1615 fmt, 1722 fmt,
1616 encoding_str, 1723 encoding_str,
1617 badchar, 1724 (int)badchar,
1618 uself->start, 1725 uself->start,
1619 reason_str); 1726 reason_str);
1620 } 1727 }
1621 else { 1728 else {
1622 result = PyUnicode_FromFormat( 1729 result = PyUnicode_FromFormat(
1623 "'%U' codec can't encode characters in position %zd-%zd: %U", 1730 "'%U' codec can't encode characters in position %zd-%zd: %U",
1624 encoding_str, 1731 encoding_str,
1625 uself->start, 1732 uself->start,
1626 uself->end-1, 1733 uself->end-1,
1627 reason_str); 1734 reason_str);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1804 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 1911 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1805 PyObject *result = NULL; 1912 PyObject *result = NULL;
1806 PyObject *reason_str = NULL; 1913 PyObject *reason_str = NULL;
1807 1914
1808 /* Get reason as a string, which it might not be if it's been 1915 /* Get reason as a string, which it might not be if it's been
1809 modified after we were contructed. */ 1916 modified after we were contructed. */
1810 reason_str = PyObject_Str(uself->reason); 1917 reason_str = PyObject_Str(uself->reason);
1811 if (reason_str == NULL) 1918 if (reason_str == NULL)
1812 goto done; 1919 goto done;
1813 1920
1814 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 1921 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1815 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 1922 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1816 const char *fmt; 1923 const char *fmt;
1817 if (badchar <= 0xff) 1924 if (badchar <= 0xff)
1818 fmt = "can't translate character '\\x%02x' in position %zd: %U"; 1925 fmt = "can't translate character '\\x%02x' in position %zd: %U";
1819 else if (badchar <= 0xffff) 1926 else if (badchar <= 0xffff)
1820 fmt = "can't translate character '\\u%04x' in position %zd: %U"; 1927 fmt = "can't translate character '\\u%04x' in position %zd: %U";
1821 else 1928 else
1822 fmt = "can't translate character '\\U%08x' in position %zd: %U"; 1929 fmt = "can't translate character '\\U%08x' in position %zd: %U";
1823 result = PyUnicode_FromFormat( 1930 result = PyUnicode_FromFormat(
1824 fmt, 1931 fmt,
1825 badchar, 1932 (int)badchar,
1826 uself->start, 1933 uself->start,
1827 reason_str 1934 reason_str
1828 ); 1935 );
1829 } else { 1936 } else {
1830 result = PyUnicode_FromFormat( 1937 result = PyUnicode_FromFormat(
1831 "can't translate characters in position %zd-%zd: %U", 1938 "can't translate characters in position %zd-%zd: %U",
1832 uself->start, 1939 uself->start,
1833 uself->end-1, 1940 uself->end-1,
1834 reason_str 1941 reason_str
1835 ); 1942 );
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
2359 Py_DECREF(bltinmod); 2466 Py_DECREF(bltinmod);
2360 } 2467 }
2361 2468
2362 void 2469 void
2363 _PyExc_Fini(void) 2470 _PyExc_Fini(void)
2364 { 2471 {
2365 Py_CLEAR(PyExc_RecursionErrorInst); 2472 Py_CLEAR(PyExc_RecursionErrorInst);
2366 free_preallocated_memerrors(); 2473 free_preallocated_memerrors();
2367 Py_CLEAR(errnomap); 2474 Py_CLEAR(errnomap);
2368 } 2475 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+