diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-11-15 12:39:12 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-11-15 16:57:35 +0000 |
commit | 45de4ca798575b38aada4c73c4d31fc9ca138f8d (patch) | |
tree | 80ebfd38c12fba0c9b31e209ac293ee6ae446a16 /morphlib/bins.py | |
parent | 4f954e5bfcfc029d524dc46d4176d848e86dedfa (diff) | |
download | morph-45de4ca798575b38aada4c73c4d31fc9ca138f8d.tar.gz |
Close file handles correctly on exceptions when unpacking strata
This prevents us from leaving file handles open when code throws an
exception. When the file handle is on a loopback mount, this is a real
problem because Morph then cannot unmount the image in its exception
handler. In most cases 'with' is the best option.
Diffstat (limited to 'morphlib/bins.py')
-rw-r--r-- | morphlib/bins.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/morphlib/bins.py b/morphlib/bins.py index 71483172..95566f39 100644 --- a/morphlib/bins.py +++ b/morphlib/bins.py @@ -187,11 +187,12 @@ def unpack_binary_from_file(f, dirname): # pragma: no cover tf.makedev = monkey_patcher(tf.makedev) tf.makelink = monkey_patcher(tf.makelink) - tf.extractall(path=dirname) - tf.close + try: + tf.extractall(path=dirname) + finally: + tf.close() def unpack_binary(filename, dirname): - f = open(filename, "rb") - unpack_binary_from_file(f, dirname) - f.close() + with open(filename, "rb") as f: + unpack_binary_from_file(f, dirname) |