cubicweb/__init__.py
changeset 11955 f85ec84355db
parent 11954 e0d708fb20e8
child 12355 c703dc95c82e
--- a/cubicweb/__init__.py	Wed Feb 08 10:31:26 2017 +0100
+++ b/cubicweb/__init__.py	Tue Feb 07 13:47:03 2017 +0100
@@ -24,7 +24,6 @@
 import logging
 import os
 import pickle
-import pkgutil
 import sys
 import types
 import warnings
@@ -283,6 +282,37 @@
 
 # Import hook for "legacy" cubes ##############################################
 
+class _CubesLoader(object):
+
+    def __init__(self, *modinfo):
+        self.modinfo = modinfo
+
+    def load_module(self, fullname):
+        try:
+            # If there is an existing module object named 'fullname' in
+            # sys.modules , the loader must use that existing module.
+            # Otherwise, the reload() builtin will not work correctly.
+            return sys.modules[fullname]
+        except KeyError:
+            pass
+        if fullname == 'cubes':
+            mod = sys.modules[fullname] = types.ModuleType(
+                fullname, doc='CubicWeb cubes')
+        else:
+            modname, file, pathname, description = self.modinfo
+            try:
+                mod = sys.modules[fullname] = imp.load_module(
+                    modname, file, pathname, description)
+            finally:
+                # https://docs.python.org/2/library/imp.html#imp.load_module
+                # Important: the caller is responsible for closing the file
+                # argument, if it was not None, even when an exception is
+                # raised. This is best done using a try ... finally statement
+                if file is not None:
+                    file.close()
+        return mod
+
+
 class _CubesImporter(object):
     """Module finder handling redirection of import of "cubes.<name>"
     to "cubicweb_<name>".
@@ -296,25 +326,12 @@
 
     def find_module(self, fullname, path=None):
         if fullname == 'cubes':
-            return self
-        elif fullname.startswith('cubes.'):
+            return _CubesLoader()
+        elif fullname.startswith('cubes.') and fullname.count('.') == 1:
             modname = 'cubicweb_' + fullname.split('.', 1)[1]
             try:
                 modinfo = imp.find_module(modname)
             except ImportError:
                 return None
             else:
-                return pkgutil.ImpLoader(fullname, *modinfo)
-
-    def load_module(self, fullname):
-        try:
-            # If there is an existing module object named 'fullname' in
-            # sys.modules , the loader must use that existing module.
-            # Otherwise, the reload() builtin will not work correctly.
-            return sys.modules[fullname]
-        except KeyError:
-            pass
-        if fullname != 'cubes':
-            raise ImportError('No module named {0}'.format(fullname))
-        mod = sys.modules[fullname] = types.ModuleType(fullname, doc='CubicWeb cubes')
-        return mod
+                return _CubesLoader(modname, *modinfo)