Issue 11944: Function call with * and generator hide exception raised by generator. (original) (raw)
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 "", line 1, in TypeError: zip() argument after * must be a sequence, not generator (lambda xs: 0)(*g()) Traceback (most recent call last): File "", line 1, in TypeError: () argument after * must be a sequence, not generator list(*g()) Traceback (most recent call last): File "", line 1, in TypeError: type object argument after * must be a sequence, not generator list(g()) Traceback (most recent call last): File "", line 1, in File "", line 2, in g TypeError: cannot concatenate 'str' and 'int' objects
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]
Some exceptions are reported correctly.
def g(): ... 1 / 0 ... yield 1, 2 ... yield 3, 4 ... zip(*g()) Traceback (most recent call last): File "", line 1, in File "", 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 "", line 1, in File "", line 2, in g IndexError: list index out of range