Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c862cf4

Browse files
committed
clearer error messages for apply() and "no locals"
1 parent b4ed8c4 commit c862cf4

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

Python/bltinmodule.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ builtin_apply(PyObject *self, PyObject *args)
7070
if (alist != NULL) {
7171
if (!PyTuple_Check(alist)) {
7272
if (!PySequence_Check(alist)) {
73-
PyErr_SetString(PyExc_TypeError,
74-
"apply() arg 2 must be a sequence");
73+
PyErr_Format(PyExc_TypeError,
74+
"apply() arg 2 expect sequence, found %s",
75+
alist->ob_type->tp_name);
7576
return NULL;
7677
}
7778
t = PySequence_Tuple(alist);
@@ -81,8 +82,9 @@ builtin_apply(PyObject *self, PyObject *args)
8182
}
8283
}
8384
if (kwdict != NULL && !PyDict_Check(kwdict)) {
84-
PyErr_SetString(PyExc_TypeError,
85-
"apply() arg 3 must be a dictionary");
85+
PyErr_Format(PyExc_TypeError,
86+
"apply() arg 3 expected dictionary, found %s",
87+
kwdict->ob_type->tp_name);
8688
goto finally;
8789
}
8890
retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);

Python/ceval.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <ctype.h>
2222

23+
#define REPR(O) PyString_AS_STRING(PyObject_Repr(O))
24+
2325
/* Turn this on if your compiler chokes on the big switch: */
2426
/* #define CASE_TOO_BIG 1 */
2527

@@ -1438,8 +1440,9 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
14381440
w = GETNAMEV(oparg);
14391441
v = POP();
14401442
if ((x = f->f_locals) == NULL) {
1441-
PyErr_SetString(PyExc_SystemError,
1442-
"no locals");
1443+
PyErr_Format(PyExc_SystemError,
1444+
"no locals found when storing %s",
1445+
REPR(w));
14431446
break;
14441447
}
14451448
err = PyDict_SetItem(x, w, v);
@@ -1449,8 +1452,9 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
14491452
case DELETE_NAME:
14501453
w = GETNAMEV(oparg);
14511454
if ((x = f->f_locals) == NULL) {
1452-
PyErr_SetString(PyExc_SystemError,
1453-
"no locals");
1455+
PyErr_Format(PyExc_SystemError,
1456+
"no locals when deleting %s",
1457+
REPR(w));
14541458
break;
14551459
}
14561460
if ((err = PyDict_DelItem(x, w)) != 0)
@@ -1543,8 +1547,9 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
15431547
case LOAD_NAME:
15441548
w = GETNAMEV(oparg);
15451549
if ((x = f->f_locals) == NULL) {
1546-
PyErr_SetString(PyExc_SystemError,
1547-
"no locals");
1550+
PyErr_Format(PyExc_SystemError,
1551+
"no locals when loading %s",
1552+
REPR(w));
15481553
break;
15491554
}
15501555
x = PyDict_GetItem(x, w);
@@ -1716,7 +1721,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
17161721
PyFrame_FastToLocals(f);
17171722
if ((x = f->f_locals) == NULL) {
17181723
PyErr_SetString(PyExc_SystemError,
1719-
"no locals");
1724+
"no locals found during 'import *'");
17201725
break;
17211726
}
17221727
err = import_all_from(x, v);

0 commit comments

Comments
 (0)