[New-bugs-announce] [issue21677] Exception context set to string by BufferedWriter.close()

Martin Panter report at bugs.python.org
Fri Jun 6 08:49:21 CEST 2014


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 <report at bugs.python.org>
<http://bugs.python.org/issue21677>
_______________________________________


More information about the New-bugs-announce mailing list