[pypy-commit] pypy py3k: zipimport namespace pkg support, hopefully

pjenvey pypy.commits at gmail.com
Fri May 20 21:57:12 EDT 2016


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r84549:812de889087b
Date: 2016-05-20 18:55 -0700
http://bitbucket.org/pypy/pypy/changeset/812de889087b/

Log:	zipimport namespace pkg support, hopefully

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
@@ -364,6 +364,28 @@
         space = self.space
         return space.wrap_fsdecoded(self.filename)
 
+    def _find_loader(self, space, fullname):
+        filename = self.make_filename(fullname)
+        for _, _, ext in ENUMERATE_EXTS:
+            if self.have_modulefile(space, filename + ext):
+                return True, None
+        # See if this is a directory (part of a namespace pkg)
+        dirpath = self.prefix + fullname
+        if self.have_modulefile(space, dirpath + ZIPSEP):
+            return True, self.filename + os.path.sep + self.corr_zname(dirpath)
+        return False, None
+
+    @unwrap_spec(fullname='str0')
+    def find_loader(self, space, fullname, w_path=None):
+        found, ns_portion = self._find_loader(space, fullname)
+        if not found:
+            result = [space.w_None, space.newlist([])]
+        elif not ns_portion:
+            result = [self, space.newlist([])]
+        else:
+            result = [space.w_None, space.newlist([space.wrap(ns_portion)])]
+        return space.newtuple(result)
+
 def descr_new_zipimporter(space, w_type, w_name):
     name = space.fsencode_w(w_name)
     ok = False
@@ -422,6 +444,7 @@
     get_filename = interp2app(W_ZipImporter.get_filename),
     is_package  = interp2app(W_ZipImporter.is_package),
     load_module = interp2app(W_ZipImporter.load_module),
+    find_loader = interp2app(W_ZipImporter.find_loader),
     archive     = GetSetProperty(W_ZipImporter.getarchive),
     prefix      = GetSetProperty(W_ZipImporter.getprefix),
 )
diff --git a/pypy/module/zipimport/test/test_zipimport.py b/pypy/module/zipimport/test/test_zipimport.py
--- a/pypy/module/zipimport/test/test_zipimport.py
+++ b/pypy/module/zipimport/test/test_zipimport.py
@@ -440,6 +440,12 @@
         self.writefile('x1test/__init__.py', 'raise ValueError')
         raises(ValueError, __import__, 'x1test', None, None, [])
 
+    def test_namespace_pkg(self):
+        self.writefile('foo/', '')
+        self.writefile('foo/one.py', "attr = 'portion1 foo one'\n")
+        foo = __import__('foo.one', None, None, [])
+        assert foo.one.attr == 'portion1 foo one'
+
 
 if os.sep != '/':
     class AppTestNativePathSep(AppTestZipimport):


More information about the pypy-commit mailing list