diff options
| -rw-r--r-- | ez_setup.py | 23 | ||||
| -rw-r--r-- | pkg_resources.py | 23 |
2 files changed, 26 insertions, 20 deletions
diff --git a/ez_setup.py b/ez_setup.py index 810c552b..1f2468f9 100644 --- a/ez_setup.py +++ b/ez_setup.py @@ -71,19 +71,22 @@ def _build_egg(egg, archive_filename, to_dir): class ContextualZipFile(zipfile.ZipFile): """ - Supplement ZipFile context manager class supporting all Python versions. - - ZipFile supports a context manager interface only in versions [2.7, 3.0> & - 3.2+. - + Supplement ZipFile class to support context manager for Python 2.6 """ - if not hasattr(zipfile.ZipFile, '__exit__'): - def __enter__(self): - return self + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() - def __exit__(self, type, value, traceback): - self.close() + def __new__(cls, *args, **kwargs): + """ + Construct a ZipFile or ContextualZipFile as appropriate + """ + if hasattr(zipfile.ZipFile, '__exit__'): + return zipfile.ZipFile(*args, **kwargs) + return super(ContextualZipFile, cls).__new__(cls) @contextlib.contextmanager diff --git a/pkg_resources.py b/pkg_resources.py index a9c737c4..5734989d 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1561,19 +1561,22 @@ def build_zipmanifest(path): class ContextualZipFile(zipfile.ZipFile): """ - Supplement ZipFile context manager class supporting all Python versions. - - ZipFile supports a context manager interface only in versions [2.7, 3.0> & - 3.2+. - + Supplement ZipFile class to support context manager for Python 2.6 """ - if not hasattr(zipfile.ZipFile, '__exit__'): - def __enter__(self): - return self + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() - def __exit__(self, type, value, traceback): - self.close() + def __new__(cls, *args, **kwargs): + """ + Construct a ZipFile or ContextualZipFile as appropriate + """ + if hasattr(zipfile.ZipFile, '__exit__'): + return zipfile.ZipFile(*args, **kwargs) + return super(ContextualZipFile, cls).__new__(cls) class ZipProvider(EggProvider): |
