[Zope3-checkins] SVN: Zope3/trunk/src/zo Fixed failing tests that
were commented out. Found a bug in zodbcode and
Stephan Richter
srichter at cosmos.phy.tufts.edu
Fri Apr 8 13:48:31 EDT 2005
Log message for revision 29918:
Fixed failing tests that were commented out. Found a bug in zodbcode and
fixed it.
Changed:
U Zope3/trunk/src/zodbcode/module.py
U Zope3/trunk/src/zodbcode/tests/test_module.py
U Zope3/trunk/src/zope/app/module/README.txt
U Zope3/trunk/src/zope/app/module/__init__.py
U Zope3/trunk/src/zope/app/module/manager.py
-=-
Modified: Zope3/trunk/src/zodbcode/module.py
===================================================================
--- Zope3/trunk/src/zodbcode/module.py 2005-04-08 16:47:44 UTC (rev 29917)
+++ Zope3/trunk/src/zodbcode/module.py 2005-04-08 17:48:30 UTC (rev 29918)
@@ -230,6 +230,7 @@
if self._saved_import is None:
raise TypeError("Not installed!")
__builtin__.__import__ = self._saved_import
+ self._saved_import = None
def _import(self, registry, name, parent, fromlist):
mod = None
Modified: Zope3/trunk/src/zodbcode/tests/test_module.py
===================================================================
--- Zope3/trunk/src/zodbcode/tests/test_module.py 2005-04-08 16:47:44 UTC (rev 29917)
+++ Zope3/trunk/src/zodbcode/tests/test_module.py 2005-04-08 17:48:30 UTC (rev 29918)
@@ -390,7 +390,6 @@
pmtest.f(4)
self.close()
pmtest._p_deactivate()
- self.importer.uninstall()
self.open()
del pmtest
import pmtest
@@ -406,7 +405,6 @@
transaction.commit()
self.close()
foo._p_deactivate()
- self.importer.uninstall()
self.open()
del foo
import foo
Modified: Zope3/trunk/src/zope/app/module/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/module/README.txt 2005-04-08 16:47:44 UTC (rev 29917)
+++ Zope3/trunk/src/zope/app/module/README.txt 2005-04-08 17:48:30 UTC (rev 29918)
@@ -57,15 +57,14 @@
>>> root_sm = setup.createSiteManager(root)
>>> from zope.app.module import interfaces
- >>> manager = setup.addUtility(root_sm, 'zope.mymodule',
+ >>> manager = setup.addUtility(root_sm, 'mymodule',
... interfaces.IModuleManager, manager)
>>> manager.name
- 'zope.mymodule'
+ 'mymodule'
- # XXX This does not currently work for some reason.
- #>>> manager.getModule().__name__
- #'zope.mymodule'
+ >>> manager.getModule().__name__
+ 'mymodule'
Next, let's ensure that the module's persistence works correctly. To do that
let's create a database and add the root folder to it:
@@ -84,7 +83,7 @@
>>> conn2 = db.open()
>>> root2 = conn2.root()['Application']
>>> module2 = root2.getSiteManager().queryUtility(
- ... interfaces.IModuleManager, 'zope.mymodule').getModule()
+ ... interfaces.IModuleManager, 'mymodule').getModule()
>>> module2.foo
1
>>> module2.bar()
@@ -101,7 +100,7 @@
module registry that uses the registered utilities to look up modules:
>>> from zope.app.module import ZopeModuleRegistry
- >>> ZopeModuleRegistry.findModule('zope.mymodule')
+ >>> ZopeModuleRegistry.findModule('mymodule')
But why did we not get the module back? Because we have not set the site yet:
@@ -111,23 +110,23 @@
Now it will find the module and we can retrieve a list of all persistent
module names:
- >>> ZopeModuleRegistry.findModule('zope.mymodule') is module
+ >>> ZopeModuleRegistry.findModule('mymodule') is module
True
>>> ZopeModuleRegistry.modules()
- ['zope.mymodule']
+ ['mymodule']
Additionally, the package provides two API functions that look up a module in
the registry and then in `sys.modules`:
>>> import zope.app.module
- >>> zope.app.module.findModule('zope.mymodule') is module
+ >>> zope.app.module.findModule('mymodule') is module
True
>>> zope.app.module.findModule('zope.app.module') is zope.app.module
True
The second function can be used to lookup objects inside any module:
- >>> zope.app.module.resolve('zope.mymodule.foo')
+ >>> zope.app.module.resolve('mymodule.foo')
1
>>> zope.app.module.resolve('zope.app.module.foo.resolve')
@@ -137,16 +136,16 @@
>>> event = object()
>>> zope.app.module.installPersistentModuleImporter(event)
>>> __builtins__['__import__'] # doctest: +ELLIPSIS
- <bound method PersistentModuleImporter.__import__ of ...>
+ <bound method ZopePersistentModuleImporter.__import__ of ...>
Now we can simply import the persistent module:
- # XXX This appears to be currently broken!
- #>>> import zope.mymodule
- #>>> zope.mymodule.Blah('my id')
- #Blah('my id')
+ >>> import mymodule
+ >>> mymodule.Blah('my id')
+ Blah(id=my id)
Finally, we unregister the hook again:
>>> zope.app.module.uninstallPersistentModuleImporter(event)
-
+ >>> __builtins__['__import__'] # doctest: +ELLIPSIS
+ <built-in function __import__>
Modified: Zope3/trunk/src/zope/app/module/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/module/__init__.py 2005-04-08 16:47:44 UTC (rev 29917)
+++ Zope3/trunk/src/zope/app/module/__init__.py 2005-04-08 17:48:30 UTC (rev 29918)
@@ -26,7 +26,11 @@
class ZopeModuleRegistry(object):
- """TODO: who am I?"""
+ """Zope-specific registry of persistent modules.
+
+ This registry is used to lookup local module managers and then get the
+ module from them.
+ """
implements(zodbcode.interfaces.IPersistentModuleImportRegistry)
def findModule(self, name):
@@ -55,9 +59,22 @@
return getattr(mod, name[pos+1:], None)
+class ZopePersistentModuleImporter(zodbcode.module.PersistentModuleImporter):
+
+ def __init__(self, registry):
+ self._registry = registry
+
+ def __import__(self, name, globals={}, locals={}, fromlist=[]):
+ mod = self._import(self._registry, name, self._get_parent(globals),
+ fromlist)
+ if mod is not None:
+ return mod
+ return self._saved_import(name, globals, locals, fromlist)
+
+
# Installer function that can be called from ZCML.
# This installs an import hook necessary to support persistent modules.
-importer = zodbcode.module.PersistentModuleImporter()
+importer = ZopePersistentModuleImporter(ZopeModuleRegistry)
def installPersistentModuleImporter(event):
importer.install()
Modified: Zope3/trunk/src/zope/app/module/manager.py
===================================================================
--- Zope3/trunk/src/zope/app/module/manager.py 2005-04-08 16:47:44 UTC (rev 29917)
+++ Zope3/trunk/src/zope/app/module/manager.py 2005-04-08 17:48:30 UTC (rev 29918)
@@ -43,6 +43,7 @@
mod = self._module = zodbcode.module.PersistentModule(self.name)
zodbcode.module.compileModule(mod, ZopeModuleRegistry, self.source)
+ self._module.__name__ = self.name
self._recompile = False
def getModule(self):
More information about the Zope3-Checkins
mailing list