[Python-checkins] cpython (merge 3.3 -> default): Issue #20243: TarFile no longer raise ReadError when opened in write mode.
serhiy.storchaka
python-checkins at python.org
Sat Jan 18 15:31:45 CET 2014
http://hg.python.org/cpython/rev/40a5c7547c3d
changeset: 88550:40a5c7547c3d
parent: 88546:e154b93f3857
parent: 88549:2f3b47b63f91
user: Serhiy Storchaka <storchaka at gmail.com>
date: Sat Jan 18 16:14:49 2014 +0200
summary:
Issue #20243: TarFile no longer raise ReadError when opened in write mode.
files:
Lib/tarfile.py | 27 +++++++++++++++++----------
Lib/test/test_tarfile.py | 16 ++++++++++++++++
Misc/NEWS | 2 ++
3 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1604,19 +1604,22 @@
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 @@
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 @@
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
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1157,6 +1157,22 @@
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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,8 @@
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.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list