diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-01-18 16:14:49 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-01-18 16:14:49 +0200 |
commit | aee0e63ed052ae891e4b5f342aae512787098fa4 (patch) | |
tree | 5c9b8a65a85398af239317bb84264f6141c7d439 | |
parent | 7d68a1c921bb53c5b72fc7a884fc048185001f2c (diff) | |
parent | c2d01423e02d9721f897812cf6a93e64c7d75c15 (diff) | |
download | cpython-git-aee0e63ed052ae891e4b5f342aae512787098fa4.tar.gz |
Issue #20243: TarFile no longer raise ReadError when opened in write mode.
-rwxr-xr-x | Lib/tarfile.py | 27 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 16 | ||||
-rw-r--r-- | Misc/NEWS | 2 |
3 files changed, 35 insertions, 10 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index ec8af06498..d914485c66 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1604,19 +1604,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 OSError: - if not extfileobj and fileobj is not None: - fileobj.close() - if fileobj is None: - raise - raise ReadError("not a gzip file") + 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 @@ -1641,7 +1644,9 @@ class TarFile(object): t = cls.taropen(name, mode, fileobj, **kwargs) except (OSError, EOFError): fileobj.close() - raise ReadError("not a bzip2 file") + if mode == 'r': + raise ReadError("not a bzip2 file") + raise t._extfileobj = False return t @@ -1664,7 +1669,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 e0c91035bc..b53f3ac399 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1157,6 +1157,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 @@ -25,6 +25,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. |