classification
Title: io.BufferedReader.writabe is False, but io.BufferedReader.truncate does not raise OSError
Type: Stage:
Components: IO Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, steverpalmer
Priority: normal Keywords:

Created on 2019-02-09 13:36 by steverpalmer, last changed 2019-02-09 13:38 by steverpalmer.

Messages (1)
msg335132 - (view) Author: Steve Palmer (steverpalmer) * Date: 2019-02-09 13:36
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.
History
Date User Action Args
2019-02-09 13:38:09steverpalmersetnosy: + martin.panter
2019-02-09 13:36:22steverpalmercreate