Ka-Ping Yee <ping@lfw.org>:
Changes to error messages to increase consistency & clarity.
This (mostly) closes SourceForge patch #101839.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 4ca1310..7c8cf18 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -71,7 +71,7 @@
if (!PyTuple_Check(alist)) {
if (!PySequence_Check(alist)) {
PyErr_SetString(PyExc_TypeError,
- "apply() 2nd argument must be a sequence");
+ "apply() arg 2 must be a sequence");
return NULL;
}
t = PySequence_Tuple(alist);
@@ -82,7 +82,7 @@
}
if (kwdict != NULL && !PyDict_Check(kwdict)) {
PyErr_SetString(PyExc_TypeError,
- "apply() 3rd argument must be dictionary");
+ "apply() arg 3 must be a dictionary");
goto finally;
}
retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
@@ -181,7 +181,7 @@
sqf = seq->ob_type->tp_as_sequence;
if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
- "argument 2 to filter() must be a sequence type");
+ "filter() arg 2 must be a sequence");
goto Fail_2;
}
@@ -368,7 +368,7 @@
start = Py_single_input;
else {
PyErr_SetString(PyExc_ValueError,
- "compile() mode must be 'exec' or 'eval' or 'single'");
+ "compile() arg 3 must be 'exec' or 'eval' or 'single'");
return NULL;
}
return Py_CompileString(str, filename, start);
@@ -421,7 +421,7 @@
}
else if (PyObject_AsCharBuffer(v, &s, &len)) {
PyErr_SetString(PyExc_TypeError,
- "complex() needs a string first argument");
+ "complex() arg is not a string");
return NULL;
}
@@ -431,7 +431,7 @@
s++;
if (s[0] == '\0') {
PyErr_SetString(PyExc_ValueError,
- "empty string for complex()");
+ "complex() arg is an empty string");
return NULL;
}
@@ -445,7 +445,7 @@
if (s-start != len) {
PyErr_SetString(
PyExc_ValueError,
- "null byte in argument for complex()");
+ "complex() arg contains a null byte");
return NULL;
}
if(!done) sw_error=1;
@@ -531,7 +531,7 @@
if (sw_error) {
PyErr_SetString(PyExc_ValueError,
- "malformed string for complex()");
+ "complex() arg is a malformed string");
return NULL;
}
@@ -557,7 +557,7 @@
((nbi = i->ob_type->tp_as_number) == NULL ||
nbi->nb_float == NULL))) {
PyErr_SetString(PyExc_TypeError,
- "complex() argument can't be converted to complex");
+ "complex() arg can't be converted to complex");
return NULL;
}
/* XXX Hack to support classes with __complex__ method */
@@ -748,7 +748,7 @@
if (!PyString_Check(cmd) &&
!PyUnicode_Check(cmd)) {
PyErr_SetString(PyExc_TypeError,
- "eval() argument 1 must be string or code object");
+ "eval() arg 1 must be a string or code object");
return NULL;
}
if (PyString_AsStringAndSize(cmd, &str, NULL))
@@ -1224,7 +1224,7 @@
base);
else {
PyErr_SetString(PyExc_TypeError,
- "can't convert non-string with explicit base");
+ "int() can't convert non-string with explicit base");
return NULL;
}
}
@@ -1257,7 +1257,7 @@
base);
else {
PyErr_SetString(PyExc_TypeError,
- "can't convert non-string with explicit base");
+ "long() can't convert non-string with explicit base");
return NULL;
}
}
@@ -1384,7 +1384,7 @@
sq = v->ob_type->tp_as_sequence;
if (sq == NULL || sq->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
- "min() or max() of non-sequence");
+ "min() or max() arg must be a sequence");
return NULL;
}
w = NULL;
@@ -1417,7 +1417,7 @@
}
if (w == NULL)
PyErr_SetString(PyExc_ValueError,
- "min() or max() of empty sequence");
+ "min() or max() arg is an empty sequence");
return w;
}
@@ -1520,7 +1520,7 @@
ord = (long)*PyUnicode_AS_UNICODE(obj);
} else {
PyErr_Format(PyExc_TypeError,
- "expected string or Unicode character, " \
+ "ord() expected string or Unicode character, " \
"%.200s found", obj->ob_type->tp_name);
return NULL;
}
@@ -1528,7 +1528,7 @@
return PyInt_FromLong(ord);
PyErr_Format(PyExc_TypeError,
- "expected a character, length-%d string found",
+ "ord() expected a character, length-%d string found",
size);
return NULL;
}
@@ -1607,7 +1607,7 @@
return NULL;
}
if (istep == 0) {
- PyErr_SetString(PyExc_ValueError, "zero step for range()");
+ PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
return NULL;
}
if (istep > 0)
@@ -1617,7 +1617,7 @@
n = (int)bign;
if (bign < 0 || (long)n != bign) {
PyErr_SetString(PyExc_OverflowError,
- "range() has too many items");
+ "range() result has too many items");
return NULL;
}
v = PyList_New(n);
@@ -1664,7 +1664,7 @@
return NULL;
}
if (istep == 0) {
- PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
+ PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
return NULL;
}
if (istep > 0)
@@ -1673,7 +1673,7 @@
n = get_len_of_range(ihigh, ilow, -istep);
if (n < 0) {
PyErr_SetString(PyExc_OverflowError,
- "xrange() has more than sys.maxint items");
+ "xrange() result has too many items");
return NULL;
}
return PyRange_New(ilow, n, istep, 1);
@@ -1779,7 +1779,7 @@
sqf = seq->ob_type->tp_as_sequence;
if (sqf == NULL || sqf->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
- "2nd argument to reduce() must be a sequence object");
+ "reduce() arg 2 must be a sequence");
return NULL;
}
@@ -1817,7 +1817,7 @@
if (result == NULL)
PyErr_SetString(PyExc_TypeError,
- "reduce of empty sequence with no initial value");
+ "reduce() of empty sequence with no initial value");
return result;
@@ -2073,7 +2073,7 @@
if (icls != NULL) {
retval = abstract_issubclass(
icls, cls,
- "second argument must be a class",
+ "isinstance() arg 2 must be a class",
1);
Py_DECREF(icls);
if (retval < 0)
@@ -2081,13 +2081,13 @@
}
else {
PyErr_SetString(PyExc_TypeError,
- "second argument must be a class");
+ "isinstance() arg 2 must be a class");
return NULL;
}
}
else {
PyErr_SetString(PyExc_TypeError,
- "second argument must be a class");
+ "isinstance() arg 2 must be a class");
return NULL;
}
return PyInt_FromLong(retval);
@@ -2140,7 +2140,7 @@
if (itemsize < 1) {
PyErr_SetString(PyExc_TypeError,
- "at least one sequence is required");
+ "zip() requires at least one sequence");
return NULL;
}
/* args must be a tuple */
diff --git a/Python/ceval.c b/Python/ceval.c
index a1c8190..df057b7 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -61,9 +61,9 @@
static void format_exc_check_arg(PyObject *, char *, PyObject *);
#define NAME_ERROR_MSG \
- "There is no variable named '%s'"
+ "name '%.200s' is not defined"
#define UNBOUNDLOCAL_ERROR_MSG \
- "Local variable '%.200s' referenced before assignment"
+ "local variable '%.200s' referenced before assignment"
/* Dynamic execution profile */
#ifdef DYNAMIC_EXECUTION_PROFILE
@@ -439,8 +439,10 @@
if (argcount > co->co_argcount) {
if (!(co->co_flags & CO_VARARGS)) {
PyErr_Format(PyExc_TypeError,
- "too many arguments; expected %d, got %d",
- co->co_argcount, argcount);
+ "too many arguments to %s(); "
+ "expected %d, got %d",
+ PyString_AsString(co->co_name),
+ co->co_argcount, argcount);
goto fail;
}
n = co->co_argcount;
@@ -483,7 +485,9 @@
if (j >= co->co_argcount) {
if (kwdict == NULL) {
PyErr_Format(PyExc_TypeError,
- "unexpected keyword argument: %.400s",
+ "%.200s() got an unexpected "
+ "keyword argument '%.400s'",
+ PyString_AsString(co->co_name),
PyString_AsString(keyword));
goto fail;
}
@@ -492,8 +496,10 @@
else {
if (GETLOCAL(j) != NULL) {
PyErr_Format(PyExc_TypeError,
- "keyword parameter redefined: %.400s",
- PyString_AsString(keyword));
+ "keyword parameter '%.400s' "
+ "redefined in call to %.200s()",
+ PyString_AsString(keyword),
+ PyString_AsString(co->co_name));
goto fail;
}
Py_INCREF(value);
@@ -505,8 +511,10 @@
for (i = argcount; i < m; i++) {
if (GETLOCAL(i) == NULL) {
PyErr_Format(PyExc_TypeError,
- "not enough arguments; expected %d, got %d",
- m, i);
+ "not enough arguments to "
+ "%.200s(); expected %d, got %d",
+ PyString_AsString(co->co_name),
+ m, i);
goto fail;
}
}
@@ -525,8 +533,9 @@
}
else {
if (argcount > 0 || kwcount > 0) {
- PyErr_SetString(PyExc_TypeError,
- "no arguments expected");
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() expected no arguments",
+ PyString_AsString(co->co_name));
goto fail;
}
}
@@ -565,7 +574,7 @@
if (++tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth;
PyErr_SetString(PyExc_RuntimeError,
- "Maximum recursion depth exceeded");
+ "maximum recursion depth exceeded");
tstate->frame = f->f_back;
Py_DECREF(f);
return NULL;
@@ -1825,7 +1834,7 @@
/* Handy-dandy */ ;
else {
PyErr_SetString(PyExc_TypeError,
- "unbound method must be called with class instance 1st argument");
+ "unbound method must be called with instance as first argument");
x = NULL;
break;
}
@@ -1908,9 +1917,10 @@
PyObject *key = POP();
if (PyDict_GetItem(kwdict, key) != NULL) {
err = 1;
- PyErr_Format(PyExc_TypeError,
- "keyword parameter redefined: %.400s",
- PyString_AsString(key));
+ PyErr_Format(PyExc_TypeError,
+ "keyword parameter '%.400s' "
+ "redefined in function call",
+ PyString_AsString(key));
Py_DECREF(key);
Py_DECREF(value);
goto extcall_fail;
@@ -2308,7 +2318,7 @@
}
else if (tb != NULL && !PyTraceBack_Check(tb)) {
PyErr_SetString(PyExc_TypeError,
- "raise 3rd arg must be traceback or None");
+ "raise: arg 3 must be a traceback or None");
goto raise_error;
}
@@ -2630,6 +2640,7 @@
call_builtin(PyObject *func, PyObject *arg, PyObject *kw)
{
if (PyCFunction_Check(func)) {
+ PyCFunctionObject* f = (PyCFunctionObject*) func;
PyCFunction meth = PyCFunction_GetFunction(func);
PyObject *self = PyCFunction_GetSelf(func);
int flags = PyCFunction_GetFlags(func);
@@ -2643,8 +2654,9 @@
if (flags & METH_KEYWORDS)
return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
if (kw != NULL && PyDict_Size(kw) != 0) {
- PyErr_SetString(PyExc_TypeError,
- "this function takes no keyword arguments");
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() takes no keyword arguments",
+ f->m_ml->ml_name);
return NULL;
}
return (*meth)(self, arg);
@@ -2653,11 +2665,13 @@
return PyInstance_New(func, arg, kw);
}
if (PyInstance_Check(func)) {
- PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
+ PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
if (call == NULL) {
+ PyInstanceObject *inst = (PyInstanceObject*) func;
PyErr_Clear();
- PyErr_SetString(PyExc_AttributeError,
- "no __call__ method defined");
+ PyErr_Format(PyExc_AttributeError,
+ "%.200s instance has no __call__ method",
+ PyString_AsString(inst->in_class->cl_name));
return NULL;
}
res = PyEval_CallObjectWithKeywords(call, arg, kw);
@@ -2703,7 +2717,7 @@
}
if (self == NULL) {
PyErr_SetString(PyExc_TypeError,
- "unbound method must be called with class instance 1st argument");
+ "unbound method must be called with instance as first argument");
return NULL;
}
Py_INCREF(arg);
@@ -2849,7 +2863,7 @@
}
} else {
PyErr_SetString(PyExc_TypeError,
- "slice index must be int");
+ "slice indices must be integers");
return 0;
}
/* Truncate -- very long indices are truncated anyway */
@@ -2935,7 +2949,7 @@
PyObject *w, *x;
if (!PyModule_Check(v)) {
PyErr_SetString(PyExc_TypeError,
- "import-from requires module object");
+ "import-from requires a module object");
return NULL;
}
w = PyModule_GetDict(v); /* TDB: can this not fail ? */
@@ -2958,7 +2972,7 @@
if (!PyModule_Check(v)) {
PyErr_SetString(PyExc_TypeError,
- "import-from requires module object");
+ "import-from requires a module object");
return -1;
}
w = PyModule_GetDict(v); /* TBD: can this not fail ? */
@@ -3068,12 +3082,17 @@
!PyCode_Check(prog) &&
!PyFile_Check(prog)) {
PyErr_SetString(PyExc_TypeError,
- "exec 1st arg must be string, code or file object");
+ "exec: arg 1 must be a string, file, or code object");
return -1;
}
- if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
+ if (!PyDict_Check(globals)) {
PyErr_SetString(PyExc_TypeError,
- "exec 2nd/3rd args must be dict or None");
+ "exec: arg 2 must be a dictionary or None");
+ return -1;
+ }
+ if (!PyDict_Check(locals)) {
+ PyErr_SetString(PyExc_TypeError,
+ "exec: arg 3 must be a dictionary or None");
return -1;
}
if (PyDict_GetItemString(globals, "__builtins__") == NULL)
diff --git a/Python/errors.c b/Python/errors.c
index 28dfbbe..3053e00 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -224,7 +224,7 @@
PyErr_BadArgument(void)
{
PyErr_SetString(PyExc_TypeError,
- "illegal argument type for built-in operation");
+ "bad argument type for built-in operation");
return 0;
}
diff --git a/Python/exceptions.c b/Python/exceptions.c
index 6d6291c..a95817c 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -188,7 +188,7 @@
/* Watch out for being called to early in the bootstrapping process */
if (PyExc_TypeError) {
PyErr_SetString(PyExc_TypeError,
- "unbound method must be called with class instance 1st argument");
+ "unbound method must be called with instance as first argument");
}
return NULL;
}