From a76f2e29b2c4fabb70ba230184ea14a248c7048e Mon Sep 17 00:00:00 2001 From: Itamar Ostricher Date: Mon, 24 Apr 2023 12:24:27 -0600 Subject: [PATCH 1/2] gh-103780: Use patch instead of mock in asyncio unix events test Fixes gh-103780 --- Lib/test/test_asyncio/test_unix_events.py | 40 +++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 96999470a7c69a..5b1f3e2dbc571f 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1713,29 +1713,29 @@ def create_policy(self): return asyncio.DefaultEventLoopPolicy() def test_get_default_child_watcher(self): - policy = self.create_policy() - self.assertIsNone(policy._watcher) - unix_events.can_use_pidfd = mock.Mock() - unix_events.can_use_pidfd.return_value = False - with self.assertWarns(DeprecationWarning): - watcher = policy.get_child_watcher() - self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) + with mock.patch('asyncio.unix_events.can_use_pidfd', + return_value=False): + policy = self.create_policy() + self.assertIsNone(policy._watcher) + with self.assertWarns(DeprecationWarning): + watcher = policy.get_child_watcher() + self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) - self.assertIs(policy._watcher, watcher) - with self.assertWarns(DeprecationWarning): - self.assertIs(watcher, policy.get_child_watcher()) + self.assertIs(policy._watcher, watcher) + with self.assertWarns(DeprecationWarning): + self.assertIs(watcher, policy.get_child_watcher()) - policy = self.create_policy() - self.assertIsNone(policy._watcher) - unix_events.can_use_pidfd = mock.Mock() - unix_events.can_use_pidfd.return_value = True - with self.assertWarns(DeprecationWarning): - watcher = policy.get_child_watcher() - self.assertIsInstance(watcher, asyncio.PidfdChildWatcher) + with mock.patch('asyncio.unix_events.can_use_pidfd', + return_value=True): + policy = self.create_policy() + self.assertIsNone(policy._watcher) + with self.assertWarns(DeprecationWarning): + watcher = policy.get_child_watcher() + self.assertIsInstance(watcher, asyncio.PidfdChildWatcher) - self.assertIs(policy._watcher, watcher) - with self.assertWarns(DeprecationWarning): - self.assertIs(watcher, policy.get_child_watcher()) + self.assertIs(policy._watcher, watcher) + with self.assertWarns(DeprecationWarning): + self.assertIs(watcher, policy.get_child_watcher()) def test_get_child_watcher_after_set(self): policy = self.create_policy() From 7e0479af9e419b0af9ab72e6c4397a61b0d24427 Mon Sep 17 00:00:00 2001 From: Itamar Ostricher Date: Mon, 24 Apr 2023 13:46:04 -0600 Subject: [PATCH 2/2] Use patch decorator instead of context manager --- Lib/test/test_asyncio/test_unix_events.py | 41 +++++++++++------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 5b1f3e2dbc571f..cdf3eaac68af15 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1712,30 +1712,29 @@ class PolicyTests(unittest.TestCase): def create_policy(self): return asyncio.DefaultEventLoopPolicy() - def test_get_default_child_watcher(self): - with mock.patch('asyncio.unix_events.can_use_pidfd', - return_value=False): - policy = self.create_policy() - self.assertIsNone(policy._watcher) - with self.assertWarns(DeprecationWarning): - watcher = policy.get_child_watcher() - self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) + @mock.patch('asyncio.unix_events.can_use_pidfd') + def test_get_default_child_watcher(self, m_can_use_pidfd): + m_can_use_pidfd.return_value = False + policy = self.create_policy() + self.assertIsNone(policy._watcher) + with self.assertWarns(DeprecationWarning): + watcher = policy.get_child_watcher() + self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) - self.assertIs(policy._watcher, watcher) - with self.assertWarns(DeprecationWarning): - self.assertIs(watcher, policy.get_child_watcher()) + self.assertIs(policy._watcher, watcher) + with self.assertWarns(DeprecationWarning): + self.assertIs(watcher, policy.get_child_watcher()) - with mock.patch('asyncio.unix_events.can_use_pidfd', - return_value=True): - policy = self.create_policy() - self.assertIsNone(policy._watcher) - with self.assertWarns(DeprecationWarning): - watcher = policy.get_child_watcher() - self.assertIsInstance(watcher, asyncio.PidfdChildWatcher) + m_can_use_pidfd.return_value = True + policy = self.create_policy() + self.assertIsNone(policy._watcher) + with self.assertWarns(DeprecationWarning): + watcher = policy.get_child_watcher() + self.assertIsInstance(watcher, asyncio.PidfdChildWatcher) - self.assertIs(policy._watcher, watcher) - with self.assertWarns(DeprecationWarning): - self.assertIs(watcher, policy.get_child_watcher()) + self.assertIs(policy._watcher, watcher) + with self.assertWarns(DeprecationWarning): + self.assertIs(watcher, policy.get_child_watcher()) def test_get_child_watcher_after_set(self): policy = self.create_policy()