[Python-checkins] r53161 - in python/trunk: Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS

lars.gustaebel python-checkins at python.org
Wed Dec 27 11:30:47 CET 2006


Author: lars.gustaebel
Date: Wed Dec 27 11:30:46 2006
New Revision: 53161

Modified:
   python/trunk/Lib/tarfile.py
   python/trunk/Lib/test/test_tarfile.py
   python/trunk/Misc/NEWS
Log:
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.

Will backport to 2.5.


Modified: python/trunk/Lib/tarfile.py
==============================================================================
--- python/trunk/Lib/tarfile.py	(original)
+++ python/trunk/Lib/tarfile.py	Wed Dec 27 11:30:46 2006
@@ -1141,9 +1141,13 @@
             # 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")
 

Modified: python/trunk/Lib/test/test_tarfile.py
==============================================================================
--- python/trunk/Lib/test/test_tarfile.py	(original)
+++ python/trunk/Lib/test/test_tarfile.py	Wed Dec 27 11:30:46 2006
@@ -648,6 +648,16 @@
         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 @@
     tests = [
         FileModeTest,
         HeaderErrorTest,
+        OpenFileobjTest,
         ReadTest,
         ReadStreamTest,
         ReadDetectTest,

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Dec 27 11:30:46 2006
@@ -103,6 +103,8 @@
 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


More information about the Python-checkins mailing list