]> granicus.if.org Git - python/commitdiff
Fix seekable() in BZ2File and LZMAFile to check whether the underlying file supports...
authorNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 11 Feb 2012 23:51:38 +0000 (01:51 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Sat, 11 Feb 2012 23:51:38 +0000 (01:51 +0200)
Lib/bz2.py
Lib/lzma.py
Lib/test/test_bz2.py
Lib/test/test_lzma.py

index 7e1a7e29c663144d8feccc5c5622748da6215e0d..51b9ac438856aae4bba0f184a1226b0e371408aa 100644 (file)
@@ -138,7 +138,7 @@ class BZ2File(io.BufferedIOBase):
 
     def seekable(self):
         """Return whether the file supports seeking."""
-        return self.readable()
+        return self.readable() and self._fp.seekable()
 
     def readable(self):
         """Return whether the file was opened for reading."""
@@ -165,9 +165,12 @@ class BZ2File(io.BufferedIOBase):
             raise io.UnsupportedOperation("File not open for writing")
 
     def _check_can_seek(self):
-        if not self.seekable():
+        if not self.readable():
             raise io.UnsupportedOperation("Seeking is only supported "
                                           "on files open for reading")
+        if not self._fp.seekable():
+            raise io.UnsupportedOperation("The underlying file object "
+                                          "does not support seeking")
 
     # Fill the readahead buffer if it is empty. Returns False on EOF.
     def _fill_buffer(self):
index 780c666eebf9be9dc0793b9d99cf931f0320e627..3786993ccff0f80cc89043b112b66126c6ff6ac2 100644 (file)
@@ -165,7 +165,7 @@ class LZMAFile(io.BufferedIOBase):
 
     def seekable(self):
         """Return whether the file supports seeking."""
-        return self.readable()
+        return self.readable() and self._fp.seekable()
 
     def readable(self):
         """Return whether the file was opened for reading."""
@@ -192,9 +192,12 @@ class LZMAFile(io.BufferedIOBase):
             raise io.UnsupportedOperation("File not open for writing")
 
     def _check_can_seek(self):
-        if not self.seekable():
+        if not self.readable():
             raise io.UnsupportedOperation("Seeking is only supported "
                                           "on files open for reading")
+        if not self._fp.seekable():
+            raise io.UnsupportedOperation("The underlying file object "
+                                          "does not support seeking")
 
     # Fill the readahead buffer if it is empty. Returns False on EOF.
     def _fill_buffer(self):
index 0f8d14910f43c1e65abe6c0ef503be6a0edc60ea..cc416ed301d1d67b4771c7c9014cd91cde4a6985 100644 (file)
@@ -372,6 +372,15 @@ class BZ2FileTest(BaseTest):
             bz2f.close()
         self.assertRaises(ValueError, bz2f.seekable)
 
+        src = BytesIO(self.DATA)
+        src.seekable = lambda: False
+        bz2f = BZ2File(fileobj=src)
+        try:
+            self.assertFalse(bz2f.seekable())
+        finally:
+            bz2f.close()
+        self.assertRaises(ValueError, bz2f.seekable)
+
     def testReadable(self):
         bz2f = BZ2File(fileobj=BytesIO(self.DATA))
         try:
index 8d3df92aa9a76e2f4b1f8f295b1adf7bdfe818db..ffde5574adbd49c930d382c1a2019e71756d2643 100644 (file)
@@ -525,6 +525,15 @@ class FileTestCase(unittest.TestCase):
             f.close()
         self.assertRaises(ValueError, f.seekable)
 
+        src = BytesIO(COMPRESSED_XZ)
+        src.seekable = lambda: False
+        f = LZMAFile(fileobj=src)
+        try:
+            self.assertFalse(f.seekable())
+        finally:
+            f.close()
+        self.assertRaises(ValueError, f.seekable)
+
     def test_readable(self):
         f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ))
         try: