[New-bugs-announce] [issue6123] tarfile:

Evan Jones report at bugs.python.org
Wed May 27 14:29:07 CEST 2009

New submission from Evan Jones <evanj at mit.edu>:

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.


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)
print "empty tar file; length:", len(data.getvalue())

# Open the tar file
tar = tarfile.open(fileobj=data)
print tar

components: Extension Modules
files: tarfile-empty.diff
keywords: patch
messages: 88416
nosy: evanj at mit.edu
severity: normal
status: open
title: tarfile:
type: behavior
Added file: http://bugs.python.org/file14093/tarfile-empty.diff

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list