Message208057
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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. |
|
Date |
User |
Action |
Args |
2014-01-13 22:18:44 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, lars.gustaebel |
2014-01-13 22:18:44 | serhiy.storchaka | set | messageid: <1389651524.31.0.708867421732.issue20245@psf.upfronthosting.co.za> |
2014-01-13 22:18:44 | serhiy.storchaka | link | issue20245 messages |
2014-01-13 22:18:44 | serhiy.storchaka | create | |
|