mirror of https://github.com/python/cpython
Closes #22922: More EventLoop methods fail if the loop is closed. Initial patch
written by Torsten Landschoff. create_task(), call_at(), call_soon(), call_soon_threadsafe() and run_in_executor() now raise an error if the event loop is closed.
This commit is contained in:
parent
dd8224e6a4
commit
e80bf0d4a9
|
@ -177,6 +177,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
|
|
||||||
Return a task object.
|
Return a task object.
|
||||||
"""
|
"""
|
||||||
|
self._check_closed()
|
||||||
task = tasks.Task(coro, loop=self)
|
task = tasks.Task(coro, loop=self)
|
||||||
if task._source_traceback:
|
if task._source_traceback:
|
||||||
del task._source_traceback[-1]
|
del task._source_traceback[-1]
|
||||||
|
@ -360,6 +361,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
if (coroutines.iscoroutine(callback)
|
if (coroutines.iscoroutine(callback)
|
||||||
or coroutines.iscoroutinefunction(callback)):
|
or coroutines.iscoroutinefunction(callback)):
|
||||||
raise TypeError("coroutines cannot be used with call_at()")
|
raise TypeError("coroutines cannot be used with call_at()")
|
||||||
|
self._check_closed()
|
||||||
if self._debug:
|
if self._debug:
|
||||||
self._assert_is_current_event_loop()
|
self._assert_is_current_event_loop()
|
||||||
timer = events.TimerHandle(when, callback, args, self)
|
timer = events.TimerHandle(when, callback, args, self)
|
||||||
|
@ -390,6 +392,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
raise TypeError("coroutines cannot be used with call_soon()")
|
raise TypeError("coroutines cannot be used with call_soon()")
|
||||||
if self._debug and check_loop:
|
if self._debug and check_loop:
|
||||||
self._assert_is_current_event_loop()
|
self._assert_is_current_event_loop()
|
||||||
|
self._check_closed()
|
||||||
handle = events.Handle(callback, args, self)
|
handle = events.Handle(callback, args, self)
|
||||||
if handle._source_traceback:
|
if handle._source_traceback:
|
||||||
del handle._source_traceback[-1]
|
del handle._source_traceback[-1]
|
||||||
|
@ -426,6 +429,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
if (coroutines.iscoroutine(callback)
|
if (coroutines.iscoroutine(callback)
|
||||||
or coroutines.iscoroutinefunction(callback)):
|
or coroutines.iscoroutinefunction(callback)):
|
||||||
raise TypeError("coroutines cannot be used with run_in_executor()")
|
raise TypeError("coroutines cannot be used with run_in_executor()")
|
||||||
|
self._check_closed()
|
||||||
if isinstance(callback, events.Handle):
|
if isinstance(callback, events.Handle):
|
||||||
assert not args
|
assert not args
|
||||||
assert not isinstance(callback, events.TimerHandle)
|
assert not isinstance(callback, events.TimerHandle)
|
||||||
|
|
|
@ -71,6 +71,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
|
||||||
or coroutines.iscoroutinefunction(callback)):
|
or coroutines.iscoroutinefunction(callback)):
|
||||||
raise TypeError("coroutines cannot be used with add_signal_handler()")
|
raise TypeError("coroutines cannot be used with add_signal_handler()")
|
||||||
self._check_signal(sig)
|
self._check_signal(sig)
|
||||||
|
self._check_closed()
|
||||||
try:
|
try:
|
||||||
# set_wakeup_fd() raises ValueError if this is not the
|
# set_wakeup_fd() raises ValueError if this is not the
|
||||||
# main thread. By calling it early we ensure that an
|
# main thread. By calling it early we ensure that an
|
||||||
|
|
|
@ -226,7 +226,8 @@ class EventLoopTestsMixin:
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
# just in case if we have transport close callbacks
|
# just in case if we have transport close callbacks
|
||||||
test_utils.run_briefly(self.loop)
|
if not self.loop.is_closed():
|
||||||
|
test_utils.run_briefly(self.loop)
|
||||||
|
|
||||||
self.loop.close()
|
self.loop.close()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
@ -1434,6 +1435,38 @@ class EventLoopTestsMixin:
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
def test_close(self):
|
||||||
|
self.loop.close()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def test():
|
||||||
|
pass
|
||||||
|
|
||||||
|
func = lambda: False
|
||||||
|
coro = test()
|
||||||
|
self.addCleanup(coro.close)
|
||||||
|
|
||||||
|
# operation blocked when the loop is closed
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.run_forever()
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
fut = asyncio.Future(loop=self.loop)
|
||||||
|
self.loop.run_until_complete(fut)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.call_soon(func)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.call_soon_threadsafe(func)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.call_later(1.0, func)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.call_at(self.loop.time() + .0, func)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.run_in_executor(None, func)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.create_task(coro)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.loop.add_signal_handler(signal.SIGTERM, func)
|
||||||
|
|
||||||
|
|
||||||
class SubprocessTestsMixin:
|
class SubprocessTestsMixin:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue