[New-bugs-announce] [issue20245] Check empty mode in TarFile.*open()

Serhiy Storchaka report at bugs.python.org
Mon Jan 13 23:18:44 CET 2014


New submission from Serhiy Storchaka:

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.

----------
assignee: serhiy.storchaka
components: Library (Lib)
files: tarfile_empty_mode.patch
keywords: patch
messages: 208057
nosy: lars.gustaebel, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Check empty mode in TarFile.*open()
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4
Added file: http://bugs.python.org/file33451/tarfile_empty_mode.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20245>
_______________________________________


More information about the New-bugs-announce mailing list