[Python-checkins] cpython (merge 3.3 -> default): Issue #20245: The open functions in the tarfile module now correctly handle

serhiy.storchaka python-checkins at python.org
Sat Jan 18 14:43:44 CET 2014


http://hg.python.org/cpython/rev/7a2db897a1b6
changeset:   88544:7a2db897a1b6
parent:      88541:32f9e0ae23f7
parent:      88543:8ef1fd952410
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Jan 18 15:37:21 2014 +0200
summary:
  Issue #20245: The open functions in the tarfile module now correctly handle empty mode.

files:
  Lib/tarfile.py           |  15 ++++++++-------
  Lib/test/test_tarfile.py |  14 ++++++++++++++
  Misc/NEWS                |   3 +++
  3 files changed, 25 insertions(+), 7 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1405,10 +1405,11 @@
            can be determined, `mode' is overridden by `fileobj's mode.
            `fileobj' is not closed, when TarFile is closed.
         """
-        if len(mode) > 1 or mode not in "raw":
+        modes = {"r": "rb", "a": "r+b", "w": "wb"}
+        if mode not in modes:
             raise ValueError("mode must be 'r', 'a' or 'w'")
         self.mode = mode
-        self._mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
+        self._mode = modes[mode]
 
         if not fileobj:
             if self.mode == "a" and not os.path.exists(name):
@@ -1564,7 +1565,7 @@
             filemode = filemode or "r"
             comptype = comptype or "tar"
 
-            if filemode not in "rw":
+            if filemode not in ("r", "w"):
                 raise ValueError("mode must be 'r' or 'w'")
 
             stream = _Stream(name, filemode, comptype, fileobj, bufsize)
@@ -1576,7 +1577,7 @@
             t._extfileobj = False
             return t
 
-        elif mode in "aw":
+        elif mode in ("a", "w"):
             return cls.taropen(name, mode, fileobj, **kwargs)
 
         raise ValueError("undiscernible mode")
@@ -1585,7 +1586,7 @@
     def taropen(cls, name, mode="r", fileobj=None, **kwargs):
         """Open uncompressed tar archive name for reading or writing.
         """
-        if len(mode) > 1 or mode not in "raw":
+        if mode not in ("r", "a", "w"):
             raise ValueError("mode must be 'r', 'a' or 'w'")
         return cls(name, mode, fileobj, **kwargs)
 
@@ -1594,7 +1595,7 @@
         """Open gzip compressed tar archive name for reading or writing.
            Appending is not allowed.
         """
-        if len(mode) > 1 or mode not in "rw":
+        if mode not in ("r", "w"):
             raise ValueError("mode must be 'r' or 'w'")
 
         try:
@@ -1625,7 +1626,7 @@
         """Open bzip2 compressed tar archive name for reading or writing.
            Appending is not allowed.
         """
-        if len(mode) > 1 or mode not in "rw":
+        if mode not in ("r", "w"):
             raise ValueError("mode must be 'r' or 'w'.")
 
         try:
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
@@ -43,6 +43,7 @@
     tarname = tarname
     suffix = ''
     open = io.FileIO
+    taropen = tarfile.TarFile.taropen
 
     @property
     def mode(self):
@@ -53,18 +54,21 @@
     tarname = gzipname
     suffix = 'gz'
     open = gzip.GzipFile if gzip else None
+    taropen = tarfile.TarFile.gzopen
 
 @support.requires_bz2
 class Bz2Test:
     tarname = bz2name
     suffix = 'bz2'
     open = bz2.BZ2File if bz2 else None
+    taropen = tarfile.TarFile.bz2open
 
 @support.requires_lzma
 class LzmaTest:
     tarname = xzname
     suffix = 'xz'
     open = lzma.LZMAFile if lzma else None
+    taropen = tarfile.TarFile.xzopen
 
 
 class ReadTest(TarTest):
@@ -289,6 +293,16 @@
         with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
             self.assertEqual(tar.name, None)
 
+    def test_illegal_mode_arg(self):
+        with open(tmpname, 'wb'):
+            pass
+        with self.assertRaisesRegex(ValueError, 'mode must be '):
+            tar = self.taropen(tmpname, 'q')
+        with self.assertRaisesRegex(ValueError, 'mode must be '):
+            tar = self.taropen(tmpname, 'rw')
+        with self.assertRaisesRegex(ValueError, 'mode must be '):
+            tar = self.taropen(tmpname, '')
+
     def test_fileobj_with_offset(self):
         # Skip the first member and store values from the second member
         # of the testtar.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@
 Library
 -------
 
+- Issue #20245: The open functions in the tarfile module now correctly handle
+  empty mode.
+
 - Issue #20242: Fixed basicConfig() format strings for the alternative
   formatting styles. Thanks to kespindler for the bug report and patch.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list