python - Can Deferred.callback() or Deferred.errback() raise an exception to the caller? -
can there condition calling .callback()
or .errback()
raise exception caller won't captured deferred?
say have the following deferred , callbacks:
from twisted.internet import defer def bad_callback(result): raise exception() def bad_errback(result): raise exception() d = defer.deferred() d.addcallbacks(bad_callback, bad_errback)
if call d.callback(none)
, result in d
exception
bad_callback()
. if call d.errback(exception())
, result in d
exception
raised bad_errback()
. but, in either of cases exceptions not raised caller.
now, know of couple conditions calling .callback()
or .errback()
raise exception caller, conditions violate proper use of deferreds.
obviously, if call
.callback()
or.errback()
improper number of arguments, raisetypeerror
.calling called deferred raise
alreadycallederror
.calling
.callback(defer.deferred())
raiseassertionerror
.calling
.errback()
equivalent calling.errback(failure.failure())
raisenocurrentexceptionerror
if there no active exception.
really question comes down to: can safely rely on behavior calling .callback(result)
or .errback(exception_or_failure)
result never raise exception long deferred has not been called , result proper?
i ran example, adding 2 lines bottom:
d.callback(none) print("ok!")
and got output:
unhandled error in deferred: unhandled error traceback (most recent call last): file "callbacks.py", line 11, in <module> d.callback(none) file ".../twisted/internet/defer.py", line 368, in callback self._startruncallbacks(result) file ".../twisted/internet/defer.py", line 464, in _startruncallbacks self._runcallbacks() --- <exception caught here> --- file ".../twisted/internet/defer.py", line 551, in _runcallbacks current.result = callback(current.result, *args, **kw) file "callbacks.py", line 4, in bad_callback raise exception() exceptions.exception: ok!
so in specific case (as have determined yourself), no, exception won't re-raised.
in general case, there few places exceptions effectively propagate out; if have memoryerror
because totally out of memory, it's deferred
implementation allocate little memory somewhere attempting call function or , exception come you.
but risk of programming in python in general; there several exceptions (memoryerror
, keyboardinterrupt
) may arise no warning. if whole process isn't burning down, no, callback
, errback
won't raise exceptions except in cases you've outlined.
Comments
Post a Comment