mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Ka-Ping Yee <ping@lfw.org>:
Changes to error messages to increase consistency & clarity. This (mostly) closes SourceForge patch #101839.
This commit is contained in:
parent
bd6f4fba1b
commit
661ea26b3d
16 changed files with 186 additions and 144 deletions
|
@ -61,9 +61,9 @@ static void reset_exc_info(PyThreadState *);
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
}
|
||||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
/* 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 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
|||
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 @@ do_raise(PyObject *type, PyObject *value, PyObject *tb)
|
|||
}
|
||||
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 @@ static PyObject *
|
|||
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 @@ call_builtin(PyObject *func, PyObject *arg, PyObject *kw)
|
|||
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 @@ call_builtin(PyObject *func, PyObject *arg, PyObject *kw)
|
|||
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 @@ call_function(PyObject *func, PyObject *arg, PyObject *kw)
|
|||
}
|
||||
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 @@ _PyEval_SliceIndex(PyObject *v, int *pi)
|
|||
}
|
||||
} 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 @@ import_from(PyObject *v, PyObject *name)
|
|||
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 @@ import_all_from(PyObject *locals, PyObject *v)
|
|||
|
||||
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 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
|
|||
!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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue