From: Serhiy Storchaka Date: Sat, 18 Jan 2014 14:14:00 +0000 (+0200) Subject: Issue #20243: TarFile no longer raise ReadError when opened in write mode. X-Git-Tag: v2.7.8~113 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7a278da4ee4ca2e4d98d8285c409dd1f370b46ce;p=python Issue #20243: TarFile no longer raise ReadError when opened in write mode. --- diff --git a/Lib/tarfile.py b/Lib/tarfile.py index dd8a14260c..57ea877911 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -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 diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 8767d5183f..e817f614f6 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -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|" diff --git a/Misc/NEWS b/Misc/NEWS index c1221b5b32..abafed787b 100644 --- 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.