Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf009f0

Browse files
committed
Issue #13087: BufferedReader.seek() now always raises UnsupportedOperation
if the underlying raw stream is unseekable, even if the seek could be satisfied using the internal buffer. Patch by John OConnor.
2 parents 5b99df6 + 0fc80c0 commit bf009f0

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/test/test_io.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,14 @@ def f():
928928
finally:
929929
support.unlink(support.TESTFN)
930930

931+
def test_unseekable(self):
932+
bufio = self.tp(self.MockUnseekableIO(b"A" * 10))
933+
self.assertRaises(self.UnsupportedOperation, bufio.tell)
934+
self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
935+
bufio.read(1)
936+
self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
937+
self.assertRaises(self.UnsupportedOperation, bufio.tell)
938+
931939
def test_misbehaved_io(self):
932940
rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg"))
933941
bufio = self.tp(rawio)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ Core and Builtins
294294
Library
295295
-------
296296

297+
- Issue #13087: BufferedReader.seek() now always raises UnsupportedOperation
298+
if the underlying raw stream is unseekable, even if the seek could be
299+
satisfied using the internal buffer. Patch by John O'Connor.
300+
297301
- Issue #7689: Allow pickling of dynamically created classes when their
298302
metaclass is registered with copyreg. Patch by Nicolas M. Thiéry and Craig
299303
Citro.

Modules/_io/bufferedio.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,9 @@ buffered_seek(buffered *self, PyObject *args)
11551155

11561156
CHECK_CLOSED(self, "seek of closed file")
11571157

1158+
if (_PyIOBase_check_seekable(self->raw, Py_True) == NULL)
1159+
return NULL;
1160+
11581161
target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
11591162
if (target == -1 && PyErr_Occurred())
11601163
return NULL;

0 commit comments

Comments
 (0)