# HG changeset patch # User Martin Panter # Date 1454221856 0 # Sun Jan 31 06:30:56 2016 +0000 # Branch 2.7 # Node ID 4f5274e34718a9c6fe4e020f32a4ddc495fbf1ea # Parent 296fb7c10a7d421b33ca2ba7f44ea9f37959770e Issue #4806: Avoid masking TypeError when *-unpacking a generator Based on patch by Hagen Fürstenau. diff -r 296fb7c10a7d Lib/test/test_extcall.py --- a/Lib/test/test_extcall.py Fri Jan 29 19:06:00 2016 -0600 +++ b/Lib/test/test_extcall.py Sun Jan 31 11:19:40 2016 +0000 @@ -93,7 +93,7 @@ >>> g(*Nothing()) Traceback (most recent call last): ... - TypeError: g() argument after * must be a sequence, not instance + TypeError: g() argument after * must be an iterable, not instance >>> class Nothing: ... def __len__(self): return 5 @@ -102,7 +102,7 @@ >>> g(*Nothing()) Traceback (most recent call last): ... - TypeError: g() argument after * must be a sequence, not instance + TypeError: g() argument after * must be an iterable, not instance >>> class Nothing(): ... def __len__(self): return 5 @@ -128,6 +128,17 @@ >>> g(*Nothing()) 0 (1, 2, 3) {} +Check for issue #4806: Does a TypeError in a generator get propagated with the +right error message? + + >>> def broken(): raise TypeError("myerror") + ... + + >>> g(*(broken() for i in range(1))) + Traceback (most recent call last): + ... + TypeError: myerror + Make sure that the function doesn't stomp the dictionary >>> d = {'a': 1, 'b': 2, 'c': 3} @@ -167,17 +178,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 296fb7c10a7d Misc/NEWS --- a/Misc/NEWS Fri Jan 29 19:06:00 2016 -0600 +++ b/Misc/NEWS Sun Jan 31 11:19:40 2016 +0000 @@ -10,6 +10,10 @@ Core and Builtins ----------------- +- Issue #4806: Avoid masking the original TypeError exception when using star + (*) unpacking and the exception was raised from a generator. Based on + patch by Hagen Fürstenau. + - Issue #25843: When compiling code, don't merge constants if they are equal but have a different types. For example, ``f1, f2 = lambda: 1, lambda: 1.0`` is now correctly compiled to two different functions: ``f1()`` returns ``1`` diff -r 296fb7c10a7d Python/ceval.c --- a/Python/ceval.c Fri Jan 29 19:06:00 2016 -0600 +++ b/Python/ceval.c Sun Jan 31 11:19:40 2016 +0000 @@ -4615,10 +4615,12 @@ PyObject *t = NULL; t = PySequence_Tuple(stararg); if (t == NULL) { - if (PyErr_ExceptionMatches(PyExc_TypeError)) { + if (PyErr_ExceptionMatches(PyExc_TypeError) && + /* Don't mask TypeError raised from a generator */ + !PyGen_Check(stararg)) { PyErr_Format(PyExc_TypeError, "%.200s%.200s argument after * " - "must be a sequence, not %200s", + "must be an iterable, not %200s", PyEval_GetFuncName(func), PyEval_GetFuncDesc(func), stararg->ob_type->tp_name);