[Twisted-Python] Forcing adapter re-registration (patch)
jml at ids.org.au
jml at ids.org.au
Tue Apr 15 05:00:38 MDT 2003
Woven code uses registerAdapter alot. When developing Woven code, it's
extremely handy to have a couple of 'rebuild' calls in your RPY. (If these
aren't there, the web server must be restarted for code canges to take effect)
However, registerAdapter and rebuild don't play nicely together. On a rebuild,
registerAdapter tries to re-register stuff, and throws an exception.
Below is a patch that adds an optional keyword parameter 'force' to the
registerAdapter method. If force is set to a true value, registerAdapter should
happily re-register stuff. Otherwise, the current behaviour holds.
The tests work, and I'll quite happily commit if no-one has any problems.
cheers,
jml
===================================================================
RCS file: /cvs/Twisted/twisted/test/test_components.py,v
diff -u -r1.26 components.py
--- twisted/python/components.py 5 Apr 2003 00:28:37 -0000 1.26
+++ twisted/python/components.py 15 Apr 2003 00:32:30 -0000
@@ -115,7 +115,8 @@
# mapping between (<class>, <interface>) and <adapter class>
self.adapterRegistry = {}
- def registerAdapter(self, adapterClass, origClass, *interfaceClasses):
+ def registerAdapter(self, adapterClass, origClass, *interfaceClasses,
+ **kwarg):
"""Register an adapter class.
An adapter class is expected to implement the given interface, by
@@ -124,7 +125,8 @@
"""
assert interfaceClasses, "You need to pass an Interface"
for interfaceClass in interfaceClasses:
- if self.adapterRegistry.has_key((origClass, interfaceClass)):
+ if (self.adapterRegistry.has_key((origClass, interfaceClass))
+ and not kwarg.get('force')):
raise ValueError(
"an adapter (%s) was already registered." % (
self.adapterRegistry[(origClass, interfaceClass)]
===================================================================
RCS file: /cvs/Twisted/twisted/test/test_components.py,v
retrieving revision 1.8
diff -u -r1.8 test_components.py
--- twisted/test/test_components.py 5 Jan 2003 02:39:22 -0000 1.8
+++ twisted/test/test_components.py 15 Apr 2003 00:39:11 -0000
@@ -284,3 +284,13 @@
# should fail because we already registered an IMultiply adapter for IntAdder
self.assertRaises(ValueError, components.registerAdapter, IntMultiplyWithAdder, IntAdder, IMultiply)
+
+ def testForcedRegister(self):
+ # should not fail, despite re-registering, because we are forcing
+ # it to re-register.
+ try:
+ components.registerAdapter(IntMultiplyWithAdder, IntAdder, IMultiply, force=1)
+ except ValueError:
+ self.fail("Should have forced re-registration")
+
+
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20030415/167ce612/attachment.sig>
More information about the Twisted-Python
mailing list