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

Skip to content

Commit 5665301

Browse files
Issue #28257: Improved error message when pass a non-mapping as a var-keyword
argument.
1 parent de0574b commit 5665301

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lib/test/test_extcall.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@
269269
...
270270
TypeError: h() argument after ** must be a mapping, not list
271271
272+
>>> h(**{'a': 1}, **h)
273+
Traceback (most recent call last):
274+
...
275+
TypeError: h() argument after ** must be a mapping, not function
276+
277+
>>> h(**{'a': 1}, **[])
278+
Traceback (most recent call last):
279+
...
280+
TypeError: h() argument after ** must be a mapping, not list
281+
272282
>>> dir(**h)
273283
Traceback (most recent call last):
274284
...

Python/ceval.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
26632663
PyObject *intersection = _PyDictView_Intersect(sum, arg);
26642664

26652665
if (intersection == NULL) {
2666-
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2666+
if (PyErr_ExceptionMatches(PyExc_AttributeError) ||
2667+
!PyMapping_Check(arg)) {
26672668
int function_location = (oparg>>8) & 0xff;
26682669
PyObject *func = (
26692670
PEEK(function_location + num_maps));
@@ -2707,9 +2708,21 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
27072708

27082709
if (PyDict_Update(sum, arg) < 0) {
27092710
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2710-
PyErr_Format(PyExc_TypeError,
2711-
"'%.200s' object is not a mapping",
2712-
arg->ob_type->tp_name);
2711+
if (with_call) {
2712+
int function_location = (oparg>>8) & 0xff;
2713+
PyObject *func = PEEK(function_location + num_maps);
2714+
PyErr_Format(PyExc_TypeError,
2715+
"%.200s%.200s argument after ** "
2716+
"must be a mapping, not %.200s",
2717+
PyEval_GetFuncName(func),
2718+
PyEval_GetFuncDesc(func),
2719+
arg->ob_type->tp_name);
2720+
}
2721+
else {
2722+
PyErr_Format(PyExc_TypeError,
2723+
"'%.200s' object is not a mapping",
2724+
arg->ob_type->tp_name);
2725+
}
27132726
}
27142727
Py_DECREF(sum);
27152728
goto error;

0 commit comments

Comments
 (0)