mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Issue #20245: The open functions in the tarfile module now correctly handle empty mode.
This commit is contained in:
parent
af69fe2311
commit
53ad0cd284
3 changed files with 25 additions and 7 deletions
|
|
@ -1429,10 +1429,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):
|
||||
|
|
@ -1588,7 +1589,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'")
|
||||
|
||||
stream = _Stream(name, filemode, comptype, fileobj, bufsize)
|
||||
|
|
@ -1600,7 +1601,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")
|
||||
|
|
@ -1609,7 +1610,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)
|
||||
|
||||
|
|
@ -1618,7 +1619,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:
|
||||
|
|
@ -1649,7 +1650,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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue