def engine(func):
 @functools.wraps(func)
 def wrapper(*args, **kwargs):
  runner = None
  def handle_exception(typ, value, tb):
   if (runner is not None):
    return runner.handle_exception(typ, value, tb)
   return False
  with ExceptionStackContext(handle_exception) as deactivate:
   try:
    result = func(*args, **kwargs)
   except (Return, StopIteration) as e:
    result = getattr(e, 'value', None)
   else:
    if isinstance(result, types.GeneratorType):
     def final_callback(value):
      if (value is not None):
       raise ReturnValueIgnoredError(('@gen.engine functions cannot return values: %r' % (value,)))
      assert (value is None)
      deactivate()
     runner = Runner(result, final_callback)
     runner.run()
     return
   if (result is not None):
    raise ReturnValueIgnoredError(('@gen.engine functions cannot return values: %r' % (result,)))
   deactivate()
 return wrapper

def engine ( func ) : @ functools . wraps ( func ) def wrapper ( * args , ** kwargs ) : runner = None def handle_exception ( typ , value , tb ) : if ( runner is not None ) : return runner . handle_exception ( typ , value , tb ) return False with ExceptionStackContext ( handle_exception ) as deactivate : try : result = func ( * args , ** kwargs ) except ( Return , StopIteration ) as e : result = getattr ( e , 'value' , None ) else : if isinstance ( result , types . GeneratorType ) : def final_callback ( value ) : if ( value is not None ) : raise ReturnValueIgnoredError ( ( '@gen.engine functions cannot return values: %r' % ( value , ) ) ) assert ( value is None ) deactivate ( ) runner = Runner ( result , final_callback ) runner . run ( ) return if ( result is not None ) : raise ReturnValueIgnoredError ( ( '@gen.engine functions cannot return values: %r' % ( result , ) ) ) deactivate ( ) return wrapper
callback-oriented decorator for asynchronous generators .