[Zope3-Users] Adapting a builtin?

Paul Winkler pw_lists at slinkp.com
Mon Dec 12 18:06:36 EST 2005


On Wed, Dec 07, 2005 at 10:23:14AM -0500, Jim Fulton wrote:
> It is valid both to:
> 
> - Declare an interface for a built-in type and
>   register adapters for that interface, and to
> 
> - Register adapters for builtin types directly.
> 
> I can't promise that this works now, but it ought to work.
> I'm 90% sure that it does in fact work, at least in Zope 3.

Neither one appears to work in a Zope2 trunk checkout.
Am I doing something wrong?

In lib/python/Products/StandardCacheManagers, I have:

######### configure.zcml #######################

<configure
    xmlns="http://namespaces.zope.org/zope">

  <adapter for=".interfaces.IString"
    provides=".interfaces.IHTTPConnection"
    factory="httplib.HTTPConnection"
  />

<class class="str">
   <implements interface=".interfaces.IString" />
</class>

############  interfaces.py ###########################

from zope.interface import Interface, Attribute

class IHTTPConnection(Interface):

    """Represents an HTTP connection.
    """

    def request(method, path):
        """Send an HTTP request.
        """

    def getresponse():
        """Get a list of strings representing a response.
        """

class IString(Interface):

    """Dummy interface to let me adapt strings
    """

    # XXX what should be in this?


############# toward the end of
# tests/testtests/test_AcceleratedHTTPCacheManager.py #############

    def test_XXXinterface(self):
        url = 'http://www.google.com'
        from Products.StandardCacheManagers.interfaces import IHTTPConnection
        conn = IHTTPConnection(url)

#####################################################################


This raises an error, regardless of whether I remove the registration of 
IString or not:

pw at kermit Zope-Trunk $ python test.py -vv -m AcceleratedH
Running tests at level 1
Running unit tests:
  Running:
    test_PURGE_passes_Host_header
(Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests)
    test_XXXinterface
(Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests)

Error in test test_XXXinterface
(Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests)
Traceback (most recent call last):
  File "/usr/lib/python2.4/unittest.py", line 260, in run
    testMethod()
  File
"/home/pw/Downloads/Apps/Net/Zope-Trunk/lib/python/Products/StandardCacheManagers/tests/test_AcceleratedHTTPCacheManager.py",
line 100, in test_XXXinterface
    conn = IHTTPConnection(url)
  File
"/home/pw/Downloads/Apps/Net/Zope-Trunk/lib/python/zope/interface/interface.py",
line 682, in __call__
    raise TypeError("Could not adapt", obj, self)
TypeError: ('Could not adapt', 'http://www.google.com', <InterfaceClass
Products.StandardCacheManagers.interfaces.IHTTPConnection>)


  Ran 2 tests with 0 failures and 1 errors in 0.261 seconds.

Tests with errors:
   test_XXXinterface
(Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests)





-- 

Paul Winkler
http://www.slinkp.com


More information about the Zope3-users mailing list