[Python-checkins] cpython (2.7): Issue #20243: TarFile no longer raise ReadError when opened in write mode.

serhiy.storchaka python-checkins at python.org
Sat Jan 18 15:31:43 CET 2014


http://hg.python.org/cpython/rev/61b6accbca9f
changeset:   88548:61b6accbca9f
branch:      2.7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Jan 18 16:14:00 2014 +0200
summary:
  Issue #20243: TarFile no longer raise ReadError when opened in write mode.

files:
  Lib/tarfile.py           |   8 ++++++--
  Lib/test/test_tarfile.py |  16 ++++++++++++++++
  Misc/NEWS                |   2 ++
  3 files changed, 24 insertions(+), 2 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1726,7 +1726,9 @@
                 gzip.GzipFile(name, mode, compresslevel, fileobj),
                 **kwargs)
         except IOError:
-            raise ReadError("not a gzip file")
+            if mode == 'r':
+                raise ReadError("not a gzip file")
+            raise
         t._extfileobj = False
         return t
 
@@ -1751,7 +1753,9 @@
         try:
             t = cls.taropen(name, mode, fileobj, **kwargs)
         except (IOError, EOFError):
-            raise ReadError("not a bzip2 file")
+            if mode == 'r':
+                raise ReadError("not a bzip2 file")
+            raise
         t._extfileobj = False
         return t
 
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -979,6 +979,22 @@
             os.unlink(temparchive)
             shutil.rmtree(tempdir)
 
+    def test_open_nonwritable_fileobj(self):
+        for exctype in IOError, EOFError, RuntimeError:
+            class BadFile(StringIO.StringIO):
+                first = True
+                def write(self, data):
+                    if self.first:
+                        self.first = False
+                        raise exctype
+
+            f = BadFile()
+            with self.assertRaises(exctype):
+                tar = tarfile.open(tmpname, self.mode, fileobj=f,
+                                   format=tarfile.PAX_FORMAT,
+                                   pax_headers={'non': 'empty'})
+            self.assertFalse(f.closed)
+
 class StreamWriteTest(WriteTestBase):
 
     mode = "w|"
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,8 @@
 Library
 -------
 
+- Issue #20243: TarFile no longer raise ReadError when opened in write mode.
+
 - Issue #20245: The open functions in the tarfile module now correctly handle
   empty mode.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list