summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-01-18 14:14:10 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-01-18 14:14:10 (GMT)
commitc2d01423e02d9721f897812cf6a93e64c7d75c15 (patch)
tree621f81bc2b6e40b3e87e4a1be364a1533dc39541
parent9fbec7ad5e8fcdd9dcc5abd496341b86d12a4472 (diff)
downloadcpython-c2d01423e02d9721f897812cf6a93e64c7d75c15.zip
cpython-c2d01423e02d9721f897812cf6a93e64c7d75c15.tar.gz
cpython-c2d01423e02d9721f897812cf6a93e64c7d75c15.tar.bz2
Issue #20243: TarFile no longer raise ReadError when opened in write mode.
-rwxr-xr-xLib/tarfile.py29
-rw-r--r--Lib/test/test_tarfile.py16
-rw-r--r--Misc/NEWS2
3 files changed, 36 insertions, 11 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 8a69988..6c40cb9 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1628,19 +1628,22 @@ class TarFile(object):
except (ImportError, AttributeError):
raise CompressionError("gzip module is not available")
- extfileobj = fileobj is not None
try:
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
+ except OSError:
+ if fileobj is not None and mode == 'r':
+ raise ReadError("not a gzip file")
+ raise
+
+ try:
t = cls.taropen(name, mode, fileobj, **kwargs)
- except IOError:
- if not extfileobj and fileobj is not None:
- fileobj.close()
- if fileobj is None:
- raise
- raise ReadError("not a gzip file")
+ except OSError:
+ fileobj.close()
+ if mode == 'r':
+ raise ReadError("not a gzip file")
+ raise
except:
- if not extfileobj and fileobj is not None:
- fileobj.close()
+ fileobj.close()
raise
t._extfileobj = False
return t
@@ -1665,7 +1668,9 @@ class TarFile(object):
t = cls.taropen(name, mode, fileobj, **kwargs)
except (IOError, EOFError):
fileobj.close()
- raise ReadError("not a bzip2 file")
+ if mode == 'r':
+ raise ReadError("not a bzip2 file")
+ raise
t._extfileobj = False
return t
@@ -1688,7 +1693,9 @@ class TarFile(object):
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
- raise ReadError("not an lzma file")
+ if mode == 'r':
+ raise ReadError("not an lzma file")
+ raise
t._extfileobj = False
return t
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index ceaa3aa..f22b908 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1155,6 +1155,22 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally:
os.chdir(cwd)
+ def test_open_nonwritable_fileobj(self):
+ for exctype in OSError, EOFError, RuntimeError:
+ class BadFile(io.BytesIO):
+ 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 GzipWriteTest(GzipTest, WriteTest):
pass
diff --git a/Misc/NEWS b/Misc/NEWS
index 839e8fe..533d5c5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,8 @@ Core and Builtins
Library
-------
+- Issue #20243: TarFile no longer raise ReadError when opened in write mode.
+
- Issue #20238: TarFile opened with external fileobj and "w:gz" mode didn't
write complete output on close.