@@ -113,7 +113,7 @@ def raise_exception(self):
113
113
raise self .type (self .value ).with_traceback (self .traceback )
114
114
115
115
else :
116
- exec ("""def raise_exception(self):
116
+ exec ("""def raise_exception(self):
117
117
raise self.type, self.value, self.traceback""" )
118
118
119
119
def catch (self , * errors ):
@@ -258,6 +258,8 @@ def add_callbacks(self, callback, errback=None,
258
258
if self .called :
259
259
self ._next ()
260
260
261
+ return self
262
+
261
263
def add_errback (self , func , * args , ** kwargs ):
262
264
"""Add a callable (function or method) to the errback chain only.
263
265
@@ -288,8 +290,8 @@ def add_errback(self, func, *args, **kwargs):
288
290
>>> deferred.result
289
291
'ignored'
290
292
"""
291
- self .add_callbacks (_passthrough , func , errback_args = args ,
292
- errback_kwargs = kwargs )
293
+ return self .add_callbacks (_passthrough , func , errback_args = args ,
294
+ errback_kwargs = kwargs )
293
295
294
296
def add_callback (self , func , * args , ** kwargs ):
295
297
"""Add a callable (function or method) to the callback chain only.
@@ -312,8 +314,8 @@ def add_callback(self, func, *args, **kwargs):
312
314
>>> deferred.result
313
315
2
314
316
"""
315
- self .add_callbacks (func , _passthrough , callback_args = args ,
316
- callback_kwargs = kwargs )
317
+ return self .add_callbacks (func , _passthrough , callback_args = args ,
318
+ callback_kwargs = kwargs )
317
319
318
320
def errback (self , error = None ):
319
321
"""Start processing the errorback chain starting with the
@@ -339,6 +341,7 @@ def errback(self, error=None):
339
341
elif not isinstance (error , DeferredException ):
340
342
assert isinstance (error , Exception )
341
343
error = DeferredException (error .__class__ , error , None )
344
+
342
345
self .called = True
343
346
self .result = error
344
347
self ._next ()
@@ -358,16 +361,28 @@ def callback(self, result=None):
358
361
if self .called :
359
362
raise AlreadyCalledDeferred ()
360
363
self .called = True
364
+
365
+ if isinstance (result , Deferred ):
366
+ self .paused = True
367
+ return result .add_callbacks (self ._continue , self ._continue )
368
+
361
369
self .result = result
362
370
self ._next ()
363
371
364
372
def _continue (self , result ):
365
373
"""Continue processing the Deferred with the given result."""
374
+ # If the result of the deferred is another deferred, we will need to wait for
375
+ # it to resolve again.
376
+ if isinstance (result , Deferred ):
377
+ return result .add_callbacks (self ._continue , self ._continue )
378
+
366
379
self .result = result
367
380
self .paused = False
368
381
if self .called :
369
382
self ._next ()
370
383
384
+ return result
385
+
371
386
def _next (self ):
372
387
"""Process the next callback."""
373
388
if self ._running or self .paused :
@@ -380,18 +395,19 @@ def _next(self):
380
395
callback , args , kwargs = next_pair [isinstance (self .result ,
381
396
DeferredException )]
382
397
383
- if callback is _passthrough :
384
- continue
398
+ if callback is not _passthrough :
399
+ self ._running = True
400
+ try :
401
+ self .result = callback (self .result , * args , ** kwargs )
385
402
386
- self ._running = True
387
- try :
388
- self .result = callback (self .result , * args , ** kwargs )
403
+ except :
404
+ self .result = DeferredException ()
389
405
390
- except :
391
- self .result = DeferredException ()
406
+ finally :
407
+ self ._running = False
392
408
393
- finally :
394
- self ._running = False
409
+ if isinstance ( self . result , Exception ) :
410
+ self .result = DeferredException ( self . result )
395
411
396
412
if isinstance (self .result , Deferred ):
397
413
# If a Deferred was returned add this deferred as callbacks to
@@ -428,12 +444,15 @@ def defer(func, *args, **kwargs):
428
444
True
429
445
"""
430
446
assert isinstance (func , collections .Callable )
447
+
431
448
try :
432
449
result = func (* args , ** kwargs )
433
450
except :
434
451
result = DeferredException ()
452
+
435
453
if isinstance (result , Deferred ):
436
454
return result
455
+
437
456
deferred = Deferred ()
438
457
deferred .callback (result )
439
458
return deferred
0 commit comments