[Twisted-Python] Interfaces and adapters

en.karpachov at ospaz.ru en.karpachov at ospaz.ru
Fri Aug 26 06:09:10 EDT 2005


On Fri, 26 Aug 2005 18:40:07 +1000
Andrew Bennetts wrote:

> On Fri, Aug 26, 2005 at 12:00:39PM +0400, en.karpachov at ospaz.ru wrote:
> > 
> > If I, say, adapt WovenContext to ISession, I get a session object which
> > does not implement the ISession interface itself. Absurdly.
> 
> Yep.  But that's a trivially fixable bug in that adapter.  And this is

Should it be considered as a bug then?

> exactly why raising an exception would be bad -- it will break code that
> otherwise works fine.  Even though such code is arguably buggy, Twisted

One could say that the code in question is already broken. At least, the
exception would point to the programmer that they forgot to declare what
interfaces are implemented by the adapter.

> tries reasonably hard not to break backwards compatibility without warning.
> We couldn't raise CannotAdapt here without first giving DeprecationWarnings
> for at least one release.

Well, if raising CannotAdapt is not appropriate, then probably some
decorator to AdapterFactory could help? Something like (pseudocode):

def affirm(adapterFactory, interface):

     def safe_factory(orig):
          adapter = adapterFactory(orig)
          if not interface.providedBy(adapter):
               warn("Failed to adapt")
               adapter = MakeTheObjectToProvide(adapter, interface)
          return adapter

     return safe_factory

adapterFactory = affirm(adapterFactory, interface)

-- 
jk




More information about the Twisted-Python mailing list