diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 21:56:27 -0400 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-05-17 21:56:27 -0400 |
| commit | c671c5da8e8b85ecdbb372074fd5531f385113d7 (patch) | |
| tree | f95aeb6ec96cdeed94ee04cb54f573656dcf250a /ez_setup.py | |
| parent | f78c3beaa55d4026b92703b18d9b54b7e4985e3d (diff) | |
| download | python-setuptools-git-c671c5da8e8b85ecdbb372074fd5531f385113d7.tar.gz | |
Replace get_zip_class with a specialized constructor.
Diffstat (limited to 'ez_setup.py')
| -rw-r--r-- | ez_setup.py | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/ez_setup.py b/ez_setup.py index 8d2199d5..53350e45 100644 --- a/ez_setup.py +++ b/ez_setup.py @@ -69,17 +69,25 @@ def _build_egg(egg, archive_filename, to_dir): raise IOError('Could not build the egg.') -def get_zip_class(): +class ContextualZipFile(zipfile.ZipFile): """ Supplement ZipFile class to support context manager for Python 2.6 """ - class ContextualZipFile(zipfile.ZipFile): - def __enter__(self): - return self - def __exit__(self, type, value, traceback): - self.close() - zf_has_exit = hasattr(zipfile.ZipFile, '__exit__') - return zipfile.ZipFile if zf_has_exit else ContextualZipFile + + 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) @contextlib.contextmanager @@ -90,7 +98,7 @@ def archive_context(filename): old_wd = os.getcwd() try: os.chdir(tmpdir) - with get_zip_class()(filename) as archive: + with ContextualZipFile.compat(filename) as archive: archive.extractall() # going in the directory |
