GH-94597: Deprecate child watcher getters and setters (#98215)

This is the next step for deprecating child watchers.

Until we've removed the API completely we have to use it, so this PR is mostly suppressing a lot of warnings when using the API internally.

Once the child watcher API is totally removed, the two child watcher implementations we actually use and need (Pidfd and Thread) will be turned into internal helpers.
This commit is contained in:
Kumar Aditya 2022-10-16 04:39:30 +05:30 committed by GitHub
parent bb56dead33
commit 660f10248b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 51 deletions

View File

@ -88,12 +88,16 @@ The abstract event loop policy base class is defined as follows:
This function is Unix specific. This function is Unix specific.
.. deprecated:: 3.12
.. method:: set_child_watcher(watcher) .. method:: set_child_watcher(watcher)
Set the current child process watcher to *watcher*. Set the current child process watcher to *watcher*.
This function is Unix specific. This function is Unix specific.
.. deprecated:: 3.12
.. _asyncio-policy-builtin: .. _asyncio-policy-builtin:
@ -158,12 +162,16 @@ implementation used by the asyncio event loop:
Return the current child watcher for the current policy. Return the current child watcher for the current policy.
.. deprecated:: 3.12
.. function:: set_child_watcher(watcher) .. function:: set_child_watcher(watcher)
Set the current child watcher to *watcher* for the current Set the current child watcher to *watcher* for the current
policy. *watcher* must implement methods defined in the policy. *watcher* must implement methods defined in the
:class:`AbstractChildWatcher` base class. :class:`AbstractChildWatcher` base class.
.. deprecated:: 3.12
.. note:: .. note::
Third-party event loops implementations might not support Third-party event loops implementations might not support
custom child watchers. For such event loops, using custom child watchers. For such event loops, using
@ -245,6 +253,8 @@ implementation used by the asyncio event loop:
.. versionadded:: 3.8 .. versionadded:: 3.8
.. deprecated:: 3.12
.. class:: SafeChildWatcher .. class:: SafeChildWatcher
This implementation uses active event loop from the main thread to handle This implementation uses active event loop from the main thread to handle
@ -257,6 +267,8 @@ implementation used by the asyncio event loop:
This solution is as safe as :class:`MultiLoopChildWatcher` and has the same *O(N)* This solution is as safe as :class:`MultiLoopChildWatcher` and has the same *O(N)*
complexity but requires a running event loop in the main thread to work. complexity but requires a running event loop in the main thread to work.
.. deprecated:: 3.12
.. class:: FastChildWatcher .. class:: FastChildWatcher
This implementation reaps every terminated processes by calling This implementation reaps every terminated processes by calling
@ -269,6 +281,8 @@ implementation used by the asyncio event loop:
This solution requires a running event loop in the main thread to work, as This solution requires a running event loop in the main thread to work, as
:class:`SafeChildWatcher`. :class:`SafeChildWatcher`.
.. deprecated:: 3.12
.. class:: PidfdChildWatcher .. class:: PidfdChildWatcher
This implementation polls process file descriptors (pidfds) to await child This implementation polls process file descriptors (pidfds) to await child

View File

@ -126,6 +126,12 @@ asyncio
if supported and :class:`~asyncio.ThreadedChildWatcher` otherwise). if supported and :class:`~asyncio.ThreadedChildWatcher` otherwise).
(Contributed by Kumar Aditya in :gh:`94597`.) (Contributed by Kumar Aditya in :gh:`94597`.)
* :func:`asyncio.set_child_watcher`, :func:`asyncio.get_child_watcher`,
:meth:`asyncio.AbstractEventLoopPolicy.set_child_watcher` and
:meth:`asyncio.AbstractEventLoopPolicy.get_child_watcher` are deprecated
and will be removed in Python 3.14.
(Contributed by Kumar Aditya in :gh:`94597`.)
pathlib pathlib
------- -------

View File

@ -195,6 +195,8 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
async def _make_subprocess_transport(self, protocol, args, shell, async def _make_subprocess_transport(self, protocol, args, shell,
stdin, stdout, stderr, bufsize, stdin, stdout, stderr, bufsize,
extra=None, **kwargs): extra=None, **kwargs):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
with events.get_child_watcher() as watcher: with events.get_child_watcher() as watcher:
if not watcher.is_active(): if not watcher.is_active():
# Check early. # Check early.
@ -1469,6 +1471,9 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
if self._watcher is None: if self._watcher is None:
self._init_watcher() self._init_watcher()
warnings._deprecated("get_child_watcher",
"{name!r} is deprecated as of Python 3.12 and will be "
"removed in Python {remove}.", remove=(3, 14))
return self._watcher return self._watcher
def set_child_watcher(self, watcher): def set_child_watcher(self, watcher):
@ -1480,6 +1485,9 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
self._watcher.close() self._watcher.close()
self._watcher = watcher self._watcher = watcher
warnings._deprecated("set_child_watcher",
"{name!r} is deprecated as of Python 3.12 and will be "
"removed in Python {remove}.", remove=(3, 14))
SelectorEventLoop = _UnixSelectorEventLoop SelectorEventLoop = _UnixSelectorEventLoop

View File

@ -2062,6 +2062,8 @@ else:
asyncio.set_child_watcher(watcher) asyncio.set_child_watcher(watcher)
def tearDown(self): def tearDown(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.set_child_watcher(None) asyncio.set_child_watcher(None)
super().tearDown() super().tearDown()
@ -2663,6 +2665,8 @@ class GetEventLoopTestsMixin:
def tearDown(self): def tearDown(self):
try: try:
if sys.platform != 'win32': if sys.platform != 'win32':
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.set_child_watcher(None) asyncio.set_child_watcher(None)
super().tearDown() super().tearDown()

View File

@ -797,6 +797,8 @@ os.close(fd)
watcher = asyncio.SafeChildWatcher() watcher = asyncio.SafeChildWatcher()
watcher.attach_loop(self.loop) watcher.attach_loop(self.loop)
try: try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.set_child_watcher(watcher) asyncio.set_child_watcher(watcher)
create = asyncio.create_subprocess_exec( create = asyncio.create_subprocess_exec(
*args, *args,
@ -805,6 +807,8 @@ os.close(fd)
proc = self.loop.run_until_complete(create) proc = self.loop.run_until_complete(create)
self.loop.run_until_complete(proc.wait()) self.loop.run_until_complete(proc.wait())
finally: finally:
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.set_child_watcher(None) asyncio.set_child_watcher(None)
os.close(wfd) os.close(wfd)

View File

@ -582,6 +582,8 @@ class SubprocessMixin:
# manually to avoid a warning when the watcher is detached. # manually to avoid a warning when the watcher is detached.
if (sys.platform != 'win32' and if (sys.platform != 'win32' and
isinstance(self, SubprocessFastWatcherTests)): isinstance(self, SubprocessFastWatcherTests)):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.get_child_watcher()._callbacks.clear() asyncio.get_child_watcher()._callbacks.clear()
async def _test_popen_error(self, stdin): async def _test_popen_error(self, stdin):
@ -696,11 +698,15 @@ if sys.platform != 'win32':
watcher = self._get_watcher() watcher = self._get_watcher()
watcher.attach_loop(self.loop) watcher.attach_loop(self.loop)
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
policy.set_child_watcher(watcher) policy.set_child_watcher(watcher)
def tearDown(self): def tearDown(self):
super().tearDown() super().tearDown()
policy = asyncio.get_event_loop_policy() policy = asyncio.get_event_loop_policy()
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
watcher = policy.get_child_watcher() watcher = policy.get_child_watcher()
policy.set_child_watcher(None) policy.set_child_watcher(None)
watcher.attach_loop(None) watcher.attach_loop(None)
@ -752,6 +758,8 @@ if sys.platform != 'win32':
) )
async def execute(): async def execute():
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
asyncio.set_child_watcher(watcher) asyncio.set_child_watcher(watcher)
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
@ -761,6 +769,8 @@ if sys.platform != 'win32':
watcher.add_child_handler.assert_not_called() watcher.add_child_handler.assert_not_called()
with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner: with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner:
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
self.assertIsNone(runner.run(execute())) self.assertIsNone(runner.run(execute()))
self.assertListEqual(watcher.mock_calls, [ self.assertListEqual(watcher.mock_calls, [
mock.call.__enter__(), mock.call.__enter__(),
@ -788,7 +798,7 @@ if sys.platform != 'win32':
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
asyncio.get_event_loop_policy().get_event_loop() asyncio.get_event_loop_policy().get_event_loop()
return await asyncio.to_thread(asyncio.run, in_thread()) return await asyncio.to_thread(asyncio.run, in_thread())
with self.assertWarns(DeprecationWarning):
asyncio.set_child_watcher(asyncio.PidfdChildWatcher()) asyncio.set_child_watcher(asyncio.PidfdChildWatcher())
try: try:
with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner: with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner:
@ -796,6 +806,7 @@ if sys.platform != 'win32':
self.assertEqual(returncode, 0) self.assertEqual(returncode, 0)
self.assertEqual(stdout, b'some data') self.assertEqual(stdout, b'some data')
finally: finally:
with self.assertWarns(DeprecationWarning):
asyncio.set_child_watcher(None) asyncio.set_child_watcher(None)
else: else:
# Windows # Windows

View File

@ -1709,22 +1709,24 @@ class PolicyTests(unittest.TestCase):
self.assertIsNone(policy._watcher) self.assertIsNone(policy._watcher)
unix_events.can_use_pidfd = mock.Mock() unix_events.can_use_pidfd = mock.Mock()
unix_events.can_use_pidfd.return_value = False unix_events.can_use_pidfd.return_value = False
with self.assertWarns(DeprecationWarning):
watcher = policy.get_child_watcher() watcher = policy.get_child_watcher()
self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher)
self.assertIs(policy._watcher, watcher) self.assertIs(policy._watcher, watcher)
with self.assertWarns(DeprecationWarning):
self.assertIs(watcher, policy.get_child_watcher()) self.assertIs(watcher, policy.get_child_watcher())
policy = self.create_policy() policy = self.create_policy()
self.assertIsNone(policy._watcher) self.assertIsNone(policy._watcher)
unix_events.can_use_pidfd = mock.Mock() unix_events.can_use_pidfd = mock.Mock()
unix_events.can_use_pidfd.return_value = True unix_events.can_use_pidfd.return_value = True
with self.assertWarns(DeprecationWarning):
watcher = policy.get_child_watcher() watcher = policy.get_child_watcher()
self.assertIsInstance(watcher, asyncio.PidfdChildWatcher) self.assertIsInstance(watcher, asyncio.PidfdChildWatcher)
self.assertIs(policy._watcher, watcher) self.assertIs(policy._watcher, watcher)
with self.assertWarns(DeprecationWarning):
self.assertIs(watcher, policy.get_child_watcher()) self.assertIs(watcher, policy.get_child_watcher())
def test_get_child_watcher_after_set(self): def test_get_child_watcher_after_set(self):
@ -1732,9 +1734,10 @@ class PolicyTests(unittest.TestCase):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning) warnings.simplefilter("ignore", DeprecationWarning)
watcher = asyncio.FastChildWatcher() watcher = asyncio.FastChildWatcher()
policy.set_child_watcher(watcher) policy.set_child_watcher(watcher)
self.assertIs(policy._watcher, watcher) self.assertIs(policy._watcher, watcher)
with self.assertWarns(DeprecationWarning):
self.assertIs(watcher, policy.get_child_watcher()) self.assertIs(watcher, policy.get_child_watcher())
def test_get_child_watcher_thread(self): def test_get_child_watcher_thread(self):

View File

@ -14,7 +14,7 @@ import sys
import threading import threading
import unittest import unittest
import weakref import weakref
import warnings
from unittest import mock from unittest import mock
from http.server import HTTPServer from http.server import HTTPServer
@ -544,6 +544,8 @@ class TestCase(unittest.TestCase):
policy = support.maybe_get_event_loop_policy() policy = support.maybe_get_event_loop_policy()
if policy is not None: if policy is not None:
try: try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
watcher = policy.get_child_watcher() watcher = policy.get_child_watcher()
except NotImplementedError: except NotImplementedError:
# watcher is not implemented by EventLoopPolicy, e.g. Windows # watcher is not implemented by EventLoopPolicy, e.g. Windows

View File

@ -0,0 +1 @@
Deprecated :meth:`asyncio.AbstractEventLoopPolicy.get_child_watcher` and :meth:`asyncio.AbstractEventLoopPolicy.set_child_watcher` methods to be removed in Python 3.14. Patch by Kumar Aditya.