[pypy-svn] r55443 - in pypy/branch/build-external/pypy/module/zipimport: . test

afa at codespeak.net afa at codespeak.net
Sat May 31 15:30:16 CEST 2008


Author: afa
Date: Sat May 31 15:30:13 2008
New Revision: 55443

Modified:
   pypy/branch/build-external/pypy/module/zipimport/interp_zipimport.py
   pypy/branch/build-external/pypy/module/zipimport/test/test_zipimport.py
Log:
Correct a bit zipimport for windows:
normalize path separators to '/',
and (like CPython) search with the full name first, removing elements until
a directory or a regular file is found. 

This handles better the root dir case ("C:" is not a directory), and should be faster as well.


Modified: pypy/branch/build-external/pypy/module/zipimport/interp_zipimport.py
==============================================================================
--- pypy/branch/build-external/pypy/module/zipimport/interp_zipimport.py	(original)
+++ pypy/branch/build-external/pypy/module/zipimport/interp_zipimport.py	Sat May 31 15:30:13 2008
@@ -309,21 +309,26 @@
     w = space.wrap
     w_ZipImportError = space.getattr(space.getbuiltinmodule('zipimport'),
                                      w('ZipImportError'))
+
+    if os.path.sep != '/':
+        filename = name.replace(os.path.sep, '/')
+    else:
+        filename = name
     ok = False
-    parts = name.split(os.path.sep)
-    filename = "" # make annotator happy
-    for i in range(1, len(parts) + 1):
-        filename = os.path.sep.join(parts[:i])
-        if not filename:
-            filename = os.path.sep
+    while True:
         try:
             s = os.stat(filename)
         except OSError:
-            raise OperationError(w_ZipImportError, space.wrap(
-                "Cannot find name %s" % (filename,)))
-        if not stat.S_ISDIR(s.st_mode):
+            # back up one path element
+            pos = filename.rfind('/')
+            if pos == -1:
+                break
+            filename = filename[:pos]
+            continue
+
+        if stat.S_ISREG(s.st_mode):
             ok = True
-            break
+        break
     if not ok:
         raise OperationError(w_ZipImportError, space.wrap(
             "Did not find %s to be a valid zippath" % (name,)))

Modified: pypy/branch/build-external/pypy/module/zipimport/test/test_zipimport.py
==============================================================================
--- pypy/branch/build-external/pypy/module/zipimport/test/test_zipimport.py	(original)
+++ pypy/branch/build-external/pypy/module/zipimport/test/test_zipimport.py	Sat May 31 15:30:13 2008
@@ -94,13 +94,14 @@
         """)
 
     def test_cache(self):
+        import os
         self.writefile(self, 'x.py', 'y')
         from zipimport import _zip_directory_cache, zipimporter
         new_importer = zipimporter(self.zipfile)
         try:
             assert zipimporter(self.zipfile) is new_importer
         finally:
-            del _zip_directory_cache[self.zipfile]
+            del _zip_directory_cache[self.zipfile.replace(os.path.sep, '/')]
 
     def test_good_bad_arguments(self):
         from zipimport import zipimporter



More information about the Pypy-commit mailing list