[Zope3-dev] Patch for NamedAdapters

Sidnei da Silva sidnei@x3ng.com
Tue, 11 Mar 2003 14:36:29 -0300


--jRHKVT23PllUwdXP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,

I was digging the deepness of Named Adapters this evening, and found
one or two bugs. Im not sure why the test I wrote refuses to work, but
I added some comments close to the failing lines so someone can find
out whats wrong.

The patch is attached.

[]'s
-- 
Sidnei da Silva (dreamcatcher) <sidnei@x3ng.com.br>
X3ng Web Technology <http://www.x3ng.com.br>
GNU/Linux user 257852
Debian GNU/Linux 3.0 (Sid) 2.4.18 ppc

From: Ean Schuessler <ean@novare.net>
The unrecognized minister of propaganda,
E
	-- Debian, joking

--jRHKVT23PllUwdXP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="adapters_patch.diff"

Index: src/zope/app/services/adapter.py
===================================================================
RCS file: /cvs-repository/Zope3/src/zope/app/services/adapter.py,v
retrieving revision 1.11
diff -r1.11 adapter.py
117d116
<         if adapters:
118a118
>         if adapters:
132c132
<         return adapters.queryAdapter(object, interface, default)
---
>         return adapters.queryAdapter(object, interface, default, name)
Index: src/zope/app/services/tests/test_adapter.py
===================================================================
RCS file: /cvs-repository/Zope3/src/zope/app/services/tests/test_adapter.py,v
retrieving revision 1.4
diff -r1.4 test_adapter.py
32a33
> from zope.app.component.nextservice import getNextService
69a71,73
> class AB:
>     def __init__(self, object):
>         self.context = object
182c186,189
<         self.assertEqual(service.getAdapter(o, I1, u"Yatta!"), o)
---
>         # Humm... 'o' implements 'I1', but isnt a named adapter
>         # named 'Yatta!'. So, this should raise a ComponentLookupError
>         self.assertRaises(ComponentLookupError, service.getAdapter,
>                           o, I1, "Yatta!")
183a191
>         self.assertEqual(service.queryAdapter(o, I1, None, u"Yatta!"), None)
192,193c200,202
< 
<         self.assertEqual(service.queryAdapter(o, I1, None, u"Yatta!"), o)
---
>                 
>         # Same as above, but should return the default value instead
>         self.assertEqual(service.queryAdapter(o, I1, None, u"Yatta!"), None)
198a208,298
> 
> 
>     def test_getAdapter_with_name_and_delegation(self):
>         # The same as above, but with a named adapter
>         # using a local adapter service
>         service1 = self._service
> 
>         folder1 = traverse(self.rootFolder, 'folder1')
>         folder1.setServiceManager(ServiceManager())
> 
>         sm1 = traverse(self.rootFolder, '++etc++Services')
>         default = traverse(sm1, 'Packages/default')
> 
>         configure = traverse(sm1, 'Packages/default/configure')
>         configuration = Configuration()
>         configuration.adapterName = u"Yatta!"
>         configure.setObject('', configuration)
>         configuration = traverse(configure, '1')
> 
>         configuration.factory = A
> 
>         registry = service1.createConfigurationsFor(configuration)
>         registry.register(configuration)
>         registry.activate(configuration)
> 
>         sm2 = traverse(folder1, '++etc++Services')
>         default = traverse(sm2, 'Packages/default')
>         service2 = ContextWrapper(AdapterService(), folder1)
> 
>         configure = traverse(sm2, 'Packages/default/configure')
>         configuration = Configuration()
>         configuration.adapterName = u"Yotta!"
>         configure.setObject('', configuration)
>         configuration = traverse(configure, '1')
> 
>         configuration.factory = AB
> 
>         registry = service2.createConfigurationsFor(configuration)
>         registry.register(configuration)
>         registry.activate(configuration)
> 
>         # At this point, getNextService should return service1, but this isnt working.
>         # Jim said that it could be bad context wrapping, but I dont think so.
>         # To me, it looks like getNextService *always* does getNextServiceManager,
>         # so it will always check only one service in each ServiceManager, and
>         # in this case, it finds the Global Adapter Service first than the local one.
>         # self.assertEqual(service1, getNextService(service2, 'Adapters'))
> 
>         class O:
>             __implements__ = I1
> 
>         o = O()
> 
>         for r in I1, I1E:
>             for p in I2B, I2:
>                 o = O()
>                 o.__implements__ = r
> 
>                 adapter = service1.getAdapter(o, p, u"Yatta!")
>                 self.assertEqual(adapter.__class__, A)
>                 self.assertEqual(adapter.context, o)
> 
>                 # Here, it should fail with the current AdapterService
>                 # and then try to do a lookup on the next service
>                 # which is service1 above. This should work, but is
>                 # failing currently.
>                 # adapter = service2.getAdapter(o, p, u"Yatta!")
>                 # self.assertEqual(adapter.__class__, A)
>                 # self.assertEqual(adapter.context, o)
> 
>         for r in I1, I1E:
>             for p in I2B, I2:
>                 o = O()
>                 o.__implements__ = r
> 
>                 adapter = service1.queryAdapter(o, p, None, u"Yatta!")
>                 self.assertEqual(adapter.__class__, A)
>                 self.assertEqual(adapter.context, o)
>                 # Same as above.
>                 # adapter = service2.queryAdapter(o, p, None, u"Yatta!")
>                 # self.assertEqual(adapter.__class__, A)
>                 # self.assertEqual(adapter.context, o)
> 
>         self.assertEqual(service2.queryAdapter(o, I1, None, u"Yatta!"), None)
> 
>         self.assertRaises(ComponentLookupError, service2.getAdapter,
>                           O(), I3, "Yatta!")
> 
>         self.assertEqual(service2.queryAdapter(o, I3, name=u"Yatta!"), None)
>         self.assertEqual(service2.queryAdapter(o, I3, 42, u"Yatta!"), 42)
> 

--jRHKVT23PllUwdXP--