New submission from Steve Palmer <st...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue35950>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to