[pypy-svn] pypy default: Catch raw BadZipfile in zipimport module,

amauryfa commits-noreply at bitbucket.org
Mon Mar 7 17:47:44 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42463:5ba25cc48026
Date: 2011-03-07 17:44 +0100
http://bitbucket.org/pypy/pypy/changeset/5ba25cc48026/

Log:	Catch raw BadZipfile in zipimport module, don't let a corrupt zip
	file crash the interpreter

diff --git a/pypy/module/zipimport/interp_zipimport.py b/pypy/module/zipimport/interp_zipimport.py
--- a/pypy/module/zipimport/interp_zipimport.py
+++ b/pypy/module/zipimport/interp_zipimport.py
@@ -246,7 +246,7 @@
             fname = filename + ext
             try:
                 buf = self.zip_file.read(fname)
-            except (KeyError, OSError):
+            except (KeyError, OSError, BadZipfile):
                 pass
             else:
                 if is_package:
@@ -277,7 +277,7 @@
         try:
             data = self.zip_file.read(filename)
             return w(data)
-        except (KeyError, OSError):
+        except (KeyError, OSError, BadZipfile):
             raise OperationError(space.w_IOError, space.wrap("Error reading file"))
 
     @unwrap_spec(fullname=str)
@@ -345,8 +345,6 @@
 @unwrap_spec(name=str)
 def descr_new_zipimporter(space, w_type, name):
     w = space.wrap
-    w_ZipImportError = space.getattr(space.getbuiltinmodule('zipimport'),
-                                     w('ZipImportError'))
     ok = False
     parts_ends = [i for i in range(0, len(name))
                     if name[i] == os.path.sep or name[i] == ZIPSEP]
@@ -359,18 +357,18 @@
         try:
             s = os.stat(filename)
         except OSError:
-            raise operationerrfmt(w_ZipImportError,
+            raise operationerrfmt(get_error(space),
                 "Cannot find name %s", filename)
         if not stat.S_ISDIR(s.st_mode):
             ok = True
             break
     if not ok:
-        raise operationerrfmt(w_ZipImportError,
+        raise operationerrfmt(get_error(space),
             "Did not find %s to be a valid zippath", name)
     try:
         w_result = zip_cache.get(filename)
         if w_result is None:
-            raise operationerrfmt(w_ZipImportError,
+            raise operationerrfmt(get_error(space),
                 "Cannot import %s from zipfile, recursion detected or"
                 "already tried and failed", name)
     except KeyError:
@@ -378,7 +376,7 @@
     try:
         zip_file = RZipFile(filename, 'r')
     except (BadZipfile, OSError):
-        raise operationerrfmt(w_ZipImportError,
+        raise operationerrfmt(get_error(space),
             "%s seems not to be a zipfile", filename)
     prefix = name[len(filename):]
     if prefix.startswith(os.path.sep) or prefix.startswith(ZIPSEP):

diff --git a/pypy/module/zipimport/__init__.py b/pypy/module/zipimport/__init__.py
--- a/pypy/module/zipimport/__init__.py
+++ b/pypy/module/zipimport/__init__.py
@@ -9,7 +9,7 @@
     interpleveldefs = {
         'zipimporter':'interp_zipimport.W_ZipImporter',
         '_zip_directory_cache' : 'space.wrap(interp_zipimport.zip_cache)',
-        'ZipImportError': 'space.fromcache(interp_zipimport.Cache).w_error',
+        'ZipImportError': 'interp_zipimport.get_error(space)',
     }
 
     appleveldefs = {


More information about the Pypy-commit mailing list