diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-02-11 23:51:38 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-02-11 23:51:38 (GMT) |
commit | ae557d767fa0862188a17914eb07b74088ed4d29 (patch) | |
tree | acd9d5cb6fd49f4cb40399eec3b866d10e8f855a | |
parent | d7e5c6ed7f8402a3ce2e6acb0cc6253456878773 (diff) | |
download | cpython-ae557d767fa0862188a17914eb07b74088ed4d29.zip cpython-ae557d767fa0862188a17914eb07b74088ed4d29.tar.gz cpython-ae557d767fa0862188a17914eb07b74088ed4d29.tar.bz2 |
Fix seekable() in BZ2File and LZMAFile to check whether the underlying file supports seek().
-rw-r--r-- | Lib/bz2.py | 7 | ||||
-rw-r--r-- | Lib/lzma.py | 7 | ||||
-rw-r--r-- | Lib/test/test_bz2.py | 9 | ||||
-rw-r--r-- | Lib/test/test_lzma.py | 9 |
4 files changed, 28 insertions, 4 deletions
@@ -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): diff --git a/Lib/lzma.py b/Lib/lzma.py index 780c666..3786993 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -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): diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 0f8d149..cc416ed 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -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: diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index 8d3df92..ffde557 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -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: |