Skip to content

bpo-35950: Raise UnsupportedOperation in BufferedReader.truncate() #18586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ def tell(self):
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.
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,13 @@ def test_read_on_closed(self):
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
Expand Down Expand Up @@ -2372,6 +2379,10 @@ def test_interleaved_readline_write(self):
# 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`io.UnsupportedOperation` in :meth:`io.BufferedReader.truncate`
when it is called on a read-only :class:`io.BufferedReader` instance.
14 changes: 9 additions & 5 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,15 +1315,19 @@ _io__Buffered_truncate_impl(buffered *self, PyObject *pos)
PyObject *res = NULL;

CHECK_INITIALIZED(self)
CHECK_CLOSED(self, "truncate of closed file")
if (!self->writable) {
return bufferediobase_unsupported("truncate");
}
if (!ENTER_BUFFERED(self))
return NULL;

if (self->writable) {
res = buffered_flush_and_rewind_unlocked(self);
if (res == NULL)
goto end;
Py_CLEAR(res);
res = buffered_flush_and_rewind_unlocked(self);
if (res == NULL) {
goto end;
}
Py_CLEAR(res);

res = PyObject_CallMethodOneArg(self->raw, _PyIO_str_truncate, pos);
if (res == NULL)
goto end;
Expand Down