Commit e234ee81 authored by Jurko Gospodnetić's avatar Jurko Gospodnetić

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
parent 72192792
...@@ -71,22 +71,19 @@ def _build_egg(egg, archive_filename, to_dir): ...@@ -71,22 +71,19 @@ def _build_egg(egg, archive_filename, to_dir):
class ContextualZipFile(zipfile.ZipFile): 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): if not hasattr(zipfile.ZipFile, '__exit__'):
self.close() def __enter__(self):
return self
def __new__(cls, *args, **kwargs): def __exit__(self, type, value, traceback):
""" self.close()
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)
@contextlib.contextmanager @contextlib.contextmanager
......
...@@ -1561,22 +1561,19 @@ def build_zipmanifest(path): ...@@ -1561,22 +1561,19 @@ def build_zipmanifest(path):
class ContextualZipFile(zipfile.ZipFile): 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): ZipFile supports a context manager interface only in versions [2.7, 3.0> &
return self 3.2+.
"""
def __exit__(self, type, value, traceback): if not hasattr(zipfile.ZipFile, '__exit__'):
self.close() def __enter__(self):
return self
def __new__(cls, *args, **kwargs): def __exit__(self, type, value, traceback):
""" self.close()
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)
class ZipProvider(EggProvider): class ZipProvider(EggProvider):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment