]> granicus.if.org Git - python/commitdiff
Issue #13087: BufferedReader.seek() now always raises UnsupportedOperation
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 4 Oct 2011 10:26:20 +0000 (12:26 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 4 Oct 2011 10:26:20 +0000 (12:26 +0200)
if the underlying raw stream is unseekable, even if the seek could be
satisfied using the internal buffer.  Patch by John O'Connor.

Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c

index 0dc9d6dff42e404ee3db8f830b2a7d5098bdfcff..53cabbbd1598e11dd051830a132a43e9cd6098b1 100644 (file)
@@ -922,6 +922,14 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
         finally:
             support.unlink(support.TESTFN)
 
+    def test_unseekable(self):
+        bufio = self.tp(self.MockUnseekableIO(b"A" * 10))
+        self.assertRaises(self.UnsupportedOperation, bufio.tell)
+        self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
+        bufio.read(1)
+        self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
+        self.assertRaises(self.UnsupportedOperation, bufio.tell)
+
     def test_misbehaved_io(self):
         rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg"))
         bufio = self.tp(rawio)
index 2961776ae22089e87d66862386b02552d1495eed..dd98665c884d18e44f8cf0c3329d6d7b42d26f83 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,10 @@ Core and Builtins
 Library
 -------
 
+- 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 O'Connor.
+
 - Issue #7689: Allow pickling of dynamically created classes when their
   metaclass is registered with copyreg.  Patch by Nicolas M. ThiĆ©ry and Craig
   Citro.
index d6f0c9cc83507f6026e794515b7da84ed2d64b47..bd1aae5ab6c1d0f3b3a8fb9490560b13878e93e4 100644 (file)
@@ -1086,6 +1086,9 @@ buffered_seek(buffered *self, PyObject *args)
 
     CHECK_CLOSED(self, "seek of closed file")
 
+    if (_PyIOBase_check_seekable(self->raw, Py_True) == NULL)
+        return NULL;
+
     target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
     if (target == -1 && PyErr_Occurred())
         return NULL;