[Twisted-Python] Interface.adaptWith and Interface.__adapt__

Glyph Lefkowitz glyph at twistedmatrix.com
Thu Jun 5 01:59:44 EDT 2003

On Wednesday, June 4, 2003, at 06:10 PM, Phillip J. Eby wrote:

> Anyway...  I noticed that the only code in Twisted currently (that I 
> could find) that actually uses the current __adapt__ setup is the test 
> suite.  Is there any way this might be changeable to something more 
> compatible with PEP 246?

Thanks for bringing this to my attention.  As I was perusing the 
implementation, I realized that the reason we committed this particular 
incompatibility was being invalidated a scant two lines away :-).  I 
could have sworn it didn't used to have a try:except: block, but I've 
checked in a revised version now.

As I understand it, PEP 246 cannot be implemented without raising 
exceptions in __adapt__.  I want to avoid mandatory exceptions at all 
costs within the components system, because in a system such as 
NewReality, where every other method call goes through an interface of 
some kind, the fact that raising exceptions is more than 6x as 
expensive as calling functions becomes a noticeable cost.

> I mean, if I jump through enough hoops building adapters and 
> monkeypatching, I can probably make it so that this will work 
> transparently, and anybody who uses PyProtocols will then be able to 
> mix Twisted, Zope, and PyProtocols interfaces in their applications.  
> But it's pretty messy, and I'm wondering about the copyright status if 
> I have to copy a lot of code from MetaInterface.__call__ in order to 
> monkeypatch a replacement.  (I'd like to release PyProtocols under the 
> PSF license.)

PyProtocols looks like an interesting project, and I'd like to support 
such an effort at interoperability.  Monkey-patching MetaInterface is 
almost certainly the wrong thing to do.  Perhaps it would be best if we 
did not call our interface-aware adaptation mechanism "__adapt__", but 
something like "adaptWithDefault"?

I am not sure exactly how you're doing compatibility, but perhaps then 
you could provide a subclass of Interface that provided an 
adaptWithDefault that called __adapt__ with the proper arguments?

More information about the Twisted-Python mailing list