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

fijal at codespeak.net fijal at codespeak.net
Tue Apr 1 19:39:57 CEST 2008


Author: fijal
Date: Tue Apr  1 19:39:57 2008
New Revision: 53237

Modified:
   pypy/dist/pypy/module/zipimport/interp_zipimport.py
   pypy/dist/pypy/module/zipimport/test/test_zipimport.py
Log:
.prefix attribute for zipimporter.


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	Tue Apr  1 19:39:57 2008
@@ -25,11 +25,15 @@
      (False, False, '.py')])
 
 class W_ZipImporter(Wrappable):
-    def __init__(self, space, name, w_dir, w_zipfile):
+    def __init__(self, space, name, w_dir, w_zipfile, prefix):
         self.space = space
         self.name = name
         self.w_dir = w_dir
         self.w_zipfile = w_zipfile
+        self.prefix = prefix
+
+    def getprefix(space, self):
+        return space.wrap(self.prefix)
 
     def import_py_file(self, space, modname, filename, w_buf, pkgpath):
         buf = space.str_w(w_buf)
@@ -220,6 +224,10 @@
     if not ok:
         raise OperationError(space.w_ImportError, space.wrap(
             "Did not find %s to be a valid zippath" % (name,)))
+    last_elem = filename.split(os.path.sep)[-1]
+    stop = len(filename)-len(last_elem)
+    assert stop > 0
+    prefix = filename[:stop]
     w_import = space.builtin.get('__import__')
     w_zipfile = space.call(w_import, space.newlist([
         space.wrap('zipfile'),
@@ -232,7 +240,7 @@
     except OperationError, e: # we catch everything as this function
         raise OperationError(space.w_ImportError, space.wrap(
             "%s seems not to be a zipfile" % (filename,)))
-    w_result = space.wrap(W_ZipImporter(space, name, w_dir, w_zipfile))
+    w_result = space.wrap(W_ZipImporter(space, name, w_dir, w_zipfile, prefix))
     space.setitem(w_zip_cache, space.wrap(name), w_result)
     return w_result
     
@@ -248,4 +256,5 @@
     is_package  = interp2app(W_ZipImporter.is_package),
     load_module = interp2app(W_ZipImporter.load_module),
     archive     = GetSetProperty(W_ZipImporter.getarchive),
+    prefix      = GetSetProperty(W_ZipImporter.getprefix),
 )

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	Tue Apr  1 19:39:57 2008
@@ -227,7 +227,10 @@
         # value.  Not sure why it doesn't the assertion uses import.archive
         # directly. -exarkun
         archive = importer.archive
+        allbutlast = self.zipfile.split(os.path.sep)[:-1]
+        prefix = os.path.sep.join(allbutlast + [''])
         assert archive == self.zipfile
+        assert importer.prefix == prefix
 
 
 class AppTestZipimportDeflated(AppTestZipimport):



More information about the Pypy-commit mailing list