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

Terry Jones terry at jon.es
Sat Nov 22 22:58:54 EST 2008


>>>>> "glyph" == glyph  <glyph at divmod.com> writes:
glyph> On 22 Nov, 05:50 pm, terry at jon.es wrote:
>> def altInlineCallbacks(f):
>>   def unwindGenerator(*args, **kwargs):
>>     try:
>>       result = f(*args, **kwargs)
>>     except Exception, e:
>>       # f was not a generator.
>>       return failure.Failure()

glyph> I hope you mean "defer.fail()".

Yes, sorry.

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

Yes, that was my original case, passing a non-generator and getting an
attribute error when _inlineCallbacks calls send. That one I knew goes back
via the Deferred.

glyph> The only thing that (potentially) needs to be done here is to
glyph> produce a more useful error message.

glyph> The other case, where inlineCallbacks decorates a function that
glyph> itself raises an exception rather than returns an object, is the
glyph> only way you won't get a Deferred.

That's what I was addressing in the code above. If you call the function in
unwindGenerator and you get an exception, you 1) know it's not a generator
(that's what I didn't understand earlier - calling a function with a yield
in it can never give you an exception, Python builds you a generator and
gives you that) and 2) can immediately give the exception back via
defer.fail (your correction above).  That's a simple change and makes sure
you always get a Deferred back.

Terry




More information about the Twisted-Python mailing list