mirror of https://github.com/django/django.git
Ensured that the archive module consistantly explicitly closed all files.
This commit is contained in:
parent
ca6015ca71
commit
ea1e8b38b3
|
@ -46,7 +46,8 @@ def extract(path, to_path=''):
|
||||||
Unpack the tar or zip file at the specified path to the directory
|
Unpack the tar or zip file at the specified path to the directory
|
||||||
specified by to_path.
|
specified by to_path.
|
||||||
"""
|
"""
|
||||||
Archive(path).extract(to_path)
|
with Archive(path) as archive:
|
||||||
|
archive.extract(to_path)
|
||||||
|
|
||||||
|
|
||||||
class Archive(object):
|
class Archive(object):
|
||||||
|
@ -77,12 +78,21 @@ class Archive(object):
|
||||||
"Path not a recognized archive format: %s" % filename)
|
"Path not a recognized archive format: %s" % filename)
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def extract(self, to_path=''):
|
def extract(self, to_path=''):
|
||||||
self._archive.extract(to_path)
|
self._archive.extract(to_path)
|
||||||
|
|
||||||
def list(self):
|
def list(self):
|
||||||
self._archive.list()
|
self._archive.list()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._archive.close()
|
||||||
|
|
||||||
|
|
||||||
class BaseArchive(object):
|
class BaseArchive(object):
|
||||||
"""
|
"""
|
||||||
|
@ -161,6 +171,9 @@ class TarArchive(BaseArchive):
|
||||||
if extracted:
|
if extracted:
|
||||||
extracted.close()
|
extracted.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._archive.close()
|
||||||
|
|
||||||
|
|
||||||
class ZipArchive(BaseArchive):
|
class ZipArchive(BaseArchive):
|
||||||
|
|
||||||
|
@ -189,6 +202,9 @@ class ZipArchive(BaseArchive):
|
||||||
with open(filename, 'wb') as outfile:
|
with open(filename, 'wb') as outfile:
|
||||||
outfile.write(data)
|
outfile.write(data)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._archive.close()
|
||||||
|
|
||||||
extension_map = {
|
extension_map = {
|
||||||
'.tar': TarArchive,
|
'.tar': TarArchive,
|
||||||
'.tar.bz2': TarArchive,
|
'.tar.bz2': TarArchive,
|
||||||
|
|
|
@ -27,12 +27,14 @@ class ArchiveTester(object):
|
||||||
os.chdir(self.old_cwd)
|
os.chdir(self.old_cwd)
|
||||||
|
|
||||||
def test_extract_method(self):
|
def test_extract_method(self):
|
||||||
Archive(self.archive).extract(self.tmpdir)
|
with Archive(self.archive) as archive:
|
||||||
|
archive.extract(self.tmpdir)
|
||||||
self.check_files(self.tmpdir)
|
self.check_files(self.tmpdir)
|
||||||
|
|
||||||
def test_extract_method_no_to_path(self):
|
def test_extract_method_no_to_path(self):
|
||||||
os.chdir(self.tmpdir)
|
os.chdir(self.tmpdir)
|
||||||
Archive(self.archive_path).extract()
|
with Archive(self.archive_path) as archive:
|
||||||
|
archive.extract()
|
||||||
self.check_files(self.tmpdir)
|
self.check_files(self.tmpdir)
|
||||||
|
|
||||||
def test_extract_function(self):
|
def test_extract_function(self):
|
||||||
|
|
Loading…
Reference in New Issue