Skip to content

Commit 402ec96

Browse files
committed
Change format_kwargs_error to use _PyObject_FunctionStr as well
1 parent 21aee80 commit 402ec96

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

Lib/test/test_extcall.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252
>>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
5353
Traceback (most recent call last):
5454
...
55-
TypeError: f() got multiple values for keyword argument 'a'
55+
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
5656
>>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
5757
Traceback (most recent call last):
5858
...
59-
TypeError: f() got multiple values for keyword argument 'a'
59+
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
6060
>>> f(1, 2, a=3, **{'a': 4}, **{'a': 5})
6161
Traceback (most recent call last):
6262
...
63-
TypeError: f() got multiple values for keyword argument 'a'
63+
TypeError: test.test_extcall.f() got multiple values for keyword argument 'a'
6464
>>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
6565
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
6666
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
@@ -274,32 +274,32 @@
274274
>>> h(**h)
275275
Traceback (most recent call last):
276276
...
277-
TypeError: h() argument after ** must be a mapping, not function
277+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
278278
279279
>>> h(**[])
280280
Traceback (most recent call last):
281281
...
282-
TypeError: h() argument after ** must be a mapping, not list
282+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
283283
284284
>>> h(a=1, **h)
285285
Traceback (most recent call last):
286286
...
287-
TypeError: h() argument after ** must be a mapping, not function
287+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
288288
289289
>>> h(a=1, **[])
290290
Traceback (most recent call last):
291291
...
292-
TypeError: h() argument after ** must be a mapping, not list
292+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
293293
294294
>>> h(**{'a': 1}, **h)
295295
Traceback (most recent call last):
296296
...
297-
TypeError: h() argument after ** must be a mapping, not function
297+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
298298
299299
>>> h(**{'a': 1}, **[])
300300
Traceback (most recent call last):
301301
...
302-
TypeError: h() argument after ** must be a mapping, not list
302+
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
303303
304304
>>> dir(**h)
305305
Traceback (most recent call last):
@@ -309,7 +309,7 @@
309309
>>> nothing(**h)
310310
Traceback (most recent call last):
311311
...
312-
TypeError: NoneType object argument after ** must be a mapping, \
312+
TypeError: None argument after ** must be a mapping, \
313313
not function
314314
315315
>>> dir(b=1, **{'b': 1})
@@ -351,17 +351,17 @@
351351
>>> g(**MultiDict([('x', 1), ('x', 2)]))
352352
Traceback (most recent call last):
353353
...
354-
TypeError: g() got multiple values for keyword argument 'x'
354+
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
355355
356356
>>> g(a=3, **MultiDict([('x', 1), ('x', 2)]))
357357
Traceback (most recent call last):
358358
...
359-
TypeError: g() got multiple values for keyword argument 'x'
359+
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
360360
361361
>>> g(**MultiDict([('a', 3)]), **MultiDict([('x', 1), ('x', 2)]))
362362
Traceback (most recent call last):
363363
...
364-
TypeError: g() got multiple values for keyword argument 'x'
364+
TypeError: test.test_extcall.g() got multiple values for keyword argument 'x'
365365
366366
Another helper function
367367

Lib/test/test_unpack_ex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,27 @@
236236
>>> f(x=5, **{'x': 3}, y=2)
237237
Traceback (most recent call last):
238238
...
239-
TypeError: f() got multiple values for keyword argument 'x'
239+
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
240240
241241
>>> f(**{'x': 3}, x=5, y=2)
242242
Traceback (most recent call last):
243243
...
244-
TypeError: f() got multiple values for keyword argument 'x'
244+
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
245245
246246
>>> f(**{'x': 3}, **{'x': 5}, y=2)
247247
Traceback (most recent call last):
248248
...
249-
TypeError: f() got multiple values for keyword argument 'x'
249+
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
250250
251251
>>> f(x=5, **{'x': 3}, **{'x': 2})
252252
Traceback (most recent call last):
253253
...
254-
TypeError: f() got multiple values for keyword argument 'x'
254+
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument 'x'
255255
256256
>>> f(**{1: 3}, **{1: 5})
257257
Traceback (most recent call last):
258258
...
259-
TypeError: f() got multiple values for keyword argument '1'
259+
TypeError: test.test_unpack_ex.f() got multiple values for keyword argument '1'
260260
261261
Unpacking non-sequence
262262

Python/ceval.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5367,24 +5367,30 @@ format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
53675367
* is not a mapping.
53685368
*/
53695369
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5370-
_PyErr_Format(tstate, PyExc_TypeError,
5371-
"%.200s%.200s argument after ** "
5372-
"must be a mapping, not %.200s",
5373-
PyEval_GetFuncName(func),
5374-
PyEval_GetFuncDesc(func),
5375-
kwargs->ob_type->tp_name);
5370+
PyErr_Clear();
5371+
PyObject *funcstr = _PyObject_FunctionStr(func);
5372+
if (funcstr != NULL) {
5373+
_PyErr_Format(
5374+
tstate, PyExc_TypeError,
5375+
"%U argument after ** must be a mapping, not %.200s",
5376+
funcstr, Py_TYPE(kwargs)->tp_name);
5377+
Py_DECREF(funcstr);
5378+
}
53765379
}
53775380
else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
53785381
PyObject *exc, *val, *tb;
53795382
_PyErr_Fetch(tstate, &exc, &val, &tb);
53805383
if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5381-
PyObject *key = PyTuple_GET_ITEM(val, 0);
5382-
_PyErr_Format(tstate, PyExc_TypeError,
5383-
"%.200s%.200s got multiple "
5384-
"values for keyword argument '%S'",
5385-
PyEval_GetFuncName(func),
5386-
PyEval_GetFuncDesc(func),
5387-
key);
5384+
PyErr_Clear();
5385+
PyObject *funcstr = _PyObject_FunctionStr(func);
5386+
if (funcstr != NULL) {
5387+
PyObject *key = PyTuple_GET_ITEM(val, 0);
5388+
_PyErr_Format(
5389+
tstate, PyExc_TypeError,
5390+
"%U got multiple values for keyword argument '%S'",
5391+
funcstr, key);
5392+
Py_DECREF(funcstr);
5393+
}
53885394
Py_XDECREF(exc);
53895395
Py_XDECREF(val);
53905396
Py_XDECREF(tb);

0 commit comments

Comments
 (0)