Commit aee0e63e authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20243: TarFile no longer raise ReadError when opened in write mode.

parents 7d68a1c9 c2d01423
...@@ -1604,19 +1604,22 @@ class TarFile(object): ...@@ -1604,19 +1604,22 @@ class TarFile(object):
except (ImportError, AttributeError): except (ImportError, AttributeError):
raise CompressionError("gzip module is not available") raise CompressionError("gzip module is not available")
extfileobj = fileobj is not None
try: try:
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj) 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) t = cls.taropen(name, mode, fileobj, **kwargs)
except OSError: except OSError:
if not extfileobj and fileobj is not None: fileobj.close()
fileobj.close() if mode == 'r':
if fileobj is None: raise ReadError("not a gzip file")
raise raise
raise ReadError("not a gzip file")
except: except:
if not extfileobj and fileobj is not None: fileobj.close()
fileobj.close()
raise raise
t._extfileobj = False t._extfileobj = False
return t return t
...@@ -1641,7 +1644,9 @@ class TarFile(object): ...@@ -1641,7 +1644,9 @@ class TarFile(object):
t = cls.taropen(name, mode, fileobj, **kwargs) t = cls.taropen(name, mode, fileobj, **kwargs)
except (OSError, EOFError): except (OSError, EOFError):
fileobj.close() fileobj.close()
raise ReadError("not a bzip2 file") if mode == 'r':
raise ReadError("not a bzip2 file")
raise
t._extfileobj = False t._extfileobj = False
return t return t
...@@ -1664,7 +1669,9 @@ class TarFile(object): ...@@ -1664,7 +1669,9 @@ class TarFile(object):
t = cls.taropen(name, mode, fileobj, **kwargs) t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError): except (lzma.LZMAError, EOFError):
fileobj.close() fileobj.close()
raise ReadError("not an lzma file") if mode == 'r':
raise ReadError("not an lzma file")
raise
t._extfileobj = False t._extfileobj = False
return t return t
......
...@@ -1157,6 +1157,22 @@ class WriteTest(WriteTestBase, unittest.TestCase): ...@@ -1157,6 +1157,22 @@ class WriteTest(WriteTestBase, unittest.TestCase):
finally: finally:
os.chdir(cwd) 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): class GzipWriteTest(GzipTest, WriteTest):
pass pass
......
...@@ -25,6 +25,8 @@ Core and Builtins ...@@ -25,6 +25,8 @@ Core and Builtins
Library 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 - Issue #20238: TarFile opened with external fileobj and "w:gz" mode didn't
write complete output on close. write complete output on close.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment