Merge heads

This commit is contained in:
Yury Selivanov 2016-09-15 13:26:58 -04:00
commit 03179ef3a8
1 changed files with 16 additions and 8 deletions

View File

@ -242,9 +242,13 @@ class BaseEventLoop(events.AbstractEventLoop):
self._task_factory = None self._task_factory = None
self._coroutine_wrapper_set = False self._coroutine_wrapper_set = False
# A weak set of all asynchronous generators that are being iterated if hasattr(sys, 'get_asyncgen_hooks'):
# by the loop. # Python >= 3.6
# A weak set of all asynchronous generators that are
# being iterated by the loop.
self._asyncgens = weakref.WeakSet() self._asyncgens = weakref.WeakSet()
else:
self._asyncgens = None
# Set to True when `loop.shutdown_asyncgens` is called. # Set to True when `loop.shutdown_asyncgens` is called.
self._asyncgens_shutdown_called = False self._asyncgens_shutdown_called = False
@ -359,7 +363,9 @@ class BaseEventLoop(events.AbstractEventLoop):
"""Shutdown all active asynchronous generators.""" """Shutdown all active asynchronous generators."""
self._asyncgens_shutdown_called = True self._asyncgens_shutdown_called = True
if not len(self._asyncgens): if self._asyncgens is None or not len(self._asyncgens):
# If Python version is <3.6 or we don't have any asynchronous
# generators alive.
return return
closing_agens = list(self._asyncgens) closing_agens = list(self._asyncgens)
@ -387,6 +393,7 @@ class BaseEventLoop(events.AbstractEventLoop):
raise RuntimeError('Event loop is running.') raise RuntimeError('Event loop is running.')
self._set_coroutine_wrapper(self._debug) self._set_coroutine_wrapper(self._debug)
self._thread_id = threading.get_ident() self._thread_id = threading.get_ident()
if self._asyncgens is not None:
old_agen_hooks = sys.get_asyncgen_hooks() old_agen_hooks = sys.get_asyncgen_hooks()
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook, sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
finalizer=self._asyncgen_finalizer_hook) finalizer=self._asyncgen_finalizer_hook)
@ -399,6 +406,7 @@ class BaseEventLoop(events.AbstractEventLoop):
self._stopping = False self._stopping = False
self._thread_id = None self._thread_id = None
self._set_coroutine_wrapper(False) self._set_coroutine_wrapper(False)
if self._asyncgens is not None:
sys.set_asyncgen_hooks(*old_agen_hooks) sys.set_asyncgen_hooks(*old_agen_hooks)
def run_until_complete(self, future): def run_until_complete(self, future):