comparison Python/exceptions.c @ 38184:e7d30d75dae7 legacy-trunk

Remove bogus DECREF of self. Change __str__() functions to METH_O. Change WindowsError__str__ to use PyTuple_Pack.
author Martin v. Löwis <martin@v.loewis.de>
date Mon, 15 May 2006 09:22:27 +0000
parents e2ef60fec3da
children
comparison
equal deleted inserted replaced
38182:d8355cccff4a 38184:e7d30d75dae7
221 Py_RETURN_NONE; 221 Py_RETURN_NONE;
222 } 222 }
223 223
224 224
225 static PyObject * 225 static PyObject *
226 BaseException__str__(PyObject *self, PyObject *args) 226 BaseException__str__(PyObject *_self, PyObject *self)
227 { 227 {
228 PyObject *out; 228 PyObject *out, *args;
229
230 if (!PyArg_ParseTuple(args, "O:__str__", &self))
231 return NULL;
232 229
233 args = PyObject_GetAttrString(self, "args"); 230 args = PyObject_GetAttrString(self, "args");
234 if (!args) 231 if (!args)
235 return NULL; 232 return NULL;
236 233
374 static PyMethodDef 371 static PyMethodDef
375 BaseException_methods[] = { 372 BaseException_methods[] = {
376 /* methods for the BaseException class */ 373 /* methods for the BaseException class */
377 {"__getitem__", BaseException__getitem__, METH_VARARGS}, 374 {"__getitem__", BaseException__getitem__, METH_VARARGS},
378 {"__repr__", BaseException__repr__, METH_VARARGS}, 375 {"__repr__", BaseException__repr__, METH_VARARGS},
379 {"__str__", BaseException__str__, METH_VARARGS}, 376 {"__str__", BaseException__str__, METH_O},
380 #ifdef Py_USING_UNICODE 377 #ifdef Py_USING_UNICODE
381 {"__unicode__", BaseException__unicode__, METH_VARARGS}, 378 {"__unicode__", BaseException__unicode__, METH_VARARGS},
382 #endif /* Py_USING_UNICODE */ 379 #endif /* Py_USING_UNICODE */
383 {"__init__", BaseException__init__, METH_VARARGS}, 380 {"__init__", BaseException__init__, METH_VARARGS},
384 {NULL, NULL } 381 {NULL, NULL }
615 return rtnval; 612 return rtnval;
616 } 613 }
617 614
618 615
619 static PyObject * 616 static PyObject *
620 EnvironmentError__str__(PyObject *self, PyObject *args) 617 EnvironmentError__str__(PyObject *originalself, PyObject *self)
621 { 618 {
622 PyObject *originalself = self;
623 PyObject *filename; 619 PyObject *filename;
624 PyObject *serrno; 620 PyObject *serrno;
625 PyObject *strerror; 621 PyObject *strerror;
626 PyObject *rtnval = NULL; 622 PyObject *rtnval = NULL;
627
628 if (!PyArg_ParseTuple(args, "O:__str__", &self))
629 return NULL;
630 623
631 filename = PyObject_GetAttrString(self, "filename"); 624 filename = PyObject_GetAttrString(self, "filename");
632 serrno = PyObject_GetAttrString(self, "errno"); 625 serrno = PyObject_GetAttrString(self, "errno");
633 strerror = PyObject_GetAttrString(self, "strerror"); 626 strerror = PyObject_GetAttrString(self, "strerror");
634 if (!filename || !serrno || !strerror) 627 if (!filename || !serrno || !strerror)
685 * return StandardError.__str__(self) 678 * return StandardError.__str__(self)
686 * 679 *
687 * but there is no StandardError__str__() function; we happen to 680 * but there is no StandardError__str__() function; we happen to
688 * know that's just a pass through to BaseException__str__(). 681 * know that's just a pass through to BaseException__str__().
689 */ 682 */
690 rtnval = BaseException__str__(originalself, args); 683 rtnval = BaseException__str__(originalself, self);
691 684
692 finally: 685 finally:
693 Py_XDECREF(filename); 686 Py_XDECREF(filename);
694 Py_XDECREF(serrno); 687 Py_XDECREF(serrno);
695 Py_XDECREF(strerror); 688 Py_XDECREF(strerror);
698 691
699 692
700 static 693 static
701 PyMethodDef EnvironmentError_methods[] = { 694 PyMethodDef EnvironmentError_methods[] = {
702 {"__init__", EnvironmentError__init__, METH_VARARGS}, 695 {"__init__", EnvironmentError__init__, METH_VARARGS},
703 {"__str__", EnvironmentError__str__, METH_VARARGS}, 696 {"__str__", EnvironmentError__str__, METH_O},
704 {NULL, NULL} 697 {NULL, NULL}
705 }; 698 };
706 699
707 PyDoc_STRVAR(IOError__doc__, "I/O operation failed."); 700 PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
708 701
744 Py_DECREF(o_errcode); 737 Py_DECREF(o_errcode);
745 return result; 738 return result;
746 failed: 739 failed:
747 /* Could not set errno. */ 740 /* Could not set errno. */
748 Py_XDECREF(o_errcode); 741 Py_XDECREF(o_errcode);
749 Py_DECREF(self);
750 Py_DECREF(result); 742 Py_DECREF(result);
751 return NULL; 743 return NULL;
752 } 744 }
753 745
754 static PyObject * 746 static PyObject *
755 WindowsError__str__(PyObject *self, PyObject *args) 747 WindowsError__str__(PyObject *originalself, PyObject *self)
756 { 748 {
757 PyObject *originalself = self;
758 PyObject *filename; 749 PyObject *filename;
759 PyObject *serrno; 750 PyObject *serrno;
760 PyObject *strerror; 751 PyObject *strerror;
752 PyObject *repr = NULL;
753 PyObject *fmt = NULL;
754 PyObject *tuple = NULL;
761 PyObject *rtnval = NULL; 755 PyObject *rtnval = NULL;
762
763 if (!PyArg_ParseTuple(args, "O:__str__", &self))
764 return NULL;
765 756
766 filename = PyObject_GetAttrString(self, "filename"); 757 filename = PyObject_GetAttrString(self, "filename");
767 serrno = PyObject_GetAttrString(self, "winerror"); 758 serrno = PyObject_GetAttrString(self, "winerror");
768 strerror = PyObject_GetAttrString(self, "strerror"); 759 strerror = PyObject_GetAttrString(self, "strerror");
769 if (!filename || !serrno || !strerror) 760 if (!filename || !serrno || !strerror)
770 goto finally; 761 goto finally;
771 762
772 if (filename != Py_None) { 763 if (filename != Py_None) {
773 PyObject *fmt = PyString_FromString("[Error %s] %s: %s"); 764 fmt = PyString_FromString("[Error %s] %s: %s");
774 PyObject *repr = PyObject_Repr(filename); 765 repr = PyObject_Repr(filename);
775 PyObject *tuple = PyTuple_New(3); 766 if (!fmt || !repr)
776
777 if (!fmt || !repr || !tuple) {
778 Py_XDECREF(fmt);
779 Py_XDECREF(repr);
780 Py_XDECREF(tuple);
781 goto finally; 767 goto finally;
782 } 768
783 769
784 PyTuple_SET_ITEM(tuple, 0, serrno); 770 tuple = PyTuple_Pack(3, serrno, strerror, repr);
785 PyTuple_SET_ITEM(tuple, 1, strerror); 771 if (!tuple)
786 PyTuple_SET_ITEM(tuple, 2, repr); 772 goto finally;
787 773
788 rtnval = PyString_Format(fmt, tuple); 774 rtnval = PyString_Format(fmt, tuple);
789
790 Py_DECREF(fmt);
791 Py_DECREF(tuple);
792 /* already freed because tuple owned only reference */
793 serrno = NULL;
794 strerror = NULL;
795 } 775 }
796 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) { 776 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
797 PyObject *fmt = PyString_FromString("[Error %s] %s"); 777 fmt = PyString_FromString("[Error %s] %s");
798 PyObject *tuple = PyTuple_New(2); 778 if (!fmt)
799
800 if (!fmt || !tuple) {
801 Py_XDECREF(fmt);
802 Py_XDECREF(tuple);
803 goto finally; 779 goto finally;
804 } 780
805 781 tuple = PyTuple_Pack(2, serrno, strerror);
806 PyTuple_SET_ITEM(tuple, 0, serrno); 782 if (!tuple)
807 PyTuple_SET_ITEM(tuple, 1, strerror); 783 goto finally;
808 784
809 rtnval = PyString_Format(fmt, tuple); 785 rtnval = PyString_Format(fmt, tuple);
810
811 Py_DECREF(fmt);
812 Py_DECREF(tuple);
813 /* already freed because tuple owned only reference */
814 serrno = NULL;
815 strerror = NULL;
816 } 786 }
817 else 787 else
818 rtnval = EnvironmentError__str__(originalself, args); 788 rtnval = EnvironmentError__str__(originalself, self);
819 789
820 finally: 790 finally:
821 Py_XDECREF(filename); 791 Py_XDECREF(filename);
822 Py_XDECREF(serrno); 792 Py_XDECREF(serrno);
823 Py_XDECREF(strerror); 793 Py_XDECREF(strerror);
794 Py_XDECREF(repr);
795 Py_XDECREF(fmt);
796 Py_XDECREF(tuple);
824 return rtnval; 797 return rtnval;
825 } 798 }
826 799
827 static 800 static
828 PyMethodDef WindowsError_methods[] = { 801 PyMethodDef WindowsError_methods[] = {
829 {"__init__", WindowsError__init__, METH_VARARGS}, 802 {"__init__", WindowsError__init__, METH_VARARGS},
830 {"__str__", WindowsError__str__, METH_VARARGS}, 803 {"__str__", WindowsError__str__, METH_O},
831 {NULL, NULL} 804 {NULL, NULL}
832 }; 805 };
833 #endif /* MS_WINDOWS */ 806 #endif /* MS_WINDOWS */
834 807
835 #ifdef __VMS 808 #ifdef __VMS
970 return result; 943 return result;
971 } 944 }
972 945
973 946
974 static PyObject * 947 static PyObject *
975 SyntaxError__str__(PyObject *self, PyObject *args) 948 SyntaxError__str__(PyObject *_self, PyObject *self)
976 { 949 {
977 PyObject *msg; 950 PyObject *msg;
978 PyObject *str; 951 PyObject *str;
979 PyObject *filename, *lineno, *result; 952 PyObject *filename, *lineno, *result;
980
981 if (!PyArg_ParseTuple(args, "O:__str__", &self))
982 return NULL;
983 953
984 if (!(msg = PyObject_GetAttrString(self, "msg"))) 954 if (!(msg = PyObject_GetAttrString(self, "msg")))
985 return NULL; 955 return NULL;
986 956
987 str = PyObject_Str(msg); 957 str = PyObject_Str(msg);
1043 } 1013 }
1044 1014
1045 1015
1046 static PyMethodDef SyntaxError_methods[] = { 1016 static PyMethodDef SyntaxError_methods[] = {
1047 {"__init__", SyntaxError__init__, METH_VARARGS}, 1017 {"__init__", SyntaxError__init__, METH_VARARGS},
1048 {"__str__", SyntaxError__str__, METH_VARARGS}, 1018 {"__str__", SyntaxError__str__, METH_O},
1049 {NULL, NULL} 1019 {NULL, NULL}
1050 }; 1020 };
1051 1021
1052 1022
1053 static PyObject * 1023 static PyObject *
1054 KeyError__str__(PyObject *self, PyObject *args) 1024 KeyError__str__(PyObject *_self, PyObject *self)
1055 { 1025 {
1056 PyObject *argsattr; 1026 PyObject *argsattr;
1057 PyObject *result; 1027 PyObject *result;
1058
1059 if (!PyArg_ParseTuple(args, "O:__str__", &self))
1060 return NULL;
1061 1028
1062 argsattr = PyObject_GetAttrString(self, "args"); 1029 argsattr = PyObject_GetAttrString(self, "args");
1063 if (!argsattr) 1030 if (!argsattr)
1064 return NULL; 1031 return NULL;
1065 1032
1075 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) { 1042 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
1076 PyObject *key = PyTuple_GET_ITEM(argsattr, 0); 1043 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
1077 result = PyObject_Repr(key); 1044 result = PyObject_Repr(key);
1078 } 1045 }
1079 else 1046 else
1080 result = BaseException__str__(self, args); 1047 result = BaseException__str__(_self, self);
1081 1048
1082 Py_DECREF(argsattr); 1049 Py_DECREF(argsattr);
1083 return result; 1050 return result;
1084 } 1051 }
1085 1052
1086 static PyMethodDef KeyError_methods[] = { 1053 static PyMethodDef KeyError_methods[] = {
1087 {"__str__", KeyError__str__, METH_VARARGS}, 1054 {"__str__", KeyError__str__, METH_O},
1088 {NULL, NULL} 1055 {NULL, NULL}
1089 }; 1056 };
1090 1057
1091 1058
1092 #ifdef Py_USING_UNICODE 1059 #ifdef Py_USING_UNICODE