[Twisted-Python] A kinder and more consistent defer.inlineCallbacks

glyph at divmod.com glyph at divmod.com
Sat Nov 22 22:40:46 EST 2008

On 22 Nov, 05:50 pm, terry at jon.es wrote:
>Agreed.  I also agree with your earlier remarks about dropping the
>isinstance(result, GeneratorType). That leaves me with an alternative:
>    def altInlineCallbacks(f):
>        def unwindGenerator(*args, **kwargs):
>            try:
>                result = f(*args, **kwargs)
>            except Exception, e:
>                # f was not a generator.
>                return failure.Failure()
I hope you mean "defer.fail()".
>and still the problem that _inlineCallbacks raises if result doesn't 
>have a
>send() method, etc. I'm also not sure of the best way to check for 
>but don't think it should be in the _inlineCallbacks loop.

As with the other case we mistakenly diagnosed here, it doesn't actually 
raise.  It returns a failed Deferred.  Consider:

    from twisted.internet.defer import inlineCallbacks
    def notDeferred():
        return object()
    def ok(result):
        ae = result.trap(AttributeError)
        print 'OK', ae

The only thing that (potentially) needs to be done here is to produce a 
more useful error message.  The other case, where inlineCallbacks 
decorates a function that itself raises an exception rather than returns 
an object, is the only way you won't get a Deferred.

More information about the Twisted-Python mailing list