[Twisted-Python] inlineCallbacks loses custom exception type?

Tom Prince tom.prince at ualberta.net
Wed Dec 5 23:00:31 EST 2012


Using the slightly simplified code (nothing uses the reactor, so do
stuff synchronosuly):

import sys, traceback
from twisted.internet import defer
 
class MyException(Exception):
  pass
 
def go2():
    "An ordinary deferred-returning function that will return a deferred which is ready to errback()"
    try:
        raise MyException('xyz')
    except Exception:
        return defer.fail() # errs back with current exception context
 
    return defer.succeed(1) # unreachable code
 
@defer.inlineCallbacks
def go_inlinecb():
    try:
        yield go2()
        print 'no exception raised'
    except MyException:
        print 'got my custom exception'
        traceback.print_exc()
    except Exception:
        print 'got a garden variety exception'
        traceback.print_exc()
 
def go_noinlinecb():
    d = go2()
    def cb(data):
        print 'got data in cb: %s' % (data)
    def eb(failure):
        try:
            failure.raiseException()
        except MyException:
            print 'got my custom exception'
            traceback.print_exc()
        except Exception:
            print 'got a garden variety exception'
            traceback.print_exc()
    d.addCallbacks(cb, eb)
 
if sys.argv[1] == 'inline':
    go_inlinecb()
elif sys.argv[1] == 'noinline':
    go_noinlinecb()


I get the follwing results:

% python test.py noinline
got my custom exception
Traceback (most recent call last):
  File "test.py", line 36, in eb
    failure.raiseException()
  File "/usr/lib64/python2.7/site-packages/twisted/python/failure.py", line 370, in raiseException
    raise self.type, self.value, self.tb
MyException: xyz

% python test.py inline  
got my custom exception
Traceback (most recent call last):
  File "test.py", line 21, in go_inlinecb
    yield go2()
MyException: xyz


This is with
% python --version
Python 2.7.3
% python -c 'import twisted; print twisted.__version__'
12.2.0

  Tom




More information about the Twisted-Python mailing list