diff options
| author | Jurko Gospodnetić <jurko.gospodnetic@pke.hr> | 2014-05-28 05:53:14 +0200 |
|---|---|---|
| committer | Jurko Gospodnetić <jurko.gospodnetic@pke.hr> | 2014-05-28 05:53:14 +0200 |
| commit | e234ee81285a535ab525f9210bf714adfb769806 (patch) | |
| tree | 7b2ffccbd1ecf503cc893a1295b79e55f031255e | |
| parent | 72192792996ccfa39c12d0d7a6594e3531e47568 (diff) | |
| download | external_python_setuptools-e234ee81285a535ab525f9210bf714adfb769806.tar.gz external_python_setuptools-e234ee81285a535ab525f9210bf714adfb769806.tar.bz2 external_python_setuptools-e234ee81285a535ab525f9210bf714adfb769806.zip | |
simplify ContextualZipFile implementation and avoid DeprecationWarnings
ContextualZipFile tried to be smart and have its __new__ method return
zipfile.ZipFile instances on Python versions supporting the zipfile.ZipFile
context manager interface (i.e. only on Python [2.7, 3.0> & 3.2+) while on
others it would return an actual ContextualZipFile instance.
The new implementation seems much simpler and avoids a DeprecationWarning on
Python [3.0, 3.2>. There zipFile.ZipFile's __new__ method implementation is
actually inherited from object, and calling object.__new__ with extra
parameters has been deprecated since Python 2.6 (it has even been converted to
an error since Python 3.3).
Notes on why there were no related problems in other Python versions:
* Python versions prior to 2.5 were not affected because they did not yet
deprecate passing extra parameters to object.__new__.
* Python version 2.6 was not affected because there zipfile.ZipFile was
implemented as an old-style class and therefore did not get its __new__ method
called in the first place.
* Python [2.7, 3.0> & 3.2+ - explained above.
--HG--
extra : source : 9388cc525f1919672fb916b0e62f80ca581072b7
| -rw-r--r-- | ez_setup.py | 23 | ||||
| -rw-r--r-- | pkg_resources.py | 23 |
2 files changed, 20 insertions, 26 deletions
diff --git a/ez_setup.py b/ez_setup.py index df3848a3..810c552b 100644 --- a/ez_setup.py +++ b/ez_setup.py @@ -71,22 +71,19 @@ def _build_egg(egg, archive_filename, to_dir): class ContextualZipFile(zipfile.ZipFile): """ - Supplement ZipFile class to support context manager for Python 2.6 - """ + Supplement ZipFile context manager class supporting all Python versions. + + ZipFile supports a context manager interface only in versions [2.7, 3.0> & + 3.2+. - def __enter__(self): - return self + """ - def __exit__(self, type, value, traceback): - self.close() + if not hasattr(zipfile.ZipFile, '__exit__'): + def __enter__(self): + return self - 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, *args, **kwargs) + def __exit__(self, type, value, traceback): + self.close() @contextlib.contextmanager diff --git a/pkg_resources.py b/pkg_resources.py index 1f8c3183..a9c737c4 100644 --- a/pkg_resources.py +++ b/pkg_resources.py @@ -1561,22 +1561,19 @@ def build_zipmanifest(path): class ContextualZipFile(zipfile.ZipFile): """ - Supplement ZipFile class to support context manager for Python 2.6 - """ + Supplement ZipFile context manager class supporting all Python versions. - def __enter__(self): - return self + ZipFile supports a context manager interface only in versions [2.7, 3.0> & + 3.2+. + + """ - def __exit__(self, type, value, traceback): - self.close() + if not hasattr(zipfile.ZipFile, '__exit__'): + def __enter__(self): + return self - 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, *args, **kwargs) + def __exit__(self, type, value, traceback): + self.close() class ZipProvider(EggProvider): |
