Message 260673 - Python tracker (original) (raw)

When passing a class implementing the dunder iter that raises to csv.writer.writerows it hides the original exception and raises a TypeError instead.

In the raised TypeError the context and cause both are None.

For example:

class X: ... def iter(self): ... raise RuntimeError("I'm hidden") ... x = X() list(x) Traceback (most recent call last): File "", line 1, in File "", line 3, in iter RuntimeError: I'm hidden import csv csv.writer(open('foo.csv', 'w', newline='')).writerows(x) Traceback (most recent call last): File "", line 1, in TypeError: writerows() argument must be iterable try: ... csv.writer(open('foo.csv', 'w', newline='')).writerows(x) ... except TypeError as e: ... e_ = e e_.context is None True e_.cause is None True

It would seem that for reasons unknown to me either PyObject_GetIter loses the exception context or the call to PyErr_SetString in csv_writerows hides it.