Skip to content

Commit 4f8e7b5

Browse files
authored
pythongh-136327: Fix inconsistent TypeError messages regarding invalid values after * and ** (python#136395)
1 parent b3c713a commit 4f8e7b5

File tree

4 files changed

+23
-36
lines changed

4 files changed

+23
-36
lines changed

Lib/test/pickletester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ def test_bad_newobj_ex_args(self):
18821882
with self.assertRaises(TypeError) as cm:
18831883
self.dumps(obj, proto)
18841884
self.assertEqual(str(cm.exception),
1885-
'functools.partial() argument after ** must be a mapping, not list')
1885+
'Value after ** must be a mapping, not list')
18861886
self.assertEqual(cm.exception.__notes__, [
18871887
'when serializing test.pickletester.REX object'])
18881888
else:

Lib/test/test_extcall.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
>>> g(*Nothing())
138138
Traceback (most recent call last):
139139
...
140-
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
140+
TypeError: Value after * must be an iterable, not Nothing
141141
142142
>>> class Nothing:
143143
... def __len__(self): return 5
@@ -146,7 +146,7 @@
146146
>>> g(*Nothing())
147147
Traceback (most recent call last):
148148
...
149-
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
149+
TypeError: Value after * must be an iterable, not Nothing
150150
151151
>>> class Nothing():
152152
... def __len__(self): return 5
@@ -266,7 +266,7 @@
266266
>>> h(*h)
267267
Traceback (most recent call last):
268268
...
269-
TypeError: test.test_extcall.h() argument after * must be an iterable, not function
269+
TypeError: Value after * must be an iterable, not function
270270
271271
>>> h(1, *h)
272272
Traceback (most recent call last):
@@ -281,55 +281,53 @@
281281
>>> dir(*h)
282282
Traceback (most recent call last):
283283
...
284-
TypeError: dir() argument after * must be an iterable, not function
284+
TypeError: Value after * must be an iterable, not function
285285
286286
>>> nothing = None
287287
>>> nothing(*h)
288288
Traceback (most recent call last):
289289
...
290-
TypeError: None argument after * must be an iterable, \
291-
not function
290+
TypeError: Value after * must be an iterable, not function
292291
293292
>>> h(**h)
294293
Traceback (most recent call last):
295294
...
296-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
295+
TypeError: Value after ** must be a mapping, not function
297296
298297
>>> h(**[])
299298
Traceback (most recent call last):
300299
...
301-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
300+
TypeError: Value after ** must be a mapping, not list
302301
303302
>>> h(a=1, **h)
304303
Traceback (most recent call last):
305304
...
306-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
305+
TypeError: Value after ** must be a mapping, not function
307306
308307
>>> h(a=1, **[])
309308
Traceback (most recent call last):
310309
...
311-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
310+
TypeError: Value after ** must be a mapping, not list
312311
313312
>>> h(**{'a': 1}, **h)
314313
Traceback (most recent call last):
315314
...
316-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
315+
TypeError: Value after ** must be a mapping, not function
317316
318317
>>> h(**{'a': 1}, **[])
319318
Traceback (most recent call last):
320319
...
321-
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
320+
TypeError: Value after ** must be a mapping, not list
322321
323322
>>> dir(**h)
324323
Traceback (most recent call last):
325324
...
326-
TypeError: dir() argument after ** must be a mapping, not function
325+
TypeError: Value after ** must be a mapping, not function
327326
328327
>>> nothing(**h)
329328
Traceback (most recent call last):
330329
...
331-
TypeError: None argument after ** must be a mapping, \
332-
not function
330+
TypeError: Value after ** must be a mapping, not function
333331
334332
>>> dir(b=1, **{'b': 1})
335333
Traceback (most recent call last):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Errors when calling functions with invalid values after ``*`` and ``**`` now do not
2+
include the function name. Patch by Ilia Solin.

Python/ceval.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,17 +3272,9 @@ int
32723272
_Py_Check_ArgsIterable(PyThreadState *tstate, PyObject *func, PyObject *args)
32733273
{
32743274
if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
3275-
/* _Py_Check_ArgsIterable() may be called with a live exception:
3276-
* clear it to prevent calling _PyObject_FunctionStr() with an
3277-
* exception set. */
3278-
_PyErr_Clear(tstate);
3279-
PyObject *funcstr = _PyObject_FunctionStr(func);
3280-
if (funcstr != NULL) {
3281-
_PyErr_Format(tstate, PyExc_TypeError,
3282-
"%U argument after * must be an iterable, not %.200s",
3283-
funcstr, Py_TYPE(args)->tp_name);
3284-
Py_DECREF(funcstr);
3285-
}
3275+
_PyErr_Format(tstate, PyExc_TypeError,
3276+
"Value after * must be an iterable, not %.200s",
3277+
Py_TYPE(args)->tp_name);
32863278
return -1;
32873279
}
32883280
return 0;
@@ -3298,15 +3290,10 @@ _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwarg
32983290
* is not a mapping.
32993291
*/
33003292
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3301-
_PyErr_Clear(tstate);
3302-
PyObject *funcstr = _PyObject_FunctionStr(func);
3303-
if (funcstr != NULL) {
3304-
_PyErr_Format(
3305-
tstate, PyExc_TypeError,
3306-
"%U argument after ** must be a mapping, not %.200s",
3307-
funcstr, Py_TYPE(kwargs)->tp_name);
3308-
Py_DECREF(funcstr);
3309-
}
3293+
_PyErr_Format(
3294+
tstate, PyExc_TypeError,
3295+
"Value after ** must be a mapping, not %.200s",
3296+
Py_TYPE(kwargs)->tp_name);
33103297
}
33113298
else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
33123299
PyObject *exc = _PyErr_GetRaisedException(tstate);

0 commit comments

Comments
 (0)