From 64b88e2ab10c2436c71368ac84eb9a1c92fd7310 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Sun, 24 Dec 2017 02:08:01 +0530 Subject: [PATCH 1/4] Add abstract get_loop() method to Server, AbstractServer classes. --- Lib/asyncio/base_events.py | 3 +++ Lib/asyncio/events.py | 4 ++++ .../Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst | 1 + 3 files changed, 8 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 96cc4f02588ac8f..edf8ccd2d98e711 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -179,6 +179,9 @@ def close(self): if self._active_count == 0: self._wakeup() + def get_loop(self): + return self._loop + def _wakeup(self): waiters = self._waiters self._waiters = None diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 3a5dbadbb105bfc..5702e1eb41ec7bf 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -155,6 +155,10 @@ async def wait_closed(self): """Coroutine to wait until service is closed.""" return NotImplemented + def get_loop(self): + """ Get the event loop the Server object is attached to.""" + return NotImplemented + class AbstractEventLoop: """Abstract event loop.""" diff --git a/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst b/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst new file mode 100644 index 000000000000000..9441b74ef844c4f --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst @@ -0,0 +1 @@ +Add get_loop() method to Server and AbstractServer classes. From 2f4b93f27b332fb6a8c1487a2362fe728a34997f Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Sun, 24 Dec 2017 14:46:16 +0530 Subject: [PATCH 2/4] Add test cases for get_loop() method in Server, AbstractServer classes --- Lib/test/test_asyncio/test_events.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 79e8d79e6b14b23..21a6df220ea977c 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2865,6 +2865,19 @@ class TestCGetEventLoop(GetEventLoopTestsMixin, unittest.TestCase): get_running_loop_impl = events._c_get_running_loop get_event_loop_impl = events._c_get_event_loop +class TestServer(unittest.TestCase): + + def test_get_loop(self): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + proto = MyProto(loop) + server = loop.create_server(lambda: proto, '0.0.0.0', 0) + self.assertEqual(server.get_loop(), loop) + +class TestAbstractServer(unittest.TestCase): + + def test_get_loop(self): + self.assertEqual(events.AbstractServer().get_loop(), NotImplemented) if __name__ == '__main__': unittest.main() From d0792fd4dd2570626be724311e83cfa28cec3058 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Tue, 26 Dec 2017 08:51:55 +0530 Subject: [PATCH 3/4] fix the failing test case --- Lib/test/test_asyncio/test_events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 21a6df220ea977c..bf6ac1c735dae39 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2869,10 +2869,10 @@ class TestServer(unittest.TestCase): def test_get_loop(self): loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) proto = MyProto(loop) - server = loop.create_server(lambda: proto, '0.0.0.0', 0) + server = loop.run_until_complete(loop.create_server(lambda: proto, '0.0.0.0', 0)) self.assertEqual(server.get_loop(), loop) + loop.close() class TestAbstractServer(unittest.TestCase): From e6f7283905af334b8a9e9ede9362977aa3b71432 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Wed, 27 Dec 2017 22:48:30 +0530 Subject: [PATCH 4/4] Add documentation for get_loop() method --- Doc/library/asyncio-eventloop.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 5dd258df3127a67..8b41772627076f9 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -881,6 +881,12 @@ Server The server is closed asynchronously, use the :meth:`wait_closed` coroutine to wait until the server is closed. + .. method:: get_loop() + + Gives the event loop associated with the server object. + + .. versionadded:: 3.7 + .. coroutinemethod:: wait_closed() Wait until the :meth:`close` method completes.