[Patches] killing off PyErr_BadArgument
Michael Hudson
mwh21@cam.ac.uk
Wed, 31 May 2000 22:02:09 +0100 (BST)
On Wed, 31 May 2000, Trent Mick wrote:
> On Wed, May 31, 2000 at 09:07:53PM +0100, Michael Hudson wrote:
> > 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).
>
> +1 Anything to improve exception error messages. I haven't tested the patch
> but its trivial.
I have, that's how I found out I'd removed gcc (!) from my machine...
test's pass, which is no surprise.
> > There were only four.
>
> There are a lot more in Modules/ and Python/
Yep:
mwh21@atrus src]$ grep -c PyErr_BadArgument -r . | grep -v ":0"
./Doc/api/api.tex:1
./Doc/api/refcounts.dat:1
./Doc/html/api/index.dat:1
./Doc/html/api/exceptionHandling.html:1
./Include/pyerrors.h:1
./Misc/RENAME:1
./Modules/almodule.c:7
./Modules/arraymodule.c:8
./Modules/cgensupport.c:25
./Modules/clmodule.c:2
./Modules/cstubs:11
./Modules/flmodule.c:4
./Modules/glmodule.c:11
./Modules/posixmodule.c:1
./Modules/structmodule.c:1
./Modules/svmodule.c:5
./Objects/bufferobject.c:3
./Objects/floatobject.c:1
./Objects/moduleobject.c:2
./Objects/unicodeobject.c:12
./PC/os2vacpp/python.def:1
./Python/errors.c:1
./Python/structmember.c:8
./Python/codecs.c:2
./TAGS:1
I'm not sure what Python/structmember.c is for, but I doubt you can get at
the PyErr_BadArguments from Python. The one in Python/errors.c is the
function definition, and the two in Python/codecs.c are likewise (I think)
inaccessible from Python. The stuff in Modules ... can wait.
Except for posixmodule.c (I thought I'd already done this one),
structmodule.c and arraymodule.c.
I think arraymodule.c could do with rewriting to use new-style argument
passing?
Cheers,
Michael
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.
Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.39
diff -u -r2.39 arraymodule.c
--- arraymodule.c 2000/05/03 23:44:31 2.39
+++ arraymodule.c 2000/05/31 20:58:52
@@ -540,7 +540,9 @@
int size;
arrayobject *np;
if (!is_arrayobject(bb)) {
- PyErr_BadArgument();
+ PyErr_Format(PyExc_TypeError,
+ "can only append array (not \"%.200s\") to array",
+ bb->ob_type->tp_name);
return NULL;
}
#define b ((arrayobject *)bb)
@@ -613,7 +615,9 @@
}
}
else {
- PyErr_BadArgument();
+ PyErr_Format(PyExc_TypeError,
+ "can only assign array (not \"%.200s\") to array slice",
+ v->ob_type->tp_name);
return -1;
}
if (ilow < 0)
@@ -821,7 +825,8 @@
char tmp[sizeof(double)]; /* Assume that's the max item size */
if (args != NULL) {
- PyErr_BadArgument();
+ PyErr_SetString(PyExc_TypeError,
+ "<array>.reverse requires exactly 0 arguments");
return NULL;
}
Index: posixmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.134
diff -u -r2.134 posixmodule.c
--- posixmodule.c 2000/05/03 02:44:55 2.134
+++ posixmodule.c 2000/05/31 20:58:56
@@ -1562,8 +1562,7 @@
getitem = PyTuple_GetItem;
}
else {
- badarg:
- PyErr_BadArgument();
+ PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
return NULL;
}
@@ -1573,7 +1572,9 @@
for (i = 0; i < argc; i++) {
if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
PyMem_DEL(argvlist);
- goto badarg;
+ PyErr_SetString(PyExc_TypeError,
+ "all arguments must be strings");
+ return NULL;
}
}
argvlist[argc] = NULL;
Index: structmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/structmodule.c,v
retrieving revision 2.30
diff -u -r2.30 structmodule.c
--- structmodule.c 2000/02/29 13:59:24 2.30
+++ structmodule.c 2000/05/31 20:58:57
@@ -1128,7 +1128,8 @@
if (args == NULL || !PyTuple_Check(args) ||
(n = PyTuple_Size(args)) < 1)
{
- PyErr_BadArgument();
+ PyErr_SetString(PyExc_TypeError,
+ "struct.pack requires at least one argument");
return NULL;
}
format = PyTuple_GetItem(args, 0);