[Twisted-web] Problem using deferreds in a freeform autocallable

Gavrie Philipson twisted-web@twistedmatrix.com
Thu, 25 Dec 2003 13:43:27 +0200


This is a multi-part message in MIME format.
--------------010006010600020205070306
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I have a freeform autocallable method which uses a deferred: The method 
needs to call a function that returns a deferred, and continue only 
after the deferred fires.

Unfortunately, I cannot get it to work: When I return a deferred from 
the autocallable, I get a traceback which doesn't make sense to me.

A test program and the associated traceback are attached.

Is this supposed to work? If not, how can I perform a deferred action 
when posting a form?

Thanks,

-- Gavrie.

--------------010006010600020205070306
Content-Type: text/plain;
 name="formpost-defer.tac"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="formpost-defer.tac"

from twisted.application import service, internet
from twisted.internet import defer

from nevow import appserver
from nevow import renderer
from nevow import tags
from nevow import formless
from nevow import freeform


class IMyForm(formless.TypedInterface):
    def submit(self,
        foo=formless.String(),
    ): pass

    submit = formless.autocallable(submit)


class MyForm(object):
    __implements__ = IMyForm

    def submit(self, foo):
        print "Submit:", foo

        def _cb(result):
            print "Callback: %s" % result
            return result
        
        d = defer.Deferred()
        d.callback("bar")
        return d


class FormPage(renderer.Renderer):

    document = tags.html[
        tags.body[
            freeform.configure
        ]
    ]

application = service.Application("test")
internet.TCPServer(
    8081, 
    appserver.NevowSite(
        FormPage(MyForm())
    )
).setServiceParent(application)

# vim: ft=python

--------------010006010600020205070306
Content-Type: text/plain;
 name="traceback"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="traceback"

2003/12/25 13:36 IST [-] Log opened.
2003/12/25 13:36 IST [-] twistd 1.1.0 (/usr/bin/python 2.3.2) starting up
2003/12/25 13:36 IST [-] reactor class: twisted.internet.default.SelectReactor
2003/12/25 13:36 IST [-] Loading formpost-defer.tac...
2003/12/25 13:36 IST [-] /usr/lib/python2.3/site-packages/atop/store.py:36: exceptions.DeprecationWarning: This module is deprecated.
2003/12/25 13:36 IST [-] TODO: add IComponentized to Twisted so that we can do this without relying on getattr magic.
2003/12/25 13:37 IST [-] Loaded.
2003/12/25 13:37 IST [-] nevow.appserver.NevowSite starting on 8081
2003/12/25 13:37 IST [-] Starting factory <nevow.appserver.NevowSite instance at 0xbf1c00ac>
2003/12/25 13:37 IST [-] Main loop terminated.
2003/12/25 13:37 IST [-] Server Shut Down.
2003/12/25 13:37 IST [-] Log opened.
2003/12/25 13:37 IST [-] twistd 1.1.0 (/usr/bin/python 2.3.2) starting up
2003/12/25 13:37 IST [-] reactor class: twisted.internet.default.SelectReactor
2003/12/25 13:37 IST [-] Loading formpost-defer.tac...
2003/12/25 13:37 IST [-] /usr/lib/python2.3/site-packages/atop/store.py:36: exceptions.DeprecationWarning: This module is deprecated.
2003/12/25 13:37 IST [-] TODO: add IComponentized to Twisted so that we can do this without relying on getattr magic.
2003/12/25 13:37 IST [-] Loaded.
2003/12/25 13:37 IST [-] nevow.appserver.NevowSite starting on 8081
2003/12/25 13:37 IST [-] Starting factory <nevow.appserver.NevowSite instance at 0xbf1c10ac>
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] Submit: aaa
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] /usr/lib/python2.3/site-packages/nevow/formless.py:514: exceptions.FutureWarning: %u/%o/%x/%X of negative int will return a signed string in Python 2.4 and up
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] /usr/lib/python2.3/site-packages/twisted/internet/defer.py:357: exceptions.FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4 and up
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] Traceback (most recent call last):
	  File "/usr/lib/python2.3/site-packages/twisted/protocols/http.py", line 549, in requestReceived
	    self.process()
	  File "/usr/lib/python2.3/site-packages/nevow/appserver.py", line 115, in process
	    return self.site.getResourceFor(self).addCallback(
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 187, in addCallback
	    callbackKeywords=kw)
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 178, in addCallbacks
	    self._runCallbacks()
	--- <exception caught here> ---
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 307, in _runCallbacks
	    self.result = callback(self.result, *args, **kw)
	  File "/usr/lib/python2.3/site-packages/nevow/appserver.py", line 124, in finishRender
	    self.write(html)
	  File "/usr/lib/python2.3/site-packages/twisted/protocols/http.py", line 677, in write
	    self.sentLength = self.sentLength + len(data)
	exceptions.TypeError: len() of unsized object
	
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] /usr/lib/python2.3/site-packages/twisted/web/server.py:266: exceptions.UserWarning: Warning! request.finish called twice.
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] Unhandled error in Deferred:
2003/12/25 13:37 IST [HTTPChannel,0,127.0.0.1] Traceback (most recent call last):
	  File "/usr/lib/python2.3/site-packages/twisted/protocols/http.py", line 549, in requestReceived
	    self.process()
	  File "/usr/lib/python2.3/site-packages/nevow/appserver.py", line 115, in process
	    return self.site.getResourceFor(self).addCallback(
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 187, in addCallback
	    callbackKeywords=kw)
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 178, in addCallbacks
	    self._runCallbacks()
	--- <exception caught here> ---
	  File "/usr/lib/python2.3/site-packages/twisted/internet/defer.py", line 307, in _runCallbacks
	    self.result = callback(self.result, *args, **kw)
	  File "/usr/lib/python2.3/site-packages/nevow/appserver.py", line 124, in finishRender
	    self.write(html)
	  File "/usr/lib/python2.3/site-packages/twisted/protocols/http.py", line 677, in write
	    self.sentLength = self.sentLength + len(data)
	exceptions.TypeError: len() of unsized object
	
2003/12/25 13:37 IST [-] Main loop terminated.
2003/12/25 13:37 IST [-] Server Shut Down.

--------------010006010600020205070306--