[Python-checkins] cpython (3.4): asyncio, Tulip issue 171: BaseEventLoop.close() now raises an exception if the

victor.stinner python-checkins at python.org
Mon Jun 23 01:04:19 CEST 2014


http://hg.python.org/cpython/rev/4f0480e92ffc
changeset:   91333:4f0480e92ffc
branch:      3.4
parent:      91331:46440d260af1
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jun 23 01:02:37 2014 +0200
summary:
  asyncio, Tulip issue 171: BaseEventLoop.close() now raises an exception if the
event loop is running. You must first stop the event loop and then wait until
it stopped, before closing it.

files:
  Doc/library/asyncio-eventloop.rst    |  2 ++
  Lib/asyncio/base_events.py           |  4 ++++
  Lib/asyncio/proactor_events.py       |  2 +-
  Lib/asyncio/selector_events.py       |  2 +-
  Lib/asyncio/unix_events.py           |  2 +-
  Lib/test/test_asyncio/test_events.py |  9 +++++++++
  6 files changed, 18 insertions(+), 3 deletions(-)


diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst
--- a/Doc/library/asyncio-eventloop.rst
+++ b/Doc/library/asyncio-eventloop.rst
@@ -132,6 +132,8 @@
    This clears the queues and shuts down the executor, but does not wait for
    the executor to finish.
 
+   The event loop must not be running.
+
    This is idempotent and irreversible. No other methods should be called after
    this one.
 
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -247,7 +247,11 @@
 
         This clears the queues and shuts down the executor,
         but does not wait for the executor to finish.
+
+        The event loop must not be running.
         """
+        if self._running:
+            raise RuntimeError("cannot close a running event loop")
         if self._closed:
             return
         self._closed = True
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -355,12 +355,12 @@
     def close(self):
         if self.is_closed():
             return
+        super().close()
         self._stop_accept_futures()
         self._close_self_pipe()
         self._proactor.close()
         self._proactor = None
         self._selector = None
-        super().close()
 
     def sock_recv(self, sock, n):
         return self._proactor.recv(sock, n)
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -57,11 +57,11 @@
     def close(self):
         if self.is_closed():
             return
+        super().close()
         self._close_self_pipe()
         if self._selector is not None:
             self._selector.close()
             self._selector = None
-        super().close()
 
     def _socketpair(self):
         raise NotImplementedError
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -44,9 +44,9 @@
         return socket.socketpair()
 
     def close(self):
+        super().close()
         for sig in list(self._signal_handlers):
             self.remove_signal_handler(sig)
-        super().close()
 
     def add_signal_handler(self, sig, callback, *args):
         """Add a handler for a signal.  UNIX only.
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -1365,6 +1365,15 @@
         with self.assertRaises(RuntimeError):
             loop.add_writer(w, callback)
 
+    def test_close_running_event_loop(self):
+        @asyncio.coroutine
+        def close_loop(loop):
+            self.loop.close()
+
+        coro = close_loop(self.loop)
+        with self.assertRaises(RuntimeError):
+            self.loop.run_until_complete(coro)
+
 
 class SubprocessTestsMixin:
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list