]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
authorLars Gustäbel <lars@gustaebel.de>
Wed, 27 Dec 2006 10:30:46 +0000 (10:30 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Wed, 27 Dec 2006 10:30:46 +0000 (10:30 +0000)
Will backport to 2.5.

Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 658f2144bf19f57d715ecf4b7b5164a1ac6b5145..178514470e8d273e4c8213136eb6992ade196eca 100644 (file)
@@ -1141,9 +1141,13 @@ class TarFile(object):
             # Find out which *open() is appropriate for opening the file.
             for comptype in cls.OPEN_METH:
                 func = getattr(cls, cls.OPEN_METH[comptype])
+                if fileobj is not None:
+                    saved_pos = fileobj.tell()
                 try:
                     return func(name, "r", fileobj)
                 except (ReadError, CompressionError):
+                    if fileobj is not None:
+                        fileobj.seek(saved_pos)
                     continue
             raise ReadError("file could not be opened successfully")
 
index a76ceb1d2a77f25704d280a55a69bcbf26674121..16745946fe7fab09885f1583467e1c0f0417be89 100644 (file)
@@ -648,6 +648,16 @@ class HeaderErrorTest(unittest.TestCase):
         b = "a" + buf[1:] # manipulate the buffer, so checksum won't match.
         self.assertRaises(tarfile.HeaderError, tarfile.TarInfo.frombuf, b)
 
+class OpenFileobjTest(BaseTest):
+    # Test for SF bug #1496501.
+
+    def test_opener(self):
+        fobj = StringIO.StringIO("foo\n")
+        try:
+            tarfile.open("", "r", fileobj=fobj)
+        except tarfile.ReadError:
+            self.assertEqual(fobj.tell(), 0, "fileobj's position has moved")
+
 if bz2:
     # Bzip2 TestCases
     class ReadTestBzip2(ReadTestGzip):
@@ -693,6 +703,7 @@ def test_main():
     tests = [
         FileModeTest,
         HeaderErrorTest,
+        OpenFileobjTest,
         ReadTest,
         ReadStreamTest,
         ReadDetectTest,
index 363320b1ea32d84fa647ed2c78520d9e693ca159..ba882ef89d90d12419bf3495cb22d4692d247719 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -103,6 +103,8 @@ Core and builtins
 Library
 -------
 
+- Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
+
 - Patch #1182394 from Shane Holloway: speed up HMAC.hexdigest.
 
 - Patch #1262036: Prevent TarFiles from being added to themselves under