[issue35950] io.BufferedReader.writabe is False, but io.BufferedReader.truncate does not raise OSError

Steve Palmer report at bugs.python.org
Sat Feb 9 08:36:24 EST 2019


New submission from Steve Palmer <steve at srpalmer.me.uk>:

An io.BUfferedReader object has an (inherited) writable method that returns False.  io.IOBase states in the description of the writable method that "If False, write() and truncate() will raise OSError."

However, if the BufferedReader object is constructed from a writabe io.RawIOBase object, then the truncate does not raise the exception.

>>> import io
>>> import tempfile
>>> rf = tempfile.TemporaryFile(buffering=0)
>>> rf
<_io.FileIO name=3 mode='rb+' closefd=True>
>>> bf = io.BufferedReader(rf)
>>> bf.writable()
False
>>> bf.truncate(0)
0

Looking at _pyio.py file, the truncate method in the  _BufferedIOMixin wrapper class delegates the truncation to it's raw attribute.  If the raw element permits the truncation, then it will proceed regardless of the writable state of the wrapper class.  I'd suggest that modifying the truncate method in _BufferedIOMixin to raise OSError (or Unsupported) if not self.writable() could fix this problem.

----------
components: IO
messages: 335132
nosy: steverpalmer
priority: normal
severity: normal
status: open
title: io.BufferedReader.writabe is False, but io.BufferedReader.truncate does not raise OSError
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35950>
_______________________________________


More information about the Python-bugs-list mailing list