[Python-checkins] r46003 - python/trunk/Python/exceptions.c

martin.v.loewis python-checkins at python.org
Mon May 15 11:22:28 CEST 2006


Author: martin.v.loewis
Date: Mon May 15 11:22:27 2006
New Revision: 46003

Modified:
   python/trunk/Python/exceptions.c
Log:
Remove bogus DECREF of self.
Change __str__() functions to METH_O.
Change WindowsError__str__ to use PyTuple_Pack.

Modified: python/trunk/Python/exceptions.c
==============================================================================
--- python/trunk/Python/exceptions.c	(original)
+++ python/trunk/Python/exceptions.c	Mon May 15 11:22:27 2006
@@ -223,12 +223,9 @@
 
 
 static PyObject *
-BaseException__str__(PyObject *self, PyObject *args)
+BaseException__str__(PyObject *_self, PyObject *self)
 {
-    PyObject *out;
-
-    if (!PyArg_ParseTuple(args, "O:__str__", &self))
-        return NULL;
+    PyObject *out, *args;
 
     args = PyObject_GetAttrString(self, "args");
     if (!args)
@@ -376,7 +373,7 @@
     /* methods for the BaseException class */
     {"__getitem__", BaseException__getitem__, METH_VARARGS},
     {"__repr__", BaseException__repr__, METH_VARARGS},
-    {"__str__",     BaseException__str__, METH_VARARGS},
+    {"__str__",     BaseException__str__, METH_O},
 #ifdef Py_USING_UNICODE
     {"__unicode__",  BaseException__unicode__, METH_VARARGS},
 #endif /* Py_USING_UNICODE */
@@ -617,17 +614,13 @@
 
 
 static PyObject *
-EnvironmentError__str__(PyObject *self, PyObject *args)
+EnvironmentError__str__(PyObject *originalself, PyObject *self)
 {
-    PyObject *originalself = self;
     PyObject *filename;
     PyObject *serrno;
     PyObject *strerror;
     PyObject *rtnval = NULL;
 
-    if (!PyArg_ParseTuple(args, "O:__str__", &self))
-	return NULL;
-
     filename = PyObject_GetAttrString(self, "filename");
     serrno = PyObject_GetAttrString(self, "errno");
     strerror = PyObject_GetAttrString(self, "strerror");
@@ -687,7 +680,7 @@
 	 * but there is no StandardError__str__() function; we happen to
 	 * know that's just a pass through to BaseException__str__().
 	 */
-	rtnval = BaseException__str__(originalself, args);
+	rtnval = BaseException__str__(originalself, self);
 
   finally:
     Py_XDECREF(filename);
@@ -700,7 +693,7 @@
 static
 PyMethodDef EnvironmentError_methods[] = {
     {"__init__", EnvironmentError__init__, METH_VARARGS},
-    {"__str__",  EnvironmentError__str__, METH_VARARGS},
+    {"__str__",  EnvironmentError__str__, METH_O},
     {NULL, NULL}
 };
 
@@ -746,23 +739,21 @@
 failed:
 	/* Could not set errno. */
 	Py_XDECREF(o_errcode);
-	Py_DECREF(self);
 	Py_DECREF(result);
 	return NULL;
 }
 
 static PyObject *
-WindowsError__str__(PyObject *self, PyObject *args)
+WindowsError__str__(PyObject *originalself, PyObject *self)
 {
-    PyObject *originalself = self;
     PyObject *filename;
     PyObject *serrno;
     PyObject *strerror;
+    PyObject *repr = NULL;
+    PyObject *fmt = NULL;
+    PyObject *tuple = NULL;
     PyObject *rtnval = NULL;
 
-    if (!PyArg_ParseTuple(args, "O:__str__", &self))
-	return NULL;
-
     filename = PyObject_GetAttrString(self, "filename");
     serrno = PyObject_GetAttrString(self, "winerror");
     strerror = PyObject_GetAttrString(self, "strerror");
@@ -770,64 +761,46 @@
 	goto finally;
 
     if (filename != Py_None) {
-	PyObject *fmt = PyString_FromString("[Error %s] %s: %s");
-	PyObject *repr = PyObject_Repr(filename);
-	PyObject *tuple = PyTuple_New(3);
-
-	if (!fmt || !repr || !tuple) {
-	    Py_XDECREF(fmt);
-	    Py_XDECREF(repr);
-	    Py_XDECREF(tuple);
+	fmt = PyString_FromString("[Error %s] %s: %s");
+	repr = PyObject_Repr(filename);
+	if (!fmt || !repr)
 	    goto finally;
-	}
+	
 
-	PyTuple_SET_ITEM(tuple, 0, serrno);
-	PyTuple_SET_ITEM(tuple, 1, strerror);
-	PyTuple_SET_ITEM(tuple, 2, repr);
+	tuple = PyTuple_Pack(3, serrno, strerror, repr);
+	if (!tuple)
+	    goto finally;
 
 	rtnval = PyString_Format(fmt, tuple);
-
-	Py_DECREF(fmt);
-	Py_DECREF(tuple);
-	/* already freed because tuple owned only reference */
-	serrno = NULL;
-	strerror = NULL;
     }
     else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
-	PyObject *fmt = PyString_FromString("[Error %s] %s");
-	PyObject *tuple = PyTuple_New(2);
-
-	if (!fmt || !tuple) {
-	    Py_XDECREF(fmt);
-	    Py_XDECREF(tuple);
+	fmt = PyString_FromString("[Error %s] %s");
+	if (!fmt)
 	    goto finally;
-	}
 
-	PyTuple_SET_ITEM(tuple, 0, serrno);
-	PyTuple_SET_ITEM(tuple, 1, strerror);
+	tuple = PyTuple_Pack(2, serrno, strerror);
+	if (!tuple)
+	    goto finally;
 
 	rtnval = PyString_Format(fmt, tuple);
-
-	Py_DECREF(fmt);
-	Py_DECREF(tuple);
-	/* already freed because tuple owned only reference */
-	serrno = NULL;
-	strerror = NULL;
     }
     else
-	rtnval = EnvironmentError__str__(originalself, args);
+	rtnval = EnvironmentError__str__(originalself, self);
 
   finally:
     Py_XDECREF(filename);
     Py_XDECREF(serrno);
     Py_XDECREF(strerror);
+    Py_XDECREF(repr);
+    Py_XDECREF(fmt);
+    Py_XDECREF(tuple);
     return rtnval;
 }
 
 static
 PyMethodDef WindowsError_methods[] = {
     {"__init__", WindowsError__init__, METH_VARARGS},
-    {"__str__", WindowsError__str__, METH_VARARGS},
+    {"__str__", WindowsError__str__, METH_O},
     {NULL, NULL}
 };
 #endif /* MS_WINDOWS */
@@ -972,15 +945,12 @@
 
 
 static PyObject *
-SyntaxError__str__(PyObject *self, PyObject *args)
+SyntaxError__str__(PyObject *_self, PyObject *self)
 {
     PyObject *msg;
     PyObject *str;
     PyObject *filename, *lineno, *result;
 
-    if (!PyArg_ParseTuple(args, "O:__str__", &self))
-	return NULL;
-
     if (!(msg = PyObject_GetAttrString(self, "msg")))
 	return NULL;
 
@@ -1045,20 +1015,17 @@
 
 static PyMethodDef SyntaxError_methods[] = {
     {"__init__", SyntaxError__init__, METH_VARARGS},
-    {"__str__",  SyntaxError__str__, METH_VARARGS},
+    {"__str__",  SyntaxError__str__, METH_O},
     {NULL, NULL}
 };
 
 
 static PyObject *
-KeyError__str__(PyObject *self, PyObject *args)
+KeyError__str__(PyObject *_self, PyObject *self)
 {
     PyObject *argsattr;
     PyObject *result;
 
-    if (!PyArg_ParseTuple(args, "O:__str__", &self))
-	return NULL;
-
     argsattr = PyObject_GetAttrString(self, "args");
     if (!argsattr)
 	    return NULL;
@@ -1077,14 +1044,14 @@
 	result = PyObject_Repr(key);
     }
     else
-	result = BaseException__str__(self, args);
+	result = BaseException__str__(_self, self);
 
     Py_DECREF(argsattr);
     return result;
 }
 
 static PyMethodDef KeyError_methods[] = {
-    {"__str__",  KeyError__str__, METH_VARARGS},
+    {"__str__",  KeyError__str__, METH_O},
     {NULL, NULL}
 };
 


More information about the Python-checkins mailing list