diff -r 60163fc72017 Lib/tarfile.py --- a/Lib/tarfile.py Mon Jan 13 13:51:42 2014 -0500 +++ b/Lib/tarfile.py Tue Jan 14 00:13:50 2014 +0200 @@ -1576,7 +1576,7 @@ t._extfileobj = False return t - elif mode in "aw": + elif mode not in ("a", "w"): return cls.taropen(name, mode, fileobj, **kwargs) raise ValueError("undiscernible mode") @@ -1585,7 +1585,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 +1594,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 +1625,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 -r 60163fc72017 Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Mon Jan 13 13:51:42 2014 -0500 +++ b/Lib/test/test_tarfile.py Tue Jan 14 00:13:50 2014 +0200 @@ -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.