[Zope3-dev] Re: adapter registration question
Tres Seaver
tseaver at palladion.com
Fri Nov 17 08:45:30 EST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris Withers wrote:
> Tres Seaver wrote:
>> We were discussion a (notional) "value adapter", whose factory would
>> have the contract of returning an object of a specific concrete type,
>> rather than on implementing an interface.
>
> Right, but the CA supports the use of classes instead of interfaces and
> that works just fine here...
>
>> There are reasonable usecases
>> for such adapters, but the CA machinery doesn't permit registering them:
>> it requires that the 'provided' argument be either an interface or None
>> (although the 'required' args can be concrete types).
>
> Nah, for both "provides" and "adapts" classes can be used in place of
> interfaces just fine, and I've got this working just how I want it now :-)
This doctest blows up on my machine when run against the Zope3 trunk:
>>> from zope.component import getAdapter
>>> from zope.component import provideAdapter
>>> from zope.interface import Interface
>>> from zope.interface import implements
>>> class IFoo(Interface):
... pass
>>> class Foo(object):
... implements(IFoo)
>>> foo = Foo()
>>> def get_a(x):
... return 'a'
>>> def get_b(x):
... return 'b'
>>> provideAdapter(get_a, adapts=(IFoo,), provides=str, name='a')
>>> provideAdapter(get_b, adapts=(IFoo,), provides=str, name='b')
>>> getAdapter(object=foo, interface=str, name='a')
'a'
>>> getAdapter(object=foo, interface=str, name='b')
'b'
The first 'provideAdapter' call raises an exeption because 'str' doesn't
have an '__iro__'. The second *doesn't* raise, because the code which
raised the first exception leaves an artifact in a module global.
Bot lookups fail with 'ComponentLookupError'.
Tres.
- --
===================================================================
Tres Seaver +1 202-558-7113 tseaver at palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFXbz6+gerLs4ltQ4RAhbKAJ9nIr2GI3MPNygbmNuqiPmYLU5aLwCgxv5G
pb6cW4/y9gjzQdN5nDuz6kQ=
=Os0S
-----END PGP SIGNATURE-----
More information about the Zope3-dev
mailing list