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

glyph at divmod.com glyph at divmod.com
Fri Nov 21 22:27:52 EST 2008


On 03:18 am, glyph at divmod.com wrote:
>
>On 21 Nov, 06:04 pm, terry at jon.es wrote:
>>Here's a suggestion for making inlineCallbacks more consistent and 
>>less
>>confusing.  Let's suppose you're writing something like this:
>
>inlineCallbacks appears to have a bug: 'raise' before 'yield' in a 
>generator results in a synchronous exception rather than an errback, 
>although its documentation does not explain this.

Ugh, scratch that.  No, it doesn't have this bug.  When I read this:
>>2. func might raise before it get to its first yield. In that case 
>>you'll
>>get an exception thrown when the inlineCallbacks decorator tries to 
>>create
>>the wrapper function:

I did some quick testing and saw some tracebacks, but apparently wasn't 
paying very close attention to them; they were unhandled-traceback-in- 
deferred tracebacks, and they *did* have the application code in 
question on the stack.  Run this and observe:

    from twisted.internet.defer import succeed, inlineCallbacks, 
returnValue
    @inlineCallbacks
    def f():
        result = 1 / 0
        yield returnValue(succeed(result))
    print f().addErrback(lambda err: err.trap(ZeroDivisionError) and 2)

If func "raises before it gets to its first yield", we get the right 
behavior.  If it just raises and doesn't yield *anywhere*, then it's not 
a generator and that's the same as your other case of accidentally-not- 
returning-a-generator.  Still worth debugging, but not as serious.




More information about the Twisted-Python mailing list