diff Python/ceval.c @ 15318:8aa947e1eee8 legacy-trunk

Ka-Ping Yee <ping@lfw.org>: Changes to error messages to increase consistency & clarity. This (mostly) closes SourceForge patch #101839.
author Fred Drake <fdrake@acm.org>
date Tue, 24 Oct 2000 19:57:45 +0000
parents e61e64d1b5ec
children 09952d6ed372
line wrap: on
line diff
--- a/Python/ceval.c	Tue Oct 24 17:16:32 2000 +0000
+++ b/Python/ceval.c	Tue Oct 24 19:57:45 2000 +0000
@@ -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)