From feabc5dc44b3a5654b1222e70c9994bf1b93c2fa Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 7 Jun 2018 12:38:21 -0400 Subject: [PATCH] bpo-33792: Add selector and proactor windows policies --- Doc/whatsnew/3.7.rst | 4 +++ Lib/asyncio/windows_events.py | 11 +++++-- Lib/test/test_asyncio/test_windows_events.py | 31 +++++++++++++++++++ .../2018-06-07-12-38-12.bpo-33792.3aKG7u.rst | 2 ++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-06-07-12-38-12.bpo-33792.3aKG7u.rst diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 3fe909ee978c24..9a6f542ec479ec 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -733,6 +733,10 @@ include: * Exceptions occurring in cancelled tasks are no longer logged. (Contributed by Yury Selivanov in :issue:`30508`.) +* New ``WindowsSelectorEventLoopPolicy`` and + ``WindowsProactorEventLoopPolicy`` classes. + (Contributed by Yury Selivanov in :issue:`33792`.) + Several ``asyncio`` APIs have been :ref:`deprecated `. diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index d22edec51efc1f..2ec542764375e9 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -21,7 +21,8 @@ __all__ = ( 'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor', - 'DefaultEventLoopPolicy', + 'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy', + 'WindowsProactorEventLoopPolicy', ) @@ -801,8 +802,12 @@ def callback(f): SelectorEventLoop = _WindowsSelectorEventLoop -class _WindowsDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy): +class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy): _loop_factory = SelectorEventLoop -DefaultEventLoopPolicy = _WindowsDefaultEventLoopPolicy +class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy): + _loop_factory = ProactorEventLoop + + +DefaultEventLoopPolicy = WindowsSelectorEventLoopPolicy diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py index f92911ea3ba29c..8f4c50e2c92b45 100644 --- a/Lib/test/test_asyncio/test_windows_events.py +++ b/Lib/test/test_asyncio/test_windows_events.py @@ -166,5 +166,36 @@ def test_wait_for_handle_cancel(self): fut.cancel() +class WinPolicyTests(test_utils.TestCase): + + def test_selector_win_policy(self): + async def main(): + self.assertIsInstance( + asyncio.get_running_loop(), + asyncio.SelectorEventLoop) + + old_policy = asyncio.get_event_loop_policy() + try: + asyncio.set_event_loop_policy( + asyncio.WindowsSelectorEventLoopPolicy()) + asyncio.run(main()) + finally: + asyncio.set_event_loop_policy(old_policy) + + def test_proactor_win_policy(self): + async def main(): + self.assertIsInstance( + asyncio.get_running_loop(), + asyncio.ProactorEventLoop) + + old_policy = asyncio.get_event_loop_policy() + try: + asyncio.set_event_loop_policy( + asyncio.WindowsProactorEventLoopPolicy()) + asyncio.run(main()) + finally: + asyncio.set_event_loop_policy(old_policy) + + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2018-06-07-12-38-12.bpo-33792.3aKG7u.rst b/Misc/NEWS.d/next/Library/2018-06-07-12-38-12.bpo-33792.3aKG7u.rst new file mode 100644 index 00000000000000..8c01764bc68233 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-06-07-12-38-12.bpo-33792.3aKG7u.rst @@ -0,0 +1,2 @@ +Add asyncio.WindowsSelectorEventLoopPolicy and +asyncio.WindowsProactorEventLoopPolicy.