diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 22:04:35 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 22:04:35 -0400 |
commit | ec81e4dca2f1735bb1ac787b464a9d0f915dd7b9 (patch) | |
tree | 45bf5a16d77dd39c75738363dcdd92736c2e7d1e | |
parent | c671c5da8e8b85ecdbb372074fd5531f385113d7 (diff) | |
download | python-setuptools-git-ec81e4dca2f1735bb1ac787b464a9d0f915dd7b9.tar.gz |
Use ContextualZipFile for context manager support
-rw-r--r-- | pkg_resources.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/pkg_resources.py b/pkg_resources.py index 63a31fb8..ba03f628 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1551,18 +1551,35 @@ def build_zipmanifest(path): * [7] - zipinfo.CRC """ zipinfo = dict() - zfile = zipfile.ZipFile(path) - #Got ZipFile has not __exit__ on python 3.1 - try: + with ContextualZipFile.compat(path) as zfile: for zitem in zfile.namelist(): zpath = zitem.replace('/', os.sep) zipinfo[zpath] = zfile.getinfo(zitem) assert zipinfo[zpath] is not None - finally: - zfile.close() return zipinfo +class ContextualZipFile(zipfile.ZipFile): + """ + Supplement ZipFile class to support context manager for Python 2.6 + """ + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + @classmethod + def compat(cls, *args, **kwargs): + """ + Construct a ZipFile or ContextualZipFile as appropriate + """ + zf_has_exit = hasattr(zipfile.ZipFile, '__exit__') + class_ = zipfile.ZipFile if zf_has_exit else cls + return class_(*args, **kwargs) + + class ZipProvider(EggProvider): """Resource support for zips and eggs""" |