Issue 20245: Check empty mode in TarFile.*open() (original) (raw)
TarFile's *open() class methods checks the mode argument to raise helpful error:
t = tarfile.TarFile.taropen('xxx.tar', 'q') Traceback (most recent call last): File "", line 1, in File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1589, in taropen raise ValueError("mode must be 'r', 'a' or 'w'") ValueError: mode must be 'r', 'a' or 'w'
But often this check doesn't catch empty mode.
t = tarfile.TarFile.taropen('xxx.tar', '') Traceback (most recent call last): File "", line 1, in File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1590, in taropen return cls(name, mode, fileobj, **kwargs) File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1411, in init self._mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode] KeyError: '' t = tarfile.TarFile.gzopen('xxx.tar', '') Traceback (most recent call last): File "", line 1, in File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1608, in gzopen fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj) File "/home/serhiy/py/cpython/Lib/gzip.py", line 181, in init fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') ValueError: Must have exactly one of create/read/write/append mode and at most one plus t = tarfile.TarFile.bz2open('xxx.tar', '') Traceback (most recent call last): File "", line 1, in File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1640, in bz2open t = cls.taropen(name, mode, fileobj, **kwargs) File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1590, in taropen return cls(name, mode, fileobj, **kwargs) File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1411, in init self._mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode] KeyError: ''
Only xzopen() works correctly.
t = tarfile.TarFile.xzopen('xxx.tar', '') Traceback (most recent call last): File "", line 1, in File "/home/serhiy/py/cpython/Lib/tarfile.py", line 1653, in xzopen raise ValueError("mode must be 'r' or 'w'") ValueError: mode must be 'r' or 'w'
Here is a patch which fixes the mode argument checking.