[Twisted-Python] Notes on switching twisted to new zope.interface code

1. Switch classes from: class C: __implements__ = IFoo, to: class C: zope.interface.implements(IFoo) and if you have third party subclasses that may depend on C having __implements__, also add: twisted.python.components.backwardsCompatImplements(C) 2. Using zope.interface APIs directly is better, e.g. IFoo.providedBy(o) rather than components.implements(o, IFoo). But! If the object is third party, it may be using __implements__, and if the class never got processed by t.p.c it will not be hooked up to zope.interface at all. So, you may want to do: t.p.componets.fixClassImplements(o.__class__) this will make sure that __implements__ declarations get converted to new style implements declarations. 3. Basically all of t.p.c is deprecated except for registerAdapter and the other APIs that talk to registry. Componentized is not, in theory, but hopefully will be replaced soon. -- Itamar Shtull-Trauring http://itamarst.org

On Sun, Jul 18, 2004 at 10:39:24PM -0400, Itamar Shtull-Trauring wrote:
What about subclasses? class C: __implements__ = IFoo, class D(C): __implements__ = (C.__implements__, IFoo) Do Zope interfaces need anything special to handle this? -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_

On Mon, Jul 19, 2004 at 01:26:42AM -0400, Alex Levy wrote: [...]
(Did you really mean to use IFoo twice in your example?) See the Zope interfaces package documentation for details, but this: class C: __implements__ = (IFoo,) class D: __implements__ = (C.__implements__, IBar) would become this: from zope.interface import implements class C: implements(IFoo) class D: implements(IBar) i.e. implements adds to the list of interfaces the class implements. There's also an "implementsOnly" function. See README.txt from the zope.interface package for more details: http://svn.zope.org/Zope3/trunk/src/zope/interface/README.txt?rev=13888&view=auto -Andrew.

On Mon, Jul 19, 2004 at 03:43:21PM +1000, Andrew Bennetts wrote:
(Did you really mean to use IFoo twice in your example?)
Hah, no -- that's what happens when I ask questions after 1am EST. :P Thanks for the clarification. -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_

Itamar Shtull-Trauring wrote:
Sorry, but can you please explain : 1) What is t.p.c and 2) Why would one want to use Zope's interfaces in places of Twisted built-in interfaces regards, Eugene Coetzee -- =============================================== Reedflute Software Solutions Web -> www.reedflute.com ===============================================

On Mon, 2004-07-19 at 16:31, Eugene Coetzee wrote:
1) What is t.p.c and
twisted.python.components
2) Why would one want to use Zope's interfaces in places of Twisted built-in interfaces
Next version of Twisted will use Zope's interfaces, so my comments were for developers so they can help port the code. -- Itamar Shtull-Trauring http://itamarst.org

I get the following error when trying to import the SOAP support :- File "./cacheserver/main.py", line 16, in ? from twisted.web import xmlrpc, server, resource, soap File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/site-packages/twisted/web/soap.py", line 32, in ? import SOAPpy ImportError: No module named SOAPpy Is there an additional lib that I need to download and install? JT

John Toohey wrote:
Yes. Twisted's support is mainly just an integration layer between the SOAP parser/generator that SOAPpy provides and Twisted's own I/O layers. I believe the required module is available on sourceforge: http://pywebsvcs.sourceforge.net/ Jp

Maybe this should appear in the list of dependencies for the download at http://twistedmatrix.com/products/download. On Tue, 20 Jul 2004 15:57:52 -0400, Jp Calderone <exarkun@divmod.com> wrote:

Is there an additional lib that I need to download and install?
Yes, you have to download ad install SOAPpy from here http://pywebsvcs.sourceforge.net/ bye -- Lawrence "in IE we trust"

On Jul 20, 2004, at 3:40 PM, John Toohey wrote:
Yes, google is your friend: http://pywebsvcs.sourceforge.net/ -bob

Itamar Shtull-Trauring wrote:
Thanks for the clarification. I am new to Python, Twisted and Zope - so I guess I look at the world through my Perlish C++ tainted goggles :) Just one or two comments - 30 000 feet from above. I have noticed the introduction of Zope interfaces in other projects as well where some developers are almost going bonkers with interfaces as "the new best thing" and where simple inheritance is being discarded in places where they should really be used. Don't think that is a good thing. IMHO - interfaces should be implented on the "boundries" of a component - i.e. those interfaces which form part of the API of a package. I get the impression that in Zope3 they are (ab)used to try and make an untyped language behave more like a typed language. This may work well in an app where performance is not a major issue - but in an networking framework the abuse of interfaces may lead to an unnecessary and significant performance hit. regards, Eugene -- -- =============================================== Web -> www.reedflute.com ===============================================

On Jul 21, 2004, at 5:04 PM, Eugene Coetzee wrote:
One important thing to note: The zope interfaces package is not being used to replace inheritance (That already happened years ago). It is being used to replace the twisted interfaces module, which was slower and buggy. From my perspective the switch is an unambiguously good thing. The only issues are in the migration path. But that's mostly worked out now thanks to itamar. Just in case what itamar said was too lengthy, here's the shortened version: First -- all your old code *will continue to work with no changes*. However, to eliminate DeprecationWarnings and cause your code to be faster, make the following change: twisted.python.components.implements(o, IFoo) => IFoo.providedBy(o) class X(): __implements__ = IFoo, => class X(): zope.interface.implements(IFoo) If you are modifying code in Twisted itself, you have to do slightly more, so that 3rd party classes relying on __implements__ to work will continue to function: twisted.python.components.implements(o, IFoo) => twisted.python.components.fixClassImplements(o.__class__); IFoo.providedBy(o) class X(): __implements__ = IFoo, => class X(): zope.interface.implements(IFoo); twisted.python.components.backwardsCompatImplements(X) James

One more thing: Before we have the next release of Twisted, all classes in Twisted that declare their implemented interfaecs using __implements__ *must* be replaced by the z.i.implements()/backwardsCompatImplements pair. If we don't do this, we can't tell third parties to use the zope.interfaces functionality directly. The t.p.c.implements() should also be replaced in order to not have DeprecationWarnings spew from twisted itself, but that isn't quite so important. James

On Sun, Jul 18, 2004 at 10:39:24PM -0400, Itamar Shtull-Trauring wrote: [...]
4. If you use zope.interface.providedBy, and you want to work with code that might not have been updated yet, call components.fixClassImplements, e.g.: components.fixClassImplements(obj) for i in interface.providedBy(obj): ... I just fixed a bug in newcred where it didn't do this, so it broke a bunch of PB tests. Please be careful to test your changes work with old classes that still use __implements__, not to mention make sure you run the test suite! -Andrew.

On Sun, Jul 18, 2004 at 10:39:24PM -0400, Itamar Shtull-Trauring wrote:
What about subclasses? class C: __implements__ = IFoo, class D(C): __implements__ = (C.__implements__, IFoo) Do Zope interfaces need anything special to handle this? -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_

On Mon, Jul 19, 2004 at 01:26:42AM -0400, Alex Levy wrote: [...]
(Did you really mean to use IFoo twice in your example?) See the Zope interfaces package documentation for details, but this: class C: __implements__ = (IFoo,) class D: __implements__ = (C.__implements__, IBar) would become this: from zope.interface import implements class C: implements(IFoo) class D: implements(IBar) i.e. implements adds to the list of interfaces the class implements. There's also an "implementsOnly" function. See README.txt from the zope.interface package for more details: http://svn.zope.org/Zope3/trunk/src/zope/interface/README.txt?rev=13888&view=auto -Andrew.

On Mon, Jul 19, 2004 at 03:43:21PM +1000, Andrew Bennetts wrote:
(Did you really mean to use IFoo twice in your example?)
Hah, no -- that's what happens when I ask questions after 1am EST. :P Thanks for the clarification. -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_

Itamar Shtull-Trauring wrote:
Sorry, but can you please explain : 1) What is t.p.c and 2) Why would one want to use Zope's interfaces in places of Twisted built-in interfaces regards, Eugene Coetzee -- =============================================== Reedflute Software Solutions Web -> www.reedflute.com ===============================================

On Mon, 2004-07-19 at 16:31, Eugene Coetzee wrote:
1) What is t.p.c and
twisted.python.components
2) Why would one want to use Zope's interfaces in places of Twisted built-in interfaces
Next version of Twisted will use Zope's interfaces, so my comments were for developers so they can help port the code. -- Itamar Shtull-Trauring http://itamarst.org

I get the following error when trying to import the SOAP support :- File "./cacheserver/main.py", line 16, in ? from twisted.web import xmlrpc, server, resource, soap File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/site-packages/twisted/web/soap.py", line 32, in ? import SOAPpy ImportError: No module named SOAPpy Is there an additional lib that I need to download and install? JT

John Toohey wrote:
Yes. Twisted's support is mainly just an integration layer between the SOAP parser/generator that SOAPpy provides and Twisted's own I/O layers. I believe the required module is available on sourceforge: http://pywebsvcs.sourceforge.net/ Jp

Maybe this should appear in the list of dependencies for the download at http://twistedmatrix.com/products/download. On Tue, 20 Jul 2004 15:57:52 -0400, Jp Calderone <exarkun@divmod.com> wrote:

Is there an additional lib that I need to download and install?
Yes, you have to download ad install SOAPpy from here http://pywebsvcs.sourceforge.net/ bye -- Lawrence "in IE we trust"

On Jul 20, 2004, at 3:40 PM, John Toohey wrote:
Yes, google is your friend: http://pywebsvcs.sourceforge.net/ -bob

Itamar Shtull-Trauring wrote:
Thanks for the clarification. I am new to Python, Twisted and Zope - so I guess I look at the world through my Perlish C++ tainted goggles :) Just one or two comments - 30 000 feet from above. I have noticed the introduction of Zope interfaces in other projects as well where some developers are almost going bonkers with interfaces as "the new best thing" and where simple inheritance is being discarded in places where they should really be used. Don't think that is a good thing. IMHO - interfaces should be implented on the "boundries" of a component - i.e. those interfaces which form part of the API of a package. I get the impression that in Zope3 they are (ab)used to try and make an untyped language behave more like a typed language. This may work well in an app where performance is not a major issue - but in an networking framework the abuse of interfaces may lead to an unnecessary and significant performance hit. regards, Eugene -- -- =============================================== Web -> www.reedflute.com ===============================================

On Jul 21, 2004, at 5:04 PM, Eugene Coetzee wrote:
One important thing to note: The zope interfaces package is not being used to replace inheritance (That already happened years ago). It is being used to replace the twisted interfaces module, which was slower and buggy. From my perspective the switch is an unambiguously good thing. The only issues are in the migration path. But that's mostly worked out now thanks to itamar. Just in case what itamar said was too lengthy, here's the shortened version: First -- all your old code *will continue to work with no changes*. However, to eliminate DeprecationWarnings and cause your code to be faster, make the following change: twisted.python.components.implements(o, IFoo) => IFoo.providedBy(o) class X(): __implements__ = IFoo, => class X(): zope.interface.implements(IFoo) If you are modifying code in Twisted itself, you have to do slightly more, so that 3rd party classes relying on __implements__ to work will continue to function: twisted.python.components.implements(o, IFoo) => twisted.python.components.fixClassImplements(o.__class__); IFoo.providedBy(o) class X(): __implements__ = IFoo, => class X(): zope.interface.implements(IFoo); twisted.python.components.backwardsCompatImplements(X) James

One more thing: Before we have the next release of Twisted, all classes in Twisted that declare their implemented interfaecs using __implements__ *must* be replaced by the z.i.implements()/backwardsCompatImplements pair. If we don't do this, we can't tell third parties to use the zope.interfaces functionality directly. The t.p.c.implements() should also be replaced in order to not have DeprecationWarnings spew from twisted itself, but that isn't quite so important. James

On Sun, Jul 18, 2004 at 10:39:24PM -0400, Itamar Shtull-Trauring wrote: [...]
4. If you use zope.interface.providedBy, and you want to work with code that might not have been updated yet, call components.fixClassImplements, e.g.: components.fixClassImplements(obj) for i in interface.providedBy(obj): ... I just fixed a bug in newcred where it didn't do this, so it broke a bunch of PB tests. Please be careful to test your changes work with old classes that still use __implements__, not to mention make sure you run the test suite! -Andrew.
participants (10)
-
Alex Levy
-
Andrew Bennetts
-
Bob Ippolito
-
Eugene Coetzee
-
Itamar Shtull-Trauring
-
James Y Knight
-
John Toohey
-
Jp Calderone
-
Justin Johnson
-
Lawrence Oluyede