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 evanj
Recipients evanj
Date 2009-05-27.12:29:04
SpamBayes Score 1.6769364e-12
Marked as misclassified No
Message-id <1243427347.79.0.845990376951.issue6123@psf.upfronthosting.co.za>
In-reply-to
Content
tarfile.open() with an empty tar archive fails with a ReadError
exception. GNU tar refuses to create empty archives, but tarfile allows
it. See the following code which reproduces the error. I used the
version of tarfile.py from subversion (revision 72458) with Python 2.5
on Linux.

Exception:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    tar = tarfile.open(fileobj=data)
  File "/home/evanj/taskmgr/tarfile.py", line 1649, in open
    raise ReadError("file could not be opened successfully")
tarfile.ReadError: file could not be opened successfully

The problem seems to be that when TarFile.next() is called, it raises
the following exception for the empty tar file:

  File "/home/evanj/taskmgr/tarfile.py", line 2310, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "/home/evanj/taskmgr/tarfile.py", line 1235, in fromtarfile
    obj = cls.frombuf(buf)
  File "/home/evanj/taskmgr/tarfile.py", line 1190, in frombuf
    raise HeaderError("empty header")


The attached patch works for me, but no guarantees that it doesn't cause
other problems!


Sample code:

import cStringIO
import tarfile

# Create an empty tar file
data = cStringIO.StringIO()
tar = tarfile.open(mode="w", fileobj=data)
tar.close()
print "empty tar file; length:", len(data.getvalue())

# Open the tar file
data.seek(0)
tar = tarfile.open(fileobj=data)
print tar
History
Date User Action Args
2009-05-27 12:29:07evanjsetrecipients: + evanj
2009-05-27 12:29:07evanjsetmessageid: <1243427347.79.0.845990376951.issue6123@psf.upfronthosting.co.za>
2009-05-27 12:29:06evanjlinkissue6123 messages
2009-05-27 12:29:05evanjcreate