FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-124293: Fix tar stream seekability checks by limadog9 · Pull Request #159854 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Lib/tarfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ def tell(self):
"""
return self.pos

def seekable(self):
return True

def seek(self, pos=0):
"""Set the stream's file pointer to pos. Negative seeking
is forbidden.
Expand Down
19 changes: 11 additions & 8 deletions Lib/test/test_tarfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -547,21 +547,15 @@ def test_extractfile_attrs(self):
self.assertEqual(fobj.mode, 'rb')
self.assertIs(fobj.readable(), True)
self.assertIs(fobj.writable(), False)
if self.is_stream:
self.assertRaises(AttributeError, fobj.seekable)
else:
self.assertIs(fobj.seekable(), True)
self.assertIs(fobj.seekable(), True)
self.assertIs(fobj.closed, False)
self.assertIs(fobj.closed, True)
self.assertEqual(fobj.name, 'ustar/regtype')
self.assertRaises(AttributeError, fobj.fileno)
self.assertEqual(fobj.mode, 'rb')
self.assertIs(fobj.readable(), True)
self.assertIs(fobj.writable(), False)
if self.is_stream:
self.assertRaises(AttributeError, fobj.seekable)
else:
self.assertIs(fobj.seekable(), True)
self.assertIs(fobj.seekable(), True)


class ReadSizeRecorder(io.BytesIO):
Expand Down Expand Up @@ -1003,6 +997,15 @@ class StreamReadTest(CommonReadTest, unittest.TestCase):
prefix="r|"
is_stream = True

def test_extractfile_seek_forward(self):
tarinfo = self.tar.next()
with tarfile.open(tarname) as reference:
expected = reference.extractfile(tarinfo.name).read()
with self.tar.extractfile(tarinfo) as file:
self.assertIs(file.seekable(), True)
self.assertEqual(file.seek(10), 10)
self.assertEqual(file.read(20), expected[10:30])

def test_read_through(self):
# Issue #11224: A poorly designed _FileInFile.read() method
# caused seeking errors with stream tar files.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :exc:`AttributeError` from ``seekable()`` and forward ``seek()`` calls on
file objects returned by :meth:`tarfile.TarFile.extractfile` in stream modes.
Loading

Back | FazBrowse Home | New Git URL