From: Lars Gustäbel Date: Wed, 27 Dec 2006 10:36:58 +0000 (+0000) Subject: Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument. X-Git-Tag: v2.5.1c1~199 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f9a2c63c79f51540e261854679eeb15d873f7dff;p=python Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument. Backport from rev. 53161. --- diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 4a41d9fb66..4c7dadbc6a 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1133,9 +1133,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") diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index fbcd1912b1..692d72a17c 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -625,6 +625,15 @@ class FileModeTest(unittest.TestCase): self.assertEqual(tarfile.filemode(0755), '-rwxr-xr-x') self.assertEqual(tarfile.filemode(07111), '---s--s--t') +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 @@ -670,6 +679,7 @@ def test_main(): tests = [ FileModeTest, + OpenFileobjTest, ReadTest, ReadStreamTest, ReadDetectTest, diff --git a/Misc/NEWS b/Misc/NEWS index 205ea409b5..9acc77194b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -122,6 +122,8 @@ Extension Modules Library ------- +- Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument. + - Patch #1262036: Prevent TarFiles from being added to themselves under certain conditions.