classification
Title: Function call with * and generator hide exception raised by generator.
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Function calls taking a generator as star argument can mask TypeErrors in the generator
View: 4806
Assigned To: Nosy List: daniel.urban, eric.araujo, falsetru, martin.panter, ncoghlan
Priority: normal Keywords:

Created on 2011-04-28 01:00 by falsetru, last changed 2012-02-04 02:06 by terry.reedy. This issue is now closed.

Messages (4)
msg134632 - (view) Author: Jeong-Min Lee (falsetru) Date: 2011-04-28 01:00
Expected "TypeError: cannot concatenate 'str' and 'int' objects" exception raised, but got following result.


>>> def g():
...     '1' + 0
...     yield 1, 2
...     yield 3, 4
...
>>> zip(*g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip() argument after * must be a sequence, not generator
>>> (lambda xs: 0)(*g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() argument after * must be a sequence, not generator
>>> list(*g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type object argument after * must be a sequence, not generator
>>> list(g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in g
TypeError: cannot concatenate 'str' and 'int' objects
msg134636 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-04-28 02:53
It's not just hiding the real error, it's also saying something that isn't true. If you remove the deliberate error from the generator definition, it is clear that generators are perfectly acceptable as *arg inputs:

>>> def g(): yield 1, 2
... 
>>> list(*g())
[1, 2]
msg134665 - (view) Author: Jeong-Min Lee (falsetru) Date: 2011-04-28 09:22
Some exceptions are reported correctly.


>>> def g():
...     1 / 0
...     yield 1, 2
...     yield 3, 4
... 
>>> zip(*g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in g
ZeroDivisionError: integer division or modulo by zero



>>> def g():
...     [][0]
...     yield 1, 2
...     yield 3, 4
... 
>>> zip(*g())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in g
IndexError: list index out of range
msg134712 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-04-28 18:52
This may be the same/similar as issue4806 (which has a patch).
History
Date User Action Args
2012-02-04 02:06:33terry.reedysetstatus: open -> closed
resolution: duplicate
superseder: Function calls taking a generator as star argument can mask TypeErrors in the generator
2012-01-12 04:41:00martin.pantersetnosy: + martin.panter
2011-04-28 18:52:53daniel.urbansetnosy: + daniel.urban
messages: + msg134712
2011-04-28 09:22:42falsetrusetmessages: + msg134665
2011-04-28 03:05:28eric.araujosetnosy: + eric.araujo
2011-04-28 02:53:22ncoghlansetnosy: + ncoghlan
messages: + msg134636
2011-04-28 01:00:58falsetrusetversions: + Python 3.2, - Python 3.3
2011-04-28 01:00:46falsetrucreate