summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2006-12-27 10:30:46 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2006-12-27 10:30:46 (GMT)
commita7ba6fc548d1d38e163aaa9ab5d9405e412ec62a (patch)
tree8d64ca20250b4cde9161956a57ce2d1b7d02dd1b /Lib
parent71662323992907f1da51ea61a6f29b09e571c089 (diff)
downloadcpython-a7ba6fc548d1d38e163aaa9ab5d9405e412ec62a.zip
cpython-a7ba6fc548d1d38e163aaa9ab5d9405e412ec62a.tar.gz
cpython-a7ba6fc548d1d38e163aaa9ab5d9405e412ec62a.tar.bz2
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Will backport to 2.5.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py4
-rw-r--r--Lib/test/test_tarfile.py11
2 files changed, 15 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 658f214..1785144 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -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")
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index a76ceb1..1674594 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -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,