classification
Title: Check empty mode in TarFile.*open()
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: lars.gustaebel, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-01-13 22:18 by serhiy.storchaka, last changed 2014-01-18 15:17 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
tarfile_empty_mode.patch serhiy.storchaka, 2014-01-13 22:18 review
Messages (2)
msg208057 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-13 22:18
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.
msg208399 - (view) Author: Roundup Robot (python-dev) Date: 2014-01-18 13:43
New changeset 8edb892f4d69 by Serhiy Storchaka in branch '2.7':
Issue #20245: The open functions in the tarfile module now correctly handle empty mode.
http://hg.python.org/cpython/rev/8edb892f4d69

New changeset 8ef1fd952410 by Serhiy Storchaka in branch '3.3':
Issue #20245: The open functions in the tarfile module now correctly handle empty mode.
http://hg.python.org/cpython/rev/8ef1fd952410

New changeset 7a2db897a1b6 by Serhiy Storchaka in branch 'default':
Issue #20245: The open functions in the tarfile module now correctly handle empty mode.
http://hg.python.org/cpython/rev/7a2db897a1b6
History
Date User Action Args
2014-01-18 15:17:45serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-01-18 13:43:40python-devsetnosy: + python-dev
messages: + msg208399
2014-01-13 22:18:44serhiy.storchakacreate