[Zope3-checkins] CVS: Zope3/lib/python/Persistence - Module.py:1.18

Jeremy Hylton jeremy@zope.com
Fri, 20 Sep 2002 15:22:31 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv6898

Modified Files:
	Module.py 
Log Message:
Reduce indentation in _import().
Add _get_parent() helper to get parent package name.


=== Zope3/lib/python/Persistence/Module.py 1.17 => 1.18 ===
--- Zope3/lib/python/Persistence/Module.py:1.17	Fri Sep 20 14:42:35 2002
+++ Zope3/lib/python/Persistence/Module.py	Fri Sep 20 15:22:30 2002
@@ -180,21 +180,20 @@
         __builtin__.__import__ = self._saved_import
 
     def _import(self, registry, name, parent, fromlist):
-        if parent is not None:
-            pass # XXX
         mod = registry.findModule(name)
-        if mod is not None:
-            if fromlist:
+        if mod is None:
+            return None
+        if fromlist:
+            if isinstance(mod, PersistentPackage):
                 self._import_fromlist(registry, mod, fromlist)
+            return mod
+        else:
+            i = name.find(".")
+            if i == -1:
                 return mod
-            else:
-                i = name.find(".")
-                if i == -1:
-                    return mod
-                top = registry.findModule(name[:i])
-                assert top is not None, "No package for module %s" % name
-                return top
-        return None
+            top = registry.findModule(name[:i])
+            assert top is not None, "No package for module %s" % name
+            return top
 
     def _import_fromlist(self, registry, mod, fromlist):
         for name in fromlist:
@@ -205,11 +204,18 @@
     def __import__(self, name, globals={}, locals={}, fromlist=[]):
         registry = globals.get(__persistent_module_registry__)
         if registry is not None:
-            mod = self._import(registry, name, globals.get("__name__"),
+            mod = self._import(registry, name, self._get_parent(globals),
                                fromlist)
             if mod is not None:
                 return mod
         return self._saved_import(name, globals, locals, fromlist)
+
+    def _get_parent(self, globals):
+        name = globals.get("__name__")
+        if name is None or "." not in name:
+            return None
+        i = name.find(".")
+        return name[:i]
 
 class PersistentModuleRegistry(Persistent):