[issue26877] tarfile use wrong code when read from fileobj

Martin Panter report at bugs.python.org
Fri Apr 29 06:08:05 EDT 2016


Martin Panter added the comment:

On the other hand, you cannot use a pipe with mode="r" because that mode does seeking; that is why I asked for more details on what you are doing:

$ cat | python3 -c 'import tarfile, sys; tarfile.open(fileobj=sys.stdin.buffer, mode="r")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.5/tarfile.py", line 1580, in open
    return func(name, filemode, fileobj, **kwargs)
  File "/usr/lib/python3.5/tarfile.py", line 1610, in taropen
    return cls(name, mode, fileobj, **kwargs)
  File "/usr/lib/python3.5/tarfile.py", line 1467, in __init__
    self.offset = self.fileobj.tell()
OSError: [Errno 29] Illegal seek

Python 3 has the io.RawIOBase class which models the low level read() system call and does partial reads, and the io.BufferedIOBase class whose read() method guarantees an exact read. You can often wrap a raw object with BufferedReader to easily convert to the buffered kind.

----------

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


More information about the Python-bugs-list mailing list