New submission from Martin Panter: I made a writer class whose write() and flush() methods (unintentionally) triggered exceptions. I wrapped this in a BufferedWriter. When close() is called, the resulting exception has a string object in its __context__ attribute. Although the original error was my fault, it created a confusing chain reaction of exception reports.
>>> from io import BufferedWriter, RawIOBase >>> import sys >>> >>> class BuggyWriter(RawIOBase): ... def writable(self): return True ... def write(self, b): in_write # Initial exception ... def flush(self): raise Exception("In flush()") ... >>> output = BufferedWriter(BuggyWriter()) >>> output.write(b"data") 4 >>> output.close() # Note the TypeError printed at the top TypeError: print_exception(): Exception expected for value, str found During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in flush Exception: In flush() >>> >>> sys.last_value Exception('In flush()',) >>> sys.last_value.__context__ # Should be exception, not string object "name 'in_write' is not defined" >>> >>> import traceback >>> traceback.print_exception(sys.last_type, sys.last_value, sys.last_traceback) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.4/traceback.py", line 169, in print_exception for line in _format_exception_iter(etype, value, tb, limit, chain): File "/usr/lib/python3.4/traceback.py", line 146, in _format_exception_iter for value, tb in values: File "/usr/lib/python3.4/traceback.py", line 138, in _iter_chain yield from it File "/usr/lib/python3.4/traceback.py", line 125, in _iter_chain context = exc.__context__ AttributeError: 'str' object has no attribute '__context__' ---------- components: IO messages: 219864 nosy: vadmium priority: normal severity: normal status: open title: Exception context set to string by BufferedWriter.close() type: behavior versions: Python 3.4 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue21677> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com