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

Skip to content

Commit ae557d7

Browse files
committed
Fix seekable() in BZ2File and LZMAFile to check whether the underlying file supports seek().
1 parent d7e5c6e commit ae557d7

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

Lib/bz2.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def fileno(self):
138138

139139
def seekable(self):
140140
"""Return whether the file supports seeking."""
141-
return self.readable()
141+
return self.readable() and self._fp.seekable()
142142

143143
def readable(self):
144144
"""Return whether the file was opened for reading."""
@@ -165,9 +165,12 @@ def _check_can_write(self):
165165
raise io.UnsupportedOperation("File not open for writing")
166166

167167
def _check_can_seek(self):
168-
if not self.seekable():
168+
if not self.readable():
169169
raise io.UnsupportedOperation("Seeking is only supported "
170170
"on files open for reading")
171+
if not self._fp.seekable():
172+
raise io.UnsupportedOperation("The underlying file object "
173+
"does not support seeking")
171174

172175
# Fill the readahead buffer if it is empty. Returns False on EOF.
173176
def _fill_buffer(self):

Lib/lzma.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def fileno(self):
165165

166166
def seekable(self):
167167
"""Return whether the file supports seeking."""
168-
return self.readable()
168+
return self.readable() and self._fp.seekable()
169169

170170
def readable(self):
171171
"""Return whether the file was opened for reading."""
@@ -192,9 +192,12 @@ def _check_can_write(self):
192192
raise io.UnsupportedOperation("File not open for writing")
193193

194194
def _check_can_seek(self):
195-
if not self.seekable():
195+
if not self.readable():
196196
raise io.UnsupportedOperation("Seeking is only supported "
197197
"on files open for reading")
198+
if not self._fp.seekable():
199+
raise io.UnsupportedOperation("The underlying file object "
200+
"does not support seeking")
198201

199202
# Fill the readahead buffer if it is empty. Returns False on EOF.
200203
def _fill_buffer(self):

Lib/test/test_bz2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ def testSeekable(self):
372372
bz2f.close()
373373
self.assertRaises(ValueError, bz2f.seekable)
374374

375+
src = BytesIO(self.DATA)
376+
src.seekable = lambda: False
377+
bz2f = BZ2File(fileobj=src)
378+
try:
379+
self.assertFalse(bz2f.seekable())
380+
finally:
381+
bz2f.close()
382+
self.assertRaises(ValueError, bz2f.seekable)
383+
375384
def testReadable(self):
376385
bz2f = BZ2File(fileobj=BytesIO(self.DATA))
377386
try:

Lib/test/test_lzma.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ def test_seekable(self):
525525
f.close()
526526
self.assertRaises(ValueError, f.seekable)
527527

528+
src = BytesIO(COMPRESSED_XZ)
529+
src.seekable = lambda: False
530+
f = LZMAFile(fileobj=src)
531+
try:
532+
self.assertFalse(f.seekable())
533+
finally:
534+
f.close()
535+
self.assertRaises(ValueError, f.seekable)
536+
528537
def test_readable(self):
529538
f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ))
530539
try:

0 commit comments

Comments
 (0)