]> granicus.if.org Git - python/commitdiff
Issue #20243: TarFile no longer raise ReadError when opened in write mode.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 18 Jan 2014 14:14:00 +0000 (16:14 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 18 Jan 2014 14:14:00 +0000 (16:14 +0200)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index dd8a14260c83784688ac850e4ece853349412e24..57ea877911b38df8c3f1d11088b82bf7675f9ebc 100644 (file)
@@ -1726,7 +1726,9 @@ class TarFile(object):
                 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 @@ class TarFile(object):
         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
 
index 8767d5183fe5792fae6dec0010b1425b47437207..e817f614f627ffe3997fc9c8ffca682accfcb106 100644 (file)
@@ -979,6 +979,22 @@ class WriteTest(WriteTestBase):
             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|"
index c1221b5b320608726942d752053869d28e484da1..abafed787b5ea53d8a9677487b9ce87d534ca09a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,8 @@ Core and Builtins
 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.