From 77b2d63b40942087f023999a0329aeea6dd1d6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Gust=C3=A4bel?= Date: Sat, 1 Dec 2007 21:02:12 +0000 Subject: Issue #1531: Read fileobj from the current offset, do not seek to the start. (will backport to 2.5) --- Lib/tarfile.py | 3 ++- Lib/test/test_tarfile.py | 32 ++++++++++++++++++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index ee9922c..ae24291 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1558,7 +1558,8 @@ class TarFile(object): self.closed = False self.members = [] # list of members as TarInfo objects self._loaded = False # flag if all members have been read - self.offset = 0L # current position in the archive file + self.offset = self.fileobj.tell() + # current position in the archive file self.inodes = {} # dictionary caching the inodes of # archive members already added diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 1f08258..a280bdd 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -160,6 +160,38 @@ class MiscReadTest(ReadTest): tar = tarfile.open(fileobj=fobj, mode=self.mode) self.assertEqual(tar.name, None) + def test_fileobj_with_offset(self): + # Skip the first member and store values from the second member + # of the testtar. + tar = tarfile.open(self.tarname, mode=self.mode) + tar.next() + t = tar.next() + name = t.name + offset = t.offset + data = tar.extractfile(t).read() + tar.close() + + # Open the testtar and seek to the offset of the second member. + if self.mode.endswith(":gz"): + _open = gzip.GzipFile + elif self.mode.endswith(":bz2"): + _open = bz2.BZ2File + else: + _open = open + fobj = _open(self.tarname, "rb") + fobj.seek(offset) + + # Test if the tarfile starts with the second member. + tar = tar.open(self.tarname, mode="r:", fileobj=fobj) + t = tar.next() + self.assertEqual(t.name, name) + # Read to the end of fileobj and test if seeking back to the + # beginning works. + tar.getmembers() + self.assertEqual(tar.extractfile(t).read(), data, + "seek back did not work") + tar.close() + def test_fail_comp(self): # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. if self.mode == "r:": diff --git a/Misc/NEWS b/Misc/NEWS index 2023143..c32e3f9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -304,6 +304,9 @@ Core and builtins Library ------- +- Issue #1531: tarfile.py: Read fileobj from the current offset, do not + seek to the start. + - Issue #1534: Added a dictionary sys.float_info with information about the internal floating point type to the sys module. -- cgit v0.12