This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients martin.panter
Date 2014-06-06.06:49:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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")
>>> 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/", line 169, in print_exception
    for line in _format_exception_iter(etype, value, tb, limit, chain):
  File "/usr/lib/python3.4/", line 146, in _format_exception_iter
    for value, tb in values:
  File "/usr/lib/python3.4/", line 138, in _iter_chain
    yield from it
  File "/usr/lib/python3.4/", line 125, in _iter_chain
    context = exc.__context__
AttributeError: 'str' object has no attribute '__context__'
Date User Action Args
2014-06-06 06:49:21martin.pantersetrecipients: + martin.panter
2014-06-06 06:49:21martin.pantersetmessageid: <>
2014-06-06 06:49:21martin.panterlinkissue21677 messages
2014-06-06 06:49:20martin.pantercreate