Skip to content

Commit 946a0b6

Browse files
[3.6] bpo-31071: Avoid masking original TypeError in call with * unpacking (GH-2957) (#2991)
when other arguments are passed. (cherry picked from commit 25e4f77)
1 parent f08b2be commit 946a0b6

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

Lib/test/test_extcall.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@
163163
Traceback (most recent call last):
164164
...
165165
TypeError: myerror
166+
>>> g(*range(1), *(broken() for i in range(1)))
167+
Traceback (most recent call last):
168+
...
169+
TypeError: myerror
166170
167171
>>> class BrokenIterable1:
168172
... def __iter__(self):
@@ -172,6 +176,10 @@
172176
Traceback (most recent call last):
173177
...
174178
TypeError: myerror
179+
>>> g(*range(1), *BrokenIterable1())
180+
Traceback (most recent call last):
181+
...
182+
TypeError: myerror
175183
176184
>>> class BrokenIterable2:
177185
... def __iter__(self):
@@ -182,6 +190,10 @@
182190
Traceback (most recent call last):
183191
...
184192
TypeError: myerror
193+
>>> g(*range(1), *BrokenIterable2())
194+
Traceback (most recent call last):
195+
...
196+
TypeError: myerror
185197
186198
>>> class BrokenSequence:
187199
... def __getitem__(self, idx):
@@ -191,6 +203,10 @@
191203
Traceback (most recent call last):
192204
...
193205
TypeError: myerror
206+
>>> g(*range(1), *BrokenSequence())
207+
Traceback (most recent call last):
208+
...
209+
TypeError: myerror
194210
195211
Make sure that the function doesn't stomp the dictionary
196212
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid masking original TypeError in call with * unpacking when other
2+
arguments are passed.

Python/ceval.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ static void format_exc_unbound(PyCodeObject *co, int oparg);
6464
static PyObject * unicode_concatenate(PyObject *, PyObject *,
6565
PyFrameObject *, const _Py_CODEUNIT *);
6666
static PyObject * special_lookup(PyObject *, _Py_Identifier *);
67+
static int check_args_iterable(PyObject *func, PyObject *vararg);
68+
static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
6769

6870
#define NAME_ERROR_MSG \
6971
"name '%.200s' is not defined"
@@ -2578,14 +2580,9 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
25782580
none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
25792581
if (none_val == NULL) {
25802582
if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
2581-
PyErr_ExceptionMatches(PyExc_TypeError)) {
2582-
PyObject *func = PEEK(1 + oparg);
2583-
PyErr_Format(PyExc_TypeError,
2584-
"%.200s%.200s argument after * "
2585-
"must be an iterable, not %.200s",
2586-
PyEval_GetFuncName(func),
2587-
PyEval_GetFuncDesc(func),
2588-
PEEK(i)->ob_type->tp_name);
2583+
PyErr_ExceptionMatches(PyExc_TypeError))
2584+
{
2585+
check_args_iterable(PEEK(1 + oparg), PEEK(i));
25892586
}
25902587
Py_DECREF(sum);
25912588
goto error;
@@ -2798,12 +2795,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
27982795
if (_PyDict_MergeEx(sum, arg, 2) < 0) {
27992796
PyObject *func = PEEK(2 + oparg);
28002797
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2801-
PyErr_Format(PyExc_TypeError,
2802-
"%.200s%.200s argument after ** "
2803-
"must be a mapping, not %.200s",
2804-
PyEval_GetFuncName(func),
2805-
PyEval_GetFuncDesc(func),
2806-
arg->ob_type->tp_name);
2798+
format_kwargs_mapping_error(func, arg);
28072799
}
28082800
else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
28092801
PyObject *exc, *val, *tb;
@@ -3370,13 +3362,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
33703362
* is not a mapping.
33713363
*/
33723364
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3373-
func = SECOND();
3374-
PyErr_Format(PyExc_TypeError,
3375-
"%.200s%.200s argument after ** "
3376-
"must be a mapping, not %.200s",
3377-
PyEval_GetFuncName(func),
3378-
PyEval_GetFuncDesc(func),
3379-
kwargs->ob_type->tp_name);
3365+
format_kwargs_mapping_error(SECOND(), kwargs);
33803366
}
33813367
Py_DECREF(kwargs);
33823368
goto error;
@@ -3389,14 +3375,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
33893375
callargs = POP();
33903376
func = TOP();
33913377
if (!PyTuple_CheckExact(callargs)) {
3392-
if (Py_TYPE(callargs)->tp_iter == NULL &&
3393-
!PySequence_Check(callargs)) {
3394-
PyErr_Format(PyExc_TypeError,
3395-
"%.200s%.200s argument after * "
3396-
"must be an iterable, not %.200s",
3397-
PyEval_GetFuncName(func),
3398-
PyEval_GetFuncDesc(func),
3399-
callargs->ob_type->tp_name);
3378+
if (check_args_iterable(func, callargs) < 0) {
34003379
Py_DECREF(callargs);
34013380
goto error;
34023381
}
@@ -5351,6 +5330,32 @@ import_all_from(PyObject *locals, PyObject *v)
53515330
return err;
53525331
}
53535332

5333+
static int
5334+
check_args_iterable(PyObject *func, PyObject *args)
5335+
{
5336+
if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5337+
PyErr_Format(PyExc_TypeError,
5338+
"%.200s%.200s argument after * "
5339+
"must be an iterable, not %.200s",
5340+
PyEval_GetFuncName(func),
5341+
PyEval_GetFuncDesc(func),
5342+
args->ob_type->tp_name);
5343+
return -1;
5344+
}
5345+
return 0;
5346+
}
5347+
5348+
static void
5349+
format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5350+
{
5351+
PyErr_Format(PyExc_TypeError,
5352+
"%.200s%.200s argument after ** "
5353+
"must be a mapping, not %.200s",
5354+
PyEval_GetFuncName(func),
5355+
PyEval_GetFuncDesc(func),
5356+
kwargs->ob_type->tp_name);
5357+
}
5358+
53545359
static void
53555360
format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
53565361
{

0 commit comments

Comments
 (0)