Properly show exceptions in coroutines.

This commit is contained in:
Shiz 2014-06-11 06:32:45 +02:00
parent bdec13274d
commit dfc53ef4bb
1 changed files with 6 additions and 14 deletions

View File

@ -37,20 +37,14 @@ def coroutine(func):
result.add_done_callback(handle_future)
except StopIteration as e:
return_future.set_result(getattr(e, 'value', None))
except Exception as e:
return_future.set_exception(e)
try:
# Handle initial value.
gen = func(*args, **kwargs)
except Exception as e:
return_future.set_exception(e)
# Handle initial value.
gen = func(*args, **kwargs)
# If this isn't a generator, then wrap the result with a future.
if not isinstance(gen, types.GeneratorType):
return_future.set_result(gen)
return return_future
else:
# If this isn't a generator, then wrap the result with a future.
if not isinstance(gen, types.GeneratorType):
return_future.set_result(gen)
return return_future
try:
result = next(gen)
@ -59,8 +53,6 @@ def coroutine(func):
result.add_done_callback(handle_future)
except StopIteration as e:
return_future.set_result(getattr(e, 'value', None))
except Exception as e:
return_future.set_exception(e)
return return_future
return wrapper