[pypy-svn] r47868 - in pypy/dist/pypy/module/zipimport: . test

fijal at codespeak.net fijal at codespeak.net
Wed Oct 24 23:28:50 CEST 2007


Author: fijal
Date: Wed Oct 24 23:28:47 2007
New Revision: 47868

Modified:
   pypy/dist/pypy/module/zipimport/interp_zipimport.py
   pypy/dist/pypy/module/zipimport/test/test_zipimport.py
Log:
A present for twisted folks - implement obscure and undocumented behavior
of cpython.


Modified: pypy/dist/pypy/module/zipimport/interp_zipimport.py
==============================================================================
--- pypy/dist/pypy/module/zipimport/interp_zipimport.py	(original)
+++ pypy/dist/pypy/module/zipimport/interp_zipimport.py	Wed Oct 24 23:28:47 2007
@@ -170,12 +170,24 @@
         return zip_importer_cache[name]
     except KeyError:
         pass
-    try:
-        s = os.stat(name)
-    except OSError:
-        return space.w_None
-    if stat.S_ISDIR(s.st_mode):
-        return space.w_None
+    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
+        try:
+            s = os.stat(filename)
+        except OSError:
+            raise OperationError(space.w_ImportError, space.wrap(
+                "Cannot find name %s" % (filename,)))
+        if not stat.S_ISDIR(s.st_mode):
+            ok = True
+            break
+    if not ok:
+        raise OperationError(space.w_ImportError, space.wrap(
+            "Did not find %s to be a valid zippath" % (name,)))
     w_import = space.builtin.get('__import__')
     w_zipfile = space.call(w_import, space.newlist([
         space.wrap('zipfile'),
@@ -184,12 +196,12 @@
         space.newlist([])]))
     w_ZipFile = space.getattr(w_zipfile, space.wrap('ZipFile'))
     try:
-        w_dir = space.call(w_ZipFile, space.newlist([space.wrap(name)]))
+        w_dir = space.call(w_ZipFile, space.newlist([space.wrap(filename)]))
     except OperationError: # we catch everything as this function
-        # should not raise
-        return space.w_None
+        raise OperationError(space.w_ImportError, space.wrap(
+            "%s seems not to be a zipfile" % (filename,)))
     result = space.wrap(W_ZipImporter(space, name, w_dir, w_zipfile))
-    zip_importer_cache[name] = result
+    zip_importer_cache[filename] = result
     return result
     
 descr_new_zipimporter.unwrap_spec = [ObjSpace, W_Root, str]

Modified: pypy/dist/pypy/module/zipimport/test/test_zipimport.py
==============================================================================
--- pypy/dist/pypy/module/zipimport/test/test_zipimport.py	(original)
+++ pypy/dist/pypy/module/zipimport/test/test_zipimport.py	Wed Oct 24 23:28:47 2007
@@ -88,6 +88,21 @@
             sys.path.pop(0)
         """)
 
+    def test_good_bad_arguments(self):
+        from zipimport import zipimporter
+        import os
+        self.writefile(self, "x.py", "y")
+        zipimporter(self.zipfile) # should work
+        raises(ImportError, "zipimporter(os.path.dirname(self.zipfile))")
+        raises(ImportError, 'zipimporter("fsafdetrssffdsagadfsafdssadasa")')
+        name = os.path.join(os.path.dirname(self.zipfile), "x.zip")
+        f = open(name, "w")
+        f.write("zzz")
+        f.close()
+        raises(ImportError, 'zipimporter(name)')
+        # this should work as well :-/
+        zipimporter(os.path.join(self.zipfile, 'x'))
+
     def test_py(self):
         import sys, os
         self.writefile(self, "uuu.py", "def f(x): return x")



More information about the Pypy-commit mailing list