summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@rd.io>2012-08-15 02:53:40 -0700
committerAlex Gaynor <alex.gaynor@rd.io>2012-08-15 02:53:40 -0700
commitea1e8b38b3bbf8f21f22028ac256ce62c40d1fc1 (patch)
treed2d800f7ef581c7db486d18c7091ccdc2197f02f /django
parentca6015ca71785dc59106ca36634d93daf59ff5bb (diff)
Ensured that the archive module consistantly explicitly closed all files.
Diffstat (limited to 'django')
-rw-r--r--django/utils/archive.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/django/utils/archive.py b/django/utils/archive.py
index 829b55dd28..0faf1fa781 100644
--- a/django/utils/archive.py
+++ b/django/utils/archive.py
@@ -46,7 +46,8 @@ def extract(path, to_path=''):
Unpack the tar or zip file at the specified path to the directory
specified by to_path.
"""
- Archive(path).extract(to_path)
+ with Archive(path) as archive:
+ archive.extract(to_path)
class Archive(object):
@@ -77,12 +78,21 @@ class Archive(object):
"Path not a recognized archive format: %s" % filename)
return cls
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.close()
+
def extract(self, to_path=''):
self._archive.extract(to_path)
def list(self):
self._archive.list()
+ def close(self):
+ self._archive.close()
+
class BaseArchive(object):
"""
@@ -161,6 +171,9 @@ class TarArchive(BaseArchive):
if extracted:
extracted.close()
+ def close(self):
+ self._archive.close()
+
class ZipArchive(BaseArchive):
@@ -189,6 +202,9 @@ class ZipArchive(BaseArchive):
with open(filename, 'wb') as outfile:
outfile.write(data)
+ def close(self):
+ self._archive.close()
+
extension_map = {
'.tar': TarArchive,
'.tar.bz2': TarArchive,