New submission from Pulin Shah <sha...@gmail.com>:

I ran into a problem the other day while trying to extract a slightly corrupted 
tar file.  I suspect this problem is really only an issue on Windows systems.  
I am running Python 2.7.1 r271:86832 win32.

The following code (simplified) snipet

try:
        tar = tarfile.open(args.file)
        tar.extractall(basefolder)
        tar.close()
except tarfile.ReadError:
        shutil.rmtree(basefolder)
except IOError:
        shutil.rmtree(basefolder)

was throwing a WindowsError on the rmtree calls.

This is due to the tarfile library not closing file handles in the case of an 
exception in the copyfileobj function, and Windows inability to delete open 
files.  

I was able to patch the issue locally by modifying tarfile's makefile function 
as follows:

    def makefile(self, tarinfo, targetpath):
        """Make a file called targetpath.
        """
        source = self.extractfile(tarinfo)
        target = bltn_open(targetpath, "wb")
        try:
            copyfileobj(source, target)
        except:
            source.close()
            target.close()
            raise
        source.close()
        target.close()

There is probably a cleaner way of implementing it.

I'm hoping you can integrate this patch into later versions of the lib.

Thanks.

----------
components: Library (Lib)
messages: 133153
nosy: shahpr
priority: normal
severity: normal
status: open
title: File handle leak in TarFile lib
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11787>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to