diff -r bb67b810aac1 Lib/test/test_extcall.py --- a/Lib/test/test_extcall.py Mon Feb 23 07:56:13 2015 -0800 +++ b/Lib/test/test_extcall.py Tue Feb 24 04:26:45 2015 +0000 @@ -92,7 +92,7 @@ >>> g(*Nothing()) Traceback (most recent call last): ... - TypeError: g() argument after * must be a sequence, not Nothing + TypeError: g() argument after * must be an iterable, not Nothing >>> class Nothing: ... def __len__(self): return 5 @@ -101,7 +101,7 @@ >>> g(*Nothing()) Traceback (most recent call last): ... - TypeError: g() argument after * must be a sequence, not Nothing + TypeError: g() argument after * must be an iterable, not Nothing >>> class Nothing(): ... def __len__(self): return 5 @@ -127,6 +127,45 @@ >>> g(*Nothing()) 0 (1, 2, 3) {} +Check for issue #4806: Does a TypeError in a generator get propagated with the +right error message? (Also check with other iterables.) + + >>> def broken(): raise TypeError("myerror") + ... + + >>> g(*(broken() for i in range(1))) + Traceback (most recent call last): + ... + TypeError: myerror + + >>> class BrokenIterable1: + ... def __iter__(self): + ... raise TypeError('myerror') + ... + >>> g(*BrokenIterable1()) + Traceback (most recent call last): + ... + TypeError: myerror + + >>> class BrokenIterable2: + ... def __iter__(self): + ... yield 0 + ... raise TypeError('myerror') + ... + >>> g(*BrokenIterable2()) + Traceback (most recent call last): + ... + TypeError: myerror + + >>> class BrokenSequence: + ... def __getitem__(self, idx): + ... raise TypeError('myerror') + ... + >>> f(*BrokenSequence()) + Traceback (most recent call last): + ... + TypeError: myerror + Make sure that the function doesn't stomp the dictionary >>> d = {'a': 1, 'b': 2, 'c': 3} @@ -166,17 +205,17 @@ >>> h(*h) Traceback (most recent call last): ... - TypeError: h() argument after * must be a sequence, not function + TypeError: h() argument after * must be an iterable, not function >>> dir(*h) Traceback (most recent call last): ... - TypeError: dir() argument after * must be a sequence, not function + TypeError: dir() argument after * must be an iterable, not function >>> None(*h) Traceback (most recent call last): ... - TypeError: NoneType object argument after * must be a sequence, \ + TypeError: NoneType object argument after * must be an iterable, \ not function >>> h(**h) diff -r bb67b810aac1 Python/ceval.c --- a/Python/ceval.c Mon Feb 23 07:56:13 2015 -0800 +++ b/Python/ceval.c Tue Feb 24 04:26:45 2015 +0000 @@ -4550,16 +4550,18 @@ stararg = EXT_POP(*pp_stack); if (!PyTuple_Check(stararg)) { PyObject *t = NULL; + if (Py_TYPE(stararg)->tp_iter == NULL && + !PySequence_Check(stararg)) { + PyErr_Format(PyExc_TypeError, + "%.200s%.200s argument after * " + "must be an iterable, not %.200s", + PyEval_GetFuncName(func), + PyEval_GetFuncDesc(func), + stararg->ob_type->tp_name); + goto ext_call_fail; + } t = PySequence_Tuple(stararg); if (t == NULL) { - if (PyErr_ExceptionMatches(PyExc_TypeError)) { - PyErr_Format(PyExc_TypeError, - "%.200s%.200s argument after * " - "must be a sequence, not %.200s", - PyEval_GetFuncName(func), - PyEval_GetFuncDesc(func), - stararg->ob_type->tp_name); - } goto ext_call_fail; } Py_DECREF(stararg);