summaryrefslogtreecommitdiffstats
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-01-18 13:35:19 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-01-18 13:35:19 (GMT)
commit75ba21a77d41be2a4d5f824363e69d45bcba1ec4 (patch)
treed3c979104366213fea1677a8023454924e9611b5 /Lib/tarfile.py
parentce46aed13e2db3b48952d68a866bb8276dde89fe (diff)
downloadcpython-75ba21a77d41be2a4d5f824363e69d45bcba1ec4.zip
cpython-75ba21a77d41be2a4d5f824363e69d45bcba1ec4.tar.gz
cpython-75ba21a77d41be2a4d5f824363e69d45bcba1ec4.tar.bz2
Issue #20245: The open functions in the tarfile module now correctly handle empty mode.
Diffstat (limited to 'Lib/tarfile.py')
-rw-r--r--Lib/tarfile.py15
1 files changed, 8 insertions, 7 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index e73ac90..dd8a142 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1508,10 +1508,11 @@ class TarFile(object):
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):
@@ -1681,7 +1682,7 @@ class TarFile(object):
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'")
t = cls(name, filemode,
@@ -1690,7 +1691,7 @@ class TarFile(object):
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")
@@ -1699,7 +1700,7 @@ class TarFile(object):
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)
@@ -1708,7 +1709,7 @@ class TarFile(object):
"""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:
@@ -1734,7 +1735,7 @@ class TarFile(object):
"""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: