[Zope3-checkins] CVS: Zope3/lib/python/Persistence - Module.py:1.15
Jeremy Hylton
jeremy@zope.com
Fri, 20 Sep 2002 11:21:06 -0400
Update of /cvs-repository/Zope3/lib/python/Persistence
In directory cvs.zope.org:/tmp/cvs-serv14533/Persistence
Modified Files:
Module.py
Log Message:
Add support for package __init__ modules.
Also extend _import signature to handle __name__. This will be used
to support package-local imports.
=== Zope3/lib/python/Persistence/Module.py 1.14 => 1.15 ===
--- Zope3/lib/python/Persistence/Module.py:1.14 Thu Sep 19 23:51:47 2002
+++ Zope3/lib/python/Persistence/Module.py Fri Sep 20 11:21:05 2002
@@ -56,8 +56,13 @@
state["__builtins__"] = __builtin__
self.__dict__.update(state)
+# TODO for persistent packages:
+# handle package-local imports
+
class PersistentPackage(PersistentModule):
+ # XXX Is it okay that these packages don't have __path__?
pass
+
__persistent_module_registry__ = "__persistent_module_registry__"
@@ -78,12 +83,14 @@
raise ValueError, "module already exists"
if "." in name:
parent = self._package(name)
- else:
- parent = None
- self._module = PersistentModule(name)
- if parent is not None:
modname = name.split(".")[-1]
- setattr(parent, modname, self._module)
+ if modname == "__init__":
+ self._module = parent
+ else:
+ self._module = PersistentModule(name)
+ setattr(parent, modname, self._module)
+ else:
+ self._module = PersistentModule(name)
try:
self._registry.setModule(name, self._module)
except ValueError:
@@ -130,6 +137,8 @@
parts = name.split(".")[:-1]
parent = None
for i in range(len(parts)):
+ if parts[i] == "__init__":
+ raise ValueError, "__init__ can not be a package"
pname = ".".join(parts[:i+1])
package = self._registry.findModule(pname)
if package is None:
@@ -154,7 +163,7 @@
def uninstall(self):
__builtin__.__import__ = self._saved_import
- def _import(self, registry, name, fromlist):
+ def _import(self, registry, name, parent, fromlist):
mod = registry.findModule(name)
if mod is not None:
if fromlist:
@@ -171,7 +180,8 @@
def __import__(self, name, globals={}, locals={}, fromlist=[]):
registry = globals.get(__persistent_module_registry__)
if registry is not None:
- mod = self._import(registry, name, fromlist)
+ mod = self._import(registry, name, globals.get("__name__"),
+ fromlist)
if mod is not None:
return mod
return self._saved_import(name, globals, locals, fromlist)