From 1634fc289a13b0e1fdad4694d5cae7dab55f7186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Srinivas=20Reddy=20Thatiparthy=20=28=E0=B0=B6=E0=B1=8D?= =?UTF-8?q?=E0=B0=B0=E0=B1=80=E0=B0=A8=E0=B0=BF=E0=B0=B5=E0=B0=BE=E0=B0=B8?= =?UTF-8?q?=E0=B1=8D=20=E0=B0=B0=E0=B1=86=E0=B0=A1=E0=B1=8D=E0=B0=A1?= =?UTF-8?q?=E0=B0=BF=20=E0=B0=A4=E0=B0=BE=E0=B0=9F=E0=B0=BF=E0=B0=AA?= =?UTF-8?q?=E0=B0=B0=E0=B1=8D=E0=B0=A4=E0=B0=BF=29?= Date: Sat, 30 Dec 2017 20:39:32 +0530 Subject: bpo-32418: Add get_loop() method on Server, AbstractServer classes (#4997) * Add abstract get_loop() method to Server, AbstractServer classes. * Add test cases for get_loop() method in Server, AbstractServer classes * Add documentation for get_loop() method --- Doc/library/asyncio-eventloop.rst | 6 ++++++ Lib/asyncio/base_events.py | 3 +++ Lib/asyncio/events.py | 4 ++++ Lib/test/test_asyncio/test_events.py | 13 +++++++++++++ .../Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst | 1 + 5 files changed, 27 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2017-12-24-17-29-37.bpo-32418.eZe-ID.rst diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 33b86d6..4fbbcd8 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -913,6 +913,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. diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 00831b3..ab00231 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -185,6 +185,9 @@ class Server(events.AbstractServer): 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 9496d5c..731a0c5 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -155,6 +155,10 @@ class AbstractServer: """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/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index da2e036..e832056 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2826,6 +2826,19 @@ else: 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() + proto = MyProto(loop) + 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): + + def test_get_loop(self): + self.assertEqual(events.AbstractServer().get_loop(), NotImplemented) if __name__ == '__main__': unittest.main() 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 0000000..9441b74 --- /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. -- cgit v0.12