[Twisted-Python] eated traceback when decorate method already decorated by inlineCallbacks

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Mar 14 10:20:46 EDT 2013


On 09:40 am, sergun at gmail.com wrote:
>Hello to all!
>
>Why traceback doesn't include call of g() here?

Tracebacks are tricky, and inlineCallbacks doesn't make them look 
exactly like they would look if you didn't have Deferreds.

twisted.python.failure is a little bit better at handling tracebacks 
from this sort of code than the traceback module is, so if you write the 
code like this things will at least be a little better:

    from twisted.python.failure import Failure

    def decor1():
        try:
            res = yield f(*argv, **kw)
        except:
            Failure.printTraceback()
        else:
            defer.returnValue(res)

Notice also that I moved returnValue outside of the try block. 
`returnValue` is implemented using exceptions, so you *must not* catch 
and handle the exception it raises or it won't work.

Jean-Paul
>import traceback
>from twisted.internet import defer, reactor
>
>def decor1(f, *argv, **kw):
>    @defer.inlineCallbacks
>    def decor1():
>        try:
>            res = yield f(*argv, **kw)
>            defer.returnValue(res)
>        except:
>            traceback.print_exc()
>    return decor1
>
>@defer.inlineCallbacks
>def g():
>    raise AttributeError
>    yield defer.succeed(1)
>
>@decor1
>@defer.inlineCallbacks
>def f():
>    yield g()
>
>f()
>reactor.run()
>
>I see just:
>Traceback (most recent call last):
>  File "C:\testing\test.py", line 9, in decor1
>    res = yield f(*argv, **kw)
>AttributeError
>
>
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python



More information about the Twisted-Python mailing list