def _deferGenerator(g, deferred):
 result = None
 waiting = [True, None]
 while 1:
  try:
   result = next(g)
  except StopIteration:
   deferred.callback(result)
   return deferred
  except:
   deferred.errback()
   return deferred
  if isinstance(result, Deferred):
   return fail(TypeError('Yield waitForDeferred(d), not d!'))
  if isinstance(result, waitForDeferred):
   def gotResult(r, result=result):
    result.result = r
    if waiting[0]:
     waiting[0] = False
     waiting[1] = r
    else:
     _deferGenerator(g, deferred)
   result.d.addBoth(gotResult)
   if waiting[0]:
    waiting[0] = False
    return deferred
   waiting[0] = True
   waiting[1] = None
   result = None

def _deferGenerator ( g , deferred ) : result = None waiting = [ True , None ] while 1 : try : result = next ( g ) except StopIteration : deferred . callback ( result ) return deferred except : deferred . errback ( ) return deferred if isinstance ( result , Deferred ) : return fail ( TypeError ( 'Yield waitForDeferred(d), not d!' ) ) if isinstance ( result , waitForDeferred ) : def gotResult ( r , result = result ) : result . result = r if waiting [ 0 ] : waiting [ 0 ] = False waiting [ 1 ] = r else : _deferGenerator ( g , deferred ) result . d . addBoth ( gotResult ) if waiting [ 0 ] : waiting [ 0 ] = False return deferred waiting [ 0 ] = True waiting [ 1 ] = None result = None
see l{deferredgenerator} .