[pypy-svn] r61366 - in pypy/trunk/pypy: module/zipimport module/zipimport/test rlib

afa at codespeak.net afa at codespeak.net
Mon Jan 26 18:07:08 CET 2009


Author: afa
Date: Mon Jan 26 18:07:07 2009
New Revision: 61366

Modified:
   pypy/trunk/pypy/module/zipimport/interp_zipimport.py
   pypy/trunk/pypy/module/zipimport/test/test_undocumented.py
   pypy/trunk/pypy/rlib/rzipfile.py
Log:
Necessary changes in zipimport for windows:
- the zip file is not kept opened. Like CPython, we cache the directory info, and reopen it every time
- ensure that the underlying file is always closed.
- open the file in binary mode.
- zip files use / as separator, not os.path.sep


Modified: pypy/trunk/pypy/module/zipimport/interp_zipimport.py
==============================================================================
--- pypy/trunk/pypy/module/zipimport/interp_zipimport.py	(original)
+++ pypy/trunk/pypy/module/zipimport/interp_zipimport.py	Mon Jan 26 18:07:07 2009
@@ -16,9 +16,9 @@
 # separators, we need to pretend that we had the os.sep.
 
 ENUMERATE_EXTS = unrolling_iterable(
-    [(True, True, os.path.sep + '__init__.pyc'),
-     (True, True, os.path.sep + '__init__.pyo'),
-     (False, True, os.path.sep + '__init__.py'),
+    [(True, True, ZIPSEP + '__init__.pyc'),
+     (True, True, ZIPSEP + '__init__.pyo'),
+     (False, True, ZIPSEP + '__init__.py'),
      (True, False, '.pyc'),
      (True, False, '.pyo'),
      (False, False, '.py')])
@@ -43,13 +43,10 @@
         except KeyError:
             raise OperationError(space.w_KeyError, space.wrap(name))
         assert isinstance(w_zipimporter, W_ZipImporter)
-        dir = w_zipimporter.dir
-        keys = [k.filename for k in dir.filelist]
         w = space.wrap
         values = {}
         w_d = space.newdict()
-        for key in keys:
-            info = dir.NameToInfo[key]
+        for key, info in w_zipimporter.dir.iteritems():
             w_values = space.newdict()
             space.setitem(w_d, w(key), space.newtuple([
                 w(info.filename), w(info.compress_type), w(info.compress_size),
@@ -118,9 +115,10 @@
 zip_cache = W_ZipCache()
 
 class W_ZipImporter(Wrappable):
-    def __init__(self, space, name, dir, prefix):
+    def __init__(self, space, name, filename, dir, prefix):
         self.space = space
         self.name = name
+        self.filename = filename
         self.dir = dir
         self.prefix = prefix
         self.w_ZipImportError = space.getattr(
@@ -131,8 +129,8 @@
         return space.wrap(self.prefix)
 
     def _find_relative_path(self, filename):
-        if filename.startswith(self.dir.filename):
-            filename = filename[len(self.dir.filename):]
+        if filename.startswith(self.filename):
+            filename = filename[len(self.filename):]
         if filename.startswith(os.sep):
             filename = filename[1:]
         if ZIPSEP != os.path.sep:
@@ -152,10 +150,9 @@
     def _parse_mtime(self, space, filename):
         w = space.wrap
         try:
-            info = self.dir.getinfo(filename)
+            info = self.dir[filename]
             t = info.date_time
-        except (BadZipfile, KeyError):
-            # in either case, this is a fallback
+        except KeyError:
             return 0
         else:
             w_mktime = space.getattr(space.getbuiltinmodule('time'),
@@ -203,7 +200,7 @@
             filename = filename.replace(os.path.sep, ZIPSEP)
         w = space.wrap
         try:
-            self.dir.getinfo(filename)
+            self.dir[filename]
             return True
         except KeyError:
             return False
@@ -216,7 +213,7 @@
     find_module.unwrap_spec = ['self', ObjSpace, str, W_Root]
 
     def mangle(self, name):
-        return name.replace('.', os.path.sep)
+        return name.replace('.', ZIPSEP)
 
     def load_module(self, space, fullname):
         w = space.wrap
@@ -230,7 +227,11 @@
         for compiled, is_package, ext in ENUMERATE_EXTS:
             fname = filename + ext
             try:
-                buf = self.dir.read(fname)
+                zip_file = RZipFile(self.filename, 'r')
+                try:
+                    buf = zip_file.read(fname)
+                finally:
+                    zip_file.close()
             except (KeyError, OSError):
                 pass
             else:
@@ -259,7 +260,12 @@
         filename = self._find_relative_path(filename)
         w = space.wrap
         try:
-            return w(self.dir.read(filename))
+            zip_file = RZipFile(self.filename, 'r')
+            try:
+                data = zip_file.read(filename)
+            finally:
+                zip_file.close()
+            return w(data)
         except (KeyError, OSError):
             raise OperationError(space.w_IOError, space.wrap("Error reading file"))
     get_data.unwrap_spec = ['self', ObjSpace, str]
@@ -307,7 +313,7 @@
 
     def getarchive(space, self):
         space = self.space
-        return space.wrap(self.dir.filename)
+        return space.wrap(self.filename)
 
 def descr_new_zipimporter(space, w_type, name):
     w = space.wrap
@@ -341,14 +347,16 @@
     except KeyError:
         zip_cache.cache[filename] = None
     try:
-        dir = RZipFile(filename, 'r')
+        zip_file = RZipFile(filename, 'r')
     except (BadZipfile, OSError):
         raise OperationError(w_ZipImportError, space.wrap(
             "%s seems not to be a zipfile" % (filename,)))
+    zip_file.close()
     prefix = name[len(filename):]
     if prefix.startswith(os.sep):
         prefix = prefix[1:]
-    w_result = space.wrap(W_ZipImporter(space, name, dir, prefix))
+    w_result = space.wrap(W_ZipImporter(space, name, filename,
+                                        zip_file.NameToInfo, prefix))
     zip_cache.set(filename, w_result)
     return w_result
 

Modified: pypy/trunk/pypy/module/zipimport/test/test_undocumented.py
==============================================================================
--- pypy/trunk/pypy/module/zipimport/test/test_undocumented.py	(original)
+++ pypy/trunk/pypy/module/zipimport/test/test_undocumented.py	Mon Jan 26 18:07:07 2009
@@ -87,13 +87,11 @@
         test_file = open(TESTFN, 'w')
         try:
             test_file.write("# Test file for zipimport.")
-            try:
-                raises(zipimport.ZipImportError,
-                        zipimport.zipimporter, TESTFN)
-            finally:
-                os.unlink(TESTFN)
+            raises(zipimport.ZipImportError,
+                   zipimport.zipimporter, TESTFN)
         finally:
             test_file.close()
+            os.unlink(TESTFN)
 
     def test_root(self):
         import zipimport, os
@@ -139,7 +137,8 @@
             importer = zipimport.zipimporter(os.path.join(zip_path, '_pkg'))
             assert zip_path in zipimport._zip_directory_cache
             file_set = set(zipimport._zip_directory_cache[zip_path].iterkeys())
-            compare_set = set(path + '.py' for path in self.created_paths)
+            compare_set = set(path.replace(os.path.sep, '/') + '.py'
+                              for path in self.created_paths)
             assert file_set == compare_set
         finally:
             self.cleanup_zipfile(self.created_paths)

Modified: pypy/trunk/pypy/rlib/rzipfile.py
==============================================================================
--- pypy/trunk/pypy/rlib/rzipfile.py	(original)
+++ pypy/trunk/pypy/rlib/rzipfile.py	Mon Jan 26 18:07:07 2009
@@ -147,8 +147,14 @@
         self.mode = mode
         self.filelist = []
         self.NameToInfo = {}
+        if 'b' not in mode:
+            mode += 'b'
         fp = open_file_as_stream(zipname, mode, 1024)
-        self._GetContents(fp)
+        try:
+            self._GetContents(fp)
+        except:
+            fp.close()
+            raise
         self.fp = fp
 
     def _GetContents(self, fp):
@@ -241,4 +247,7 @@
         if crc != zinfo.CRC:
             raise BadZipfile, "Bad CRC-32 for file %s" % filename
         return bytes
+
+    def close(self):
+        self.fp.close()
     



More information about the Pypy-commit mailing list