diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-12 02:27:25 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-12 02:27:25 (GMT) |
commit | 1af2bf75a2f816eaaf8353eaab8b6dcfee0064c0 (patch) | |
tree | d76885ce449761c6dd9893446f359a03327ed6d5 /Lib/asyncio/base_events.py | |
parent | d7e19bb566889343f39c34c98bca4d6db61b53d7 (diff) | |
download | cpython-1af2bf75a2f816eaaf8353eaab8b6dcfee0064c0.zip cpython-1af2bf75a2f816eaaf8353eaab8b6dcfee0064c0.tar.gz cpython-1af2bf75a2f816eaaf8353eaab8b6dcfee0064c0.tar.bz2 |
asyncio: Support PEP 492. Issue #24017.
Diffstat (limited to 'Lib/asyncio/base_events.py')
-rw-r--r-- | Lib/asyncio/base_events.py | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 98aadaf..38344a7 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -191,8 +191,8 @@ class BaseEventLoop(events.AbstractEventLoop): self._thread_id = None self._clock_resolution = time.get_clock_info('monotonic').resolution self._exception_handler = None - self._debug = (not sys.flags.ignore_environment - and bool(os.environ.get('PYTHONASYNCIODEBUG'))) + self.set_debug((not sys.flags.ignore_environment + and bool(os.environ.get('PYTHONASYNCIODEBUG')))) # In debug mode, if the execution of a callback or a step of a task # exceed this duration in seconds, the slow callback/task is logged. self.slow_callback_duration = 0.1 @@ -360,13 +360,18 @@ class BaseEventLoop(events.AbstractEventLoop): return if self._debug: logger.debug("Close %r", self) - self._closed = True - self._ready.clear() - self._scheduled.clear() - executor = self._default_executor - if executor is not None: - self._default_executor = None - executor.shutdown(wait=False) + try: + self._closed = True + self._ready.clear() + self._scheduled.clear() + executor = self._default_executor + if executor is not None: + self._default_executor = None + executor.shutdown(wait=False) + finally: + # It is important to unregister "sys.coroutine_wrapper" + # if it was registered. + self.set_debug(False) def is_closed(self): """Returns True if the event loop was closed.""" @@ -1199,3 +1204,27 @@ class BaseEventLoop(events.AbstractEventLoop): def set_debug(self, enabled): self._debug = enabled + wrapper = coroutines.debug_wrapper + + try: + set_wrapper = sys.set_coroutine_wrapper + except AttributeError: + pass + else: + current_wrapper = sys.get_coroutine_wrapper() + if enabled: + if current_wrapper not in (None, wrapper): + warnings.warn( + "loop.set_debug(True): cannot set debug coroutine " + "wrapper; another wrapper is already set %r" % + current_wrapper, RuntimeWarning) + else: + set_wrapper(wrapper) + else: + if current_wrapper not in (None, wrapper): + warnings.warn( + "loop.set_debug(False): cannot unset debug coroutine " + "wrapper; another wrapper was set %r" % + current_wrapper, RuntimeWarning) + else: + set_wrapper(None) |