This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients lars.gustaebel, serhiy.storchaka
Date 2014-01-13.22:18:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1389651524.31.0.708867421732.issue20245@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-01-13 22:18:44serhiy.storchakasetrecipients: + serhiy.storchaka, lars.gustaebel
2014-01-13 22:18:44serhiy.storchakasetmessageid: <1389651524.31.0.708867421732.issue20245@psf.upfronthosting.co.za>
2014-01-13 22:18:44serhiy.storchakalinkissue20245 messages
2014-01-13 22:18:44serhiy.storchakacreate