def _deferGenerator(g, deferred=None):
result = None<EOL>while <NUM_LIT:1>:<EOL><INDENT>if deferred is None:<EOL><INDENT>deferred = defer.Deferred()<EOL><DEDENT>try:<EOL><INDENT>result = g.next()<EOL><DEDENT>except StopIteration:<EOL><INDENT>deferred.callback(result)<EOL>return deferred<EOL><DEDENT>except:<EOL><INDENT>deferred.errback()<EOL>return deferred<EOL><DEDENT>if isinstance(result, defer.Deferred):<EOL><INDENT>return defer.fail(TypeError("<STR_LIT>"))<EOL><DEDENT>if isinstance(result, defer.waitForDeferred):<EOL><INDENT>waiting = [True, None]<EOL>def gotResult(r, waiting=waiting, result=result):<EOL><INDENT>result.result = r<EOL>if waiting[<NUM_LIT:0>]:<EOL><INDENT>waiting[<NUM_LIT:0>] = False<EOL>waiting[<NUM_LIT:1>] = r<EOL><DEDENT>else:<EOL><INDENT>_deferGenerator(g, deferred)<EOL><DEDENT><DEDENT>result.d.addBoth(gotResult)<EOL>if waiting[<NUM_LIT:0>]:<EOL><INDENT>waiting[<NUM_LIT:0>] = False<EOL>return deferred<EOL><DEDENT>result = None<EOL><DEDENT><DEDENT>
See L{waitForDeferred}.