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

Commit 405d919

Browse files
committed
Don't allow passing Handles in loop.run_in_executor.
1 parent e1874b4 commit 405d919

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

asyncio/base_events.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,11 @@ def _check_callback(self, callback, method):
566566
coroutines.iscoroutinefunction(callback)):
567567
raise TypeError(
568568
"coroutines cannot be used with {}()".format(method))
569-
if isinstance(callback, events.Handle):
570-
raise TypeError('A Handle is not a callback')
569+
if not callable(callback):
570+
raise TypeError(
571+
'a callable object was expected by {}(), got {!r}'.format(
572+
method, callback))
573+
571574

572575
def _call_soon(self, callback, args):
573576
handle = events.Handle(callback, args, self)
@@ -608,17 +611,6 @@ def run_in_executor(self, executor, func, *args):
608611
self._check_closed()
609612
if self._debug:
610613
self._check_callback(func, 'run_in_executor')
611-
if isinstance(func, events.Handle):
612-
assert not args
613-
assert not isinstance(func, events.TimerHandle)
614-
warnings.warn(
615-
"Passing Handle to loop.run_in_executor() is deprecated",
616-
DeprecationWarning)
617-
if func._cancelled:
618-
f = self.create_future()
619-
f.set_result(None)
620-
return f
621-
func, args = func._callback, func._args
622614
if executor is None:
623615
executor = self._default_executor
624616
if executor is None:

tests/test_base_events.py

+7-28
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ def cb():
235235
self.assertIsInstance(h, asyncio.Handle)
236236
self.assertIn(h, self.loop._ready)
237237

238+
def test_call_soon_non_callable(self):
239+
self.loop.set_debug(True)
240+
with self.assertRaisesRegex(TypeError, 'a callable object'):
241+
self.loop.call_soon(1)
242+
238243
def test_call_later(self):
239244
def cb():
240245
pass
@@ -341,47 +346,21 @@ def test_thread(loop, debug, create_loop=False):
341346
# check disabled if debug mode is disabled
342347
test_thread(self.loop, False, create_loop=True)
343348

344-
def test_run_once_in_executor_handle(self):
345-
def cb():
346-
pass
347-
348-
self.assertRaises(
349-
AssertionError, self.loop.run_in_executor,
350-
None, asyncio.Handle(cb, (), self.loop), ('',))
351-
self.assertRaises(
352-
AssertionError, self.loop.run_in_executor,
353-
None, asyncio.TimerHandle(10, cb, (), self.loop))
354-
355-
def test_run_once_in_executor_cancelled(self):
356-
def cb():
357-
pass
358-
h = asyncio.Handle(cb, (), self.loop)
359-
h.cancel()
360-
361-
with self.assertWarnsRegex(DeprecationWarning, "Passing Handle"):
362-
f = self.loop.run_in_executor(None, h)
363-
self.assertIsInstance(f, asyncio.Future)
364-
self.assertTrue(f.done())
365-
self.assertIsNone(f.result())
366-
367349
def test_run_once_in_executor_plain(self):
368350
def cb():
369351
pass
370-
h = asyncio.Handle(cb, (), self.loop)
371352
f = asyncio.Future(loop=self.loop)
372353
executor = mock.Mock()
373354
executor.submit.return_value = f
374355

375356
self.loop.set_default_executor(executor)
376357

377-
with self.assertWarnsRegex(DeprecationWarning, "Passing Handle"):
378-
res = self.loop.run_in_executor(None, h)
358+
res = self.loop.run_in_executor(None, cb)
379359
self.assertIs(f, res)
380360

381361
executor = mock.Mock()
382362
executor.submit.return_value = f
383-
with self.assertWarnsRegex(DeprecationWarning, "Passing Handle"):
384-
res = self.loop.run_in_executor(executor, h)
363+
res = self.loop.run_in_executor(executor, cb)
385364
self.assertIs(f, res)
386365
self.assertTrue(executor.submit.called)
387366

0 commit comments

Comments
 (0)