diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-11-15 12:50:38 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-11-15 16:57:36 +0000 |
commit | cc5913c56a2ae55eb6254e352592751e194b6cd9 (patch) | |
tree | ecb14a87b48bf5355e88016e969e778b96e3d5d3 /morphlib/bins.py | |
parent | 45de4ca798575b38aada4c73c4d31fc9ca138f8d (diff) | |
download | morph-cc5913c56a2ae55eb6254e352592751e194b6cd9.tar.gz |
Override Python tarfile module to close file handles on exception
Reported upstream as: http://bugs.python.org/issue16477
Diffstat (limited to 'morphlib/bins.py')
-rw-r--r-- | morphlib/bins.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/morphlib/bins.py b/morphlib/bins.py index 95566f39..622aa165 100644 --- a/morphlib/bins.py +++ b/morphlib/bins.py @@ -26,9 +26,24 @@ import os import re import errno import stat +import shutil import tarfile +# Work around http://bugs.python.org/issue16477 +def safe_makefile(self, tarinfo, targetpath): + '''Create a file, closing correctly in case of exception''' + + source = self.extractfile(tarinfo) + try: + with open(targetpath, "wb") as target: + shutil.copyfileobj(source, target) + finally: + source.close() + +tarfile.TarFile.makefile = safe_makefile + + def create_chunk(rootdir, f, regexps, dump_memory_profile=None): '''Create a chunk from the contents of a directory. |