Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit e1874b4

Browse files
committed
Optimize call_soon to perform extra checks in debug mode only
1 parent 1d6f0ed commit e1874b4

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

asyncio/base_events.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,10 @@ def call_at(self, when, callback, *args):
531531
532532
Absolute time corresponds to the event loop's time() method.
533533
"""
534-
if (coroutines.iscoroutine(callback)
535-
or coroutines.iscoroutinefunction(callback)):
536-
raise TypeError("coroutines cannot be used with call_at()")
537534
self._check_closed()
538535
if self._debug:
539536
self._check_thread()
537+
self._check_callback(callback, 'call_at')
540538
timer = events.TimerHandle(when, callback, args, self)
541539
if timer._source_traceback:
542540
del timer._source_traceback[-1]
@@ -554,18 +552,24 @@ def call_soon(self, callback, *args):
554552
Any positional arguments after the callback will be passed to
555553
the callback when it is called.
556554
"""
555+
self._check_closed()
557556
if self._debug:
558557
self._check_thread()
558+
self._check_callback(callback, 'call_soon')
559559
handle = self._call_soon(callback, args)
560560
if handle._source_traceback:
561561
del handle._source_traceback[-1]
562562
return handle
563563

564+
def _check_callback(self, callback, method):
565+
if (coroutines.iscoroutine(callback) or
566+
coroutines.iscoroutinefunction(callback)):
567+
raise TypeError(
568+
"coroutines cannot be used with {}()".format(method))
569+
if isinstance(callback, events.Handle):
570+
raise TypeError('A Handle is not a callback')
571+
564572
def _call_soon(self, callback, args):
565-
if (coroutines.iscoroutine(callback)
566-
or coroutines.iscoroutinefunction(callback)):
567-
raise TypeError("coroutines cannot be used with call_soon()")
568-
self._check_closed()
569573
handle = events.Handle(callback, args, self)
570574
if handle._source_traceback:
571575
del handle._source_traceback[-1]
@@ -591,17 +595,19 @@ def _check_thread(self):
591595

592596
def call_soon_threadsafe(self, callback, *args):
593597
"""Like call_soon(), but thread-safe."""
598+
self._check_closed()
599+
if self._debug:
600+
self._check_callback(callback, 'call_soon_threadsafe')
594601
handle = self._call_soon(callback, args)
595602
if handle._source_traceback:
596603
del handle._source_traceback[-1]
597604
self._write_to_self()
598605
return handle
599606

600607
def run_in_executor(self, executor, func, *args):
601-
if (coroutines.iscoroutine(func)
602-
or coroutines.iscoroutinefunction(func)):
603-
raise TypeError("coroutines cannot be used with run_in_executor()")
604608
self._check_closed()
609+
if self._debug:
610+
self._check_callback(func, 'run_in_executor')
605611
if isinstance(func, events.Handle):
606612
assert not args
607613
assert not isinstance(func, events.TimerHandle)

asyncio/events.py

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class Handle:
8282
'_source_traceback', '_repr', '__weakref__')
8383

8484
def __init__(self, callback, args, loop):
85-
assert not isinstance(callback, Handle), 'A Handle is not a callback'
8685
self._loop = loop
8786
self._callback = callback
8887
self._args = args

tests/test_base_events.py

+1
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,7 @@ def test_call_coroutine(self):
16661666
def simple_coroutine():
16671667
pass
16681668

1669+
self.loop.set_debug(True)
16691670
coro_func = simple_coroutine
16701671
coro_obj = coro_func()
16711672
self.addCleanup(coro_obj.close)

tests/test_events.py

-7
Original file line numberDiff line numberDiff line change
@@ -2249,13 +2249,6 @@ def callback(*args):
22492249
h.cancel()
22502250
self.assertTrue(h._cancelled)
22512251

2252-
def test_handle_from_handle(self):
2253-
def callback(*args):
2254-
return args
2255-
h1 = asyncio.Handle(callback, (), loop=self.loop)
2256-
self.assertRaises(
2257-
AssertionError, asyncio.Handle, h1, (), self.loop)
2258-
22592252
def test_callback_with_exception(self):
22602253
def callback():
22612254
raise ValueError()

0 commit comments

Comments
 (0)