diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2020-02-21 17:57:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-21 17:57:26 (GMT) |
commit | fd5116c0e77aec05f67fb24f10562ac920648035 (patch) | |
tree | e980fc8e6aa1ed8d62312fb17dbf01935c1e6e04 /Lib | |
parent | d4d17fd2cf69e7c8f4cd03fbf2d575370945b952 (diff) | |
download | cpython-fd5116c0e77aec05f67fb24f10562ac920648035.zip cpython-fd5116c0e77aec05f67fb24f10562ac920648035.tar.gz cpython-fd5116c0e77aec05f67fb24f10562ac920648035.tar.bz2 |
bpo-35950: Raise UnsupportedOperation in BufferedReader.truncate() (GH-18586)
The truncate() method of io.BufferedReader() should raise
UnsupportedOperation when it is called on a read-only
io.BufferedReader() instance.
https://bugs.python.org/issue35950
Automerge-Triggered-By: @methane
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 3 | ||||
-rw-r--r-- | Lib/test/test_io.py | 11 |
2 files changed, 14 insertions, 0 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 4c24146..8eaa114 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -792,6 +792,9 @@ class _BufferedIOMixin(BufferedIOBase): return pos def truncate(self, pos=None): + self._checkClosed() + self._checkWritable() + # Flush the stream. We're mixing buffered I/O with lower-level I/O, # and a flush may be necessary to synch both views of the current # file state. diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 0bfa4d2..c27dfd9 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1528,6 +1528,13 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): self.assertRaises(ValueError, b.peek) self.assertRaises(ValueError, b.read1, 1) + def test_truncate_on_read_only(self): + rawio = self.MockFileIO(b"abc") + bufio = self.tp(rawio) + self.assertFalse(bufio.writable()) + self.assertRaises(self.UnsupportedOperation, bufio.truncate) + self.assertRaises(self.UnsupportedOperation, bufio.truncate, 0) + class CBufferedReaderTest(BufferedReaderTest, SizeofTest): tp = io.BufferedReader @@ -2372,6 +2379,10 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest): # You can't construct a BufferedRandom over a non-seekable stream. test_unseekable = None + # writable() returns True, so there's no point to test it over + # a writable stream. + test_truncate_on_read_only = None + class CBufferedRandomTest(BufferedRandomTest, SizeofTest): tp = io.BufferedRandom |