classification
Title: io.BufferedReader.writabe is False, but io.BufferedReader.truncate does not raise OSError
Type: Stage: resolved
Components: IO Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, martin.panter, miss-islington, steverpalmer
Priority: normal Keywords: patch

Created on 2019-02-09 13:36 by steverpalmer, last changed 2020-02-22 07:19 by berker.peksag. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13689 closed berker.peksag, 2019-05-30 22:51
PR 18586 merged berker.peksag, 2020-02-21 00:57
Messages (2)
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.
msg362417 - (view) Author: miss-islington (miss-islington) Date: 2020-02-21 17:57
New changeset fd5116c0e77aec05f67fb24f10562ac920648035 by Berker Peksag in branch 'master':
bpo-35950: Raise UnsupportedOperation in BufferedReader.truncate() (GH-18586)
https://github.com/python/cpython/commit/fd5116c0e77aec05f67fb24f10562ac920648035
History
Date User Action Args
2020-02-22 07:19:05berker.peksagsetnosy: + berker.peksag
2020-02-21 18:01:25methanesetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.9, - Python 3.7
2020-02-21 17:57:33miss-islingtonsetnosy: + miss-islington
messages: + msg362417
2020-02-21 00:57:14berker.peksagsetpull_requests: + pull_request17956
2019-05-30 22:51:03berker.peksagsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13577
2019-02-09 13:38:09steverpalmersetnosy: + martin.panter
2019-02-09 13:36:22steverpalmercreate