[Patches] killing off PyErr_BadArgument

Michael Hudson mwh21@cam.ac.uk
Wed, 31 May 2000 21:07:53 +0100 (BST)


Prompted by jhylton on comp.lang.python, I've made a patch that I think
removes all the PyErr_BadArguments from code (just in Objects/* for now)
that you can reach by writing Python (I'm not sure about all the ones in
Objects/unicodeobject.c, but I don't think you can get at them from
Python-land).

There were only four.

I'm not particularly attached to the error messages I wrote.  Season to
taste.

Is this:

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

still necessary?

Anyways, here's the patch:

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.69
diff -u -r2.69 listobject.c
--- listobject.c	2000/05/03 23:44:35	2.69
+++ listobject.c	2000/05/31 20:01:01
@@ -388,7 +388,9 @@
 	int i;
 	PyListObject *np;
 	if (!PyList_Check(bb)) {
-		PyErr_BadArgument();
+		PyErr_Format(PyExc_TypeError,
+			     "can only append list (not \"%.200s\") to list",
+			     bb->ob_type->tp_name);
 		return NULL;
 	}
 #define b ((PyListObject *)bb)
@@ -469,7 +471,9 @@
 		}
 	}
 	else {
-		PyErr_BadArgument();
+		PyErr_Format(PyExc_TypeError,
+			     "must assign list (not \"%.200s\") to slice",
+			     v->ob_type->tp_name);
 		return -1;
 	}
 	if (ilow < 0)
Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.65
diff -u -r2.65 stringobject.c
--- stringobject.c	2000/05/08 14:08:05	2.65
+++ stringobject.c	2000/05/31 20:01:05
@@ -293,7 +293,9 @@
 	if (!PyString_Check(bb)) {
 		if (PyUnicode_Check(bb))
 		    return PyUnicode_Concat((PyObject *)a, bb);
-		PyErr_BadArgument();
+		PyErr_Format(PyExc_TypeError, 
+			     "cannot add type \"%.200s\" to string",
+			     bb->ob_type->tp_name);
 		return NULL;
 	}
 #define b ((PyStringObject *)bb)
Index: tupleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/tupleobject.c,v
retrieving revision 2.33
diff -u -r2.33 tupleobject.c
--- tupleobject.c	2000/05/03 23:44:36	2.33
+++ tupleobject.c	2000/05/31 20:01:05
@@ -361,7 +361,9 @@
 	register int i;
 	PyTupleObject *np;
 	if (!PyTuple_Check(bb)) {
-		PyErr_BadArgument();
+		PyErr_Format(PyExc_TypeError,
+       		     "can only append tuple (not \"%.200s\") to tuple",
+			     bb->ob_type->tp_name);
 		return NULL;
 	}
 #define b ((PyTupleObject *)bb)


Cheers,
M.