@@ -528,12 +528,10 @@ def call_at(self, when, callback, *args):
528528
529529 Absolute time corresponds to the event loop's time() method.
530530 """
531- if (coroutines .iscoroutine (callback )
532- or coroutines .iscoroutinefunction (callback )):
533- raise TypeError ("coroutines cannot be used with call_at()" )
534531 self ._check_closed ()
535532 if self ._debug :
536533 self ._check_thread ()
534+ self ._check_callback (callback , 'call_at' )
537535 timer = events .TimerHandle (when , callback , args , self )
538536 if timer ._source_traceback :
539537 del timer ._source_traceback [- 1 ]
@@ -551,18 +549,27 @@ def call_soon(self, callback, *args):
551549 Any positional arguments after the callback will be passed to
552550 the callback when it is called.
553551 """
552+ self ._check_closed ()
554553 if self ._debug :
555554 self ._check_thread ()
555+ self ._check_callback (callback , 'call_soon' )
556556 handle = self ._call_soon (callback , args )
557557 if handle ._source_traceback :
558558 del handle ._source_traceback [- 1 ]
559559 return handle
560560
561+ def _check_callback (self , callback , method ):
562+ if (coroutines .iscoroutine (callback ) or
563+ coroutines .iscoroutinefunction (callback )):
564+ raise TypeError (
565+ "coroutines cannot be used with {}()" .format (method ))
566+ if not callable (callback ):
567+ raise TypeError (
568+ 'a callable object was expected by {}(), got {!r}' .format (
569+ method , callback ))
570+
571+
561572 def _call_soon (self , callback , args ):
562- if (coroutines .iscoroutine (callback )
563- or coroutines .iscoroutinefunction (callback )):
564- raise TypeError ("coroutines cannot be used with call_soon()" )
565- self ._check_closed ()
566573 handle = events .Handle (callback , args , self )
567574 if handle ._source_traceback :
568575 del handle ._source_traceback [- 1 ]
@@ -588,28 +595,19 @@ def _check_thread(self):
588595
589596 def call_soon_threadsafe (self , callback , * args ):
590597 """Like call_soon(), but thread-safe."""
598+ self ._check_closed ()
599+ if self ._debug :
600+ self ._check_callback (callback , 'call_soon_threadsafe' )
591601 handle = self ._call_soon (callback , args )
592602 if handle ._source_traceback :
593603 del handle ._source_traceback [- 1 ]
594604 self ._write_to_self ()
595605 return handle
596606
597607 def run_in_executor (self , executor , func , * args ):
598- if (coroutines .iscoroutine (func )
599- or coroutines .iscoroutinefunction (func )):
600- raise TypeError ("coroutines cannot be used with run_in_executor()" )
601608 self ._check_closed ()
602- if isinstance (func , events .Handle ):
603- assert not args
604- assert not isinstance (func , events .TimerHandle )
605- warnings .warn (
606- "Passing Handle to loop.run_in_executor() is deprecated" ,
607- DeprecationWarning )
608- if func ._cancelled :
609- f = self .create_future ()
610- f .set_result (None )
611- return f
612- func , args = func ._callback , func ._args
609+ if self ._debug :
610+ self ._check_callback (func , 'run_in_executor' )
613611 if executor is None :
614612 executor = self ._default_executor
615613 if executor is None :
0 commit comments