diff -r c4319c0d0131 Lib/asyncio/base_events.py --- a/Lib/asyncio/base_events.py Thu Nov 03 15:38:17 2016 +0200 +++ b/Lib/asyncio/base_events.py Thu Nov 03 11:21:01 2016 -0700 @@ -532,12 +532,10 @@ Absolute time corresponds to the event loop's time() method. """ - if (coroutines.iscoroutine(callback) - or coroutines.iscoroutinefunction(callback)): - raise TypeError("coroutines cannot be used with call_at()") - self._check_closed() if self._debug: self._check_thread() + self._check_closed() + self._check_callback(callback, 'call_at') timer = events.TimerHandle(when, callback, args, self) if timer._source_traceback: del timer._source_traceback[-1] @@ -557,16 +555,22 @@ """ if self._debug: self._check_thread() + self._check_closed() + self._check_callback(callback, 'call_soon') handle = self._call_soon(callback, args) if handle._source_traceback: del handle._source_traceback[-1] return handle + def _check_callback(self, callback, method): + if not self._debug: + return + if (coroutines.iscoroutine(callback) or + coroutines.iscoroutinefunction(callback)): + raise TypeError( + "coroutines cannot be used with {}()".format(method)) + def _call_soon(self, callback, args): - if (coroutines.iscoroutine(callback) - or coroutines.iscoroutinefunction(callback)): - raise TypeError("coroutines cannot be used with call_soon()") - self._check_closed() handle = events.Handle(callback, args, self) if handle._source_traceback: del handle._source_traceback[-1] @@ -592,6 +596,9 @@ def call_soon_threadsafe(self, callback, *args): """Like call_soon(), but thread-safe.""" + if self._debug: + self._check_closed() + self._check_callback(callback, 'call_soon_threadsafe') handle = self._call_soon(callback, args) if handle._source_traceback: del handle._source_traceback[-1] @@ -599,9 +606,7 @@ return handle def run_in_executor(self, executor, func, *args): - if (coroutines.iscoroutine(func) - or coroutines.iscoroutinefunction(func)): - raise TypeError("coroutines cannot be used with run_in_executor()") + self._check_callback(func, 'run_in_executor') self._check_closed() if isinstance(func, events.Handle): assert not args