classification
Title: tarfile raises an exception when reading an empty tar in streaming mode
Type: behavior Stage: needs patch
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, iritkatriel, larry, lars.gustaebel, liu chang, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-12-15 19:27 by gregory.p.smith, last changed 2021-11-30 00:06 by iritkatriel.

Files
File name Uploaded Description Edit
fix-tar-23056.patch liu chang, 2014-12-16 12:09 review
Messages (4)
msg232675 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2014-12-15 19:27
$ cat >test.py <<EOF
import tarfile
import sys

with tarfile.open(sys.argv[1], mode="r|*") as f:
  while True:
    info = f.next()
    if not info:
      break
EOF
$ tar cf test.tar -T /dev/null
$ python2.7 test.py test.tar
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    info = f.next()
  File "/usr/lib/python2.7/tarfile.py", line 2319, in next
    self.fileobj.seek(self.offset)
  File "/usr/lib/python2.7/tarfile.py", line 555, in seek
    raise StreamError("seeking backwards is not allowed")
tarfile.StreamError: seeking backwards is not allowed
$ python3.4 test.py test.tar
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    info = f.next()
  File "/usr/lib/python3.4/tarfile.py", line 2244, in next
    self.fileobj.seek(self.offset)
  File "/usr/lib/python3.4/tarfile.py", line 518, in seek
    raise StreamError("seeking backwards is not allowed")
tarfile.StreamError: seeking backwards is not allowed

I have reconfirmed that the above still happens using a top of tree 2.7.9+ build.
msg232735 - (view) Author: liu chang (liu chang) * Date: 2014-12-16 12:02
2232     def next(self):
2233         """Return the next member of the archive as a TarInfo object, when
2234            TarFile is opened for reading. Return None if there is no more
2235            available.
2236         """
2237         self._check("ra")
2238         if self.firstmember is not None:
2239             m = self.firstmember
2240             self.firstmember = None
2241             return m
2242 
2243         # Read the next block.
2244         self.fileobj.seek(self.offset)
2245         tarinfo = None

raise a StreamError at #2244, It should catch this Error and return None. I would like to post a patch to fix it.
msg232736 - (view) Author: liu chang (liu chang) * Date: 2014-12-16 12:09
a simple fix
msg407339 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-30 00:06
Reproduced on 3.11.
History
Date User Action Args
2021-11-30 00:06:03iritkatrielsetnosy: + iritkatriel

messages: + msg407339
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.4, Python 3.5
2014-12-16 12:09:20liu changsetfiles: + fix-tar-23056.patch
keywords: + patch
messages: + msg232736
2014-12-16 12:02:19liu changsetnosy: + liu chang
messages: + msg232735
2014-12-16 07:40:44serhiy.storchakasetnosy: + lars.gustaebel, larry, serhiy.storchaka
2014-12-15 19:27:54gregory.p.smithcreate