summaryrefslogtreecommitdiff
path: root/morphlib/bins.py
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2012-11-15 12:50:38 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2012-11-15 16:57:36 +0000
commitcc5913c56a2ae55eb6254e352592751e194b6cd9 (patch)
treeecb14a87b48bf5355e88016e969e778b96e3d5d3 /morphlib/bins.py
parent45de4ca798575b38aada4c73c4d31fc9ca138f8d (diff)
downloadmorph-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.py15
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.