Skip to content

Commit 0bfe1e4

Browse files
committed
loop: Set coroutine wrapper in debug mode
1 parent d82d5c8 commit 0bfe1e4

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

uvloop/includes/stdlib.pxi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import asyncio, asyncio.log, asyncio.base_events, asyncio.sslproto
1+
import asyncio, asyncio.log, asyncio.base_events, \
2+
asyncio.sslproto, asyncio.coroutines
23
import collections
34
import concurrent.futures
45
import functools
@@ -30,6 +31,7 @@ cdef aio_wrap_future = asyncio.wrap_future
3031
cdef aio_BaseProtocol = asyncio.BaseProtocol
3132
cdef aio_Protocol = asyncio.Protocol
3233
cdef aio_SSLProtocol = asyncio.sslproto.SSLProtocol
34+
cdef aio_debug_wrapper = asyncio.coroutines.debug_wrapper
3335

3436
cdef col_deque = collections.deque
3537
cdef col_Iterable = collections.Iterable
@@ -78,6 +80,8 @@ cdef os_O_RDWR = os.O_RDWR
7880

7981
cdef sys_ignore_environment = sys.flags.ignore_environment
8082
cdef sys_exc_info = sys.exc_info
83+
cdef sys_set_coroutine_wrapper = sys.set_coroutine_wrapper
84+
cdef sys_get_coroutine_wrapper = sys.get_coroutine_wrapper
8185

8286
cdef ssl_SSLContext = ssl.SSLContext
8387

uvloop/loop.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ cdef class Loop:
3535
cdef:
3636
uv.uv_loop_t *uvloop
3737

38+
bint _coroutine_wrapper_set
39+
3840
readonly bint _closed
3941
bint _debug
4042
bint _running
@@ -156,6 +158,8 @@ cdef class Loop:
156158
cdef _sock_connect(self, fut, sock, address)
157159
cdef _sock_connect_cb(self, fut, sock, address)
158160

161+
cdef _set_coroutine_wrapper(self, bint enabled)
162+
159163

160164
include "cbhandles.pxd"
161165

uvloop/loop.pyx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ cdef class Loop:
149149

150150
uv.uv_disable_stdio_inheritance()
151151

152+
self._coroutine_wrapper_set = False
153+
152154
def __init__(self):
153155
self.set_debug((not sys_ignore_environment
154156
and bool(os_environ.get('PYTHONASYNCIODEBUG'))))
@@ -684,6 +686,33 @@ cdef class Loop:
684686
else:
685687
fut.set_result(None)
686688

689+
cdef _set_coroutine_wrapper(self, bint enabled):
690+
enabled = bool(enabled)
691+
if self._coroutine_wrapper_set == enabled:
692+
return
693+
694+
wrapper = aio_debug_wrapper
695+
current_wrapper = sys_get_coroutine_wrapper()
696+
697+
if enabled:
698+
if current_wrapper not in (None, wrapper):
699+
warnings.warn(
700+
"loop.set_debug(True): cannot set debug coroutine "
701+
"wrapper; another wrapper is already set %r" %
702+
current_wrapper, RuntimeWarning)
703+
else:
704+
sys_set_coroutine_wrapper(wrapper)
705+
self._coroutine_wrapper_set = True
706+
else:
707+
if current_wrapper not in (None, wrapper):
708+
warnings.warn(
709+
"loop.set_debug(False): cannot unset debug coroutine "
710+
"wrapper; another wrapper was set %r" %
711+
current_wrapper, RuntimeWarning)
712+
else:
713+
sys_set_coroutine_wrapper(None)
714+
self._coroutine_wrapper_set = False
715+
687716
IF DEBUG:
688717
def print_debug_info(self):
689718
cdef:
@@ -833,7 +862,11 @@ cdef class Loop:
833862
# loop.stop() was called right before loop.run_forever().
834863
# This is how asyncio loop behaves.
835864
mode = uv.UV_RUN_NOWAIT
836-
self._run(mode)
865+
self._set_coroutine_wrapper(self._debug)
866+
try:
867+
self._run(mode)
868+
finally:
869+
self._set_coroutine_wrapper(0)
837870

838871
def close(self):
839872
self._close()
@@ -850,6 +883,9 @@ cdef class Loop:
850883
else:
851884
self._debug = 0
852885

886+
if self.is_running():
887+
self._set_coroutine_wrapper(bool(enabled))
888+
853889
def is_running(self):
854890
if self._running == 0:
855891
return False

0 commit comments

Comments
 (0)