Skip to content

Commit 2dc5d61

Browse files
deprecate config methods
1 parent 0895c2a commit 2dc5d61

File tree

6 files changed

+85
-54
lines changed

6 files changed

+85
-54
lines changed

Lib/asyncio/unix_events.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,34 @@ def _make_write_pipe_transport(self, pipe, protocol, waiter=None,
195195
async def _make_subprocess_transport(self, protocol, args, shell,
196196
stdin, stdout, stderr, bufsize,
197197
extra=None, **kwargs):
198-
with events.get_child_watcher() as watcher:
199-
if not watcher.is_active():
200-
# Check early.
201-
# Raising exception before process creation
202-
# prevents subprocess execution if the watcher
203-
# is not ready to handle it.
204-
raise RuntimeError("asyncio.get_child_watcher() is not activated, "
205-
"subprocess support is not installed.")
206-
waiter = self.create_future()
207-
transp = _UnixSubprocessTransport(self, protocol, args, shell,
208-
stdin, stdout, stderr, bufsize,
209-
waiter=waiter, extra=extra,
210-
**kwargs)
211-
212-
watcher.add_child_handler(transp.get_pid(),
213-
self._child_watcher_callback, transp)
214-
try:
215-
await waiter
216-
except (SystemExit, KeyboardInterrupt):
217-
raise
218-
except BaseException:
219-
transp.close()
220-
await transp._wait()
221-
raise
198+
with warnings.catch_warnings():
199+
warnings.simplefilter('ignore', DeprecationWarning)
200+
with events.get_child_watcher() as watcher:
201+
if not watcher.is_active():
202+
# Check early.
203+
# Raising exception before process creation
204+
# prevents subprocess execution if the watcher
205+
# is not ready to handle it.
206+
raise RuntimeError("asyncio.get_child_watcher() is not activated, "
207+
"subprocess support is not installed.")
208+
waiter = self.create_future()
209+
transp = _UnixSubprocessTransport(self, protocol, args, shell,
210+
stdin, stdout, stderr, bufsize,
211+
waiter=waiter, extra=extra,
212+
**kwargs)
213+
214+
watcher.add_child_handler(transp.get_pid(),
215+
self._child_watcher_callback, transp)
216+
try:
217+
await waiter
218+
except (SystemExit, KeyboardInterrupt):
219+
raise
220+
except BaseException:
221+
transp.close()
222+
await transp._wait()
223+
raise
222224

223-
return transp
225+
return transp
224226

225227
def _child_watcher_callback(self, pid, returncode, transp):
226228
# Skip one iteration for callbacks to be executed
@@ -1470,17 +1472,22 @@ def get_child_watcher(self):
14701472
if self._watcher is None:
14711473
self._init_watcher()
14721474

1475+
warnings._deprecated("get_child_watcher",
1476+
"{name!r} is deprecated as of Python 3.12 and will be "
1477+
"removed in Python {remove}.", remove=(3, 14))
14731478
return self._watcher
14741479

14751480
def set_child_watcher(self, watcher):
14761481
"""Set the watcher for child processes."""
14771482

14781483
assert watcher is None or isinstance(watcher, AbstractChildWatcher)
1479-
14801484
if self._watcher is not None:
14811485
self._watcher.close()
14821486

14831487
self._watcher = watcher
1488+
warnings._deprecated("set_child_watcher",
1489+
"{name!r} is deprecated as of Python 3.12 and will be "
1490+
"removed in Python {remove}.", remove=(3, 14))
14841491

14851492

14861493
SelectorEventLoop = _UnixSelectorEventLoop

Lib/test/test_asyncio/test_events.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,11 +2058,13 @@ def setUp(self):
20582058
with warnings.catch_warnings():
20592059
warnings.simplefilter('ignore', DeprecationWarning)
20602060
watcher = asyncio.SafeChildWatcher()
2061-
watcher.attach_loop(self.loop)
2062-
asyncio.set_child_watcher(watcher)
2061+
watcher.attach_loop(self.loop)
2062+
asyncio.set_child_watcher(watcher)
20632063

20642064
def tearDown(self):
2065-
asyncio.set_child_watcher(None)
2065+
with warnings.catch_warnings():
2066+
warnings.simplefilter('ignore', DeprecationWarning)
2067+
asyncio.set_child_watcher(None)
20662068
super().tearDown()
20672069

20682070

@@ -2657,13 +2659,15 @@ def setUp(self):
26572659
with warnings.catch_warnings():
26582660
warnings.simplefilter('ignore', DeprecationWarning)
26592661
watcher = asyncio.SafeChildWatcher()
2660-
watcher.attach_loop(self.loop)
2661-
asyncio.set_child_watcher(watcher)
2662+
watcher.attach_loop(self.loop)
2663+
asyncio.set_child_watcher(watcher)
26622664

26632665
def tearDown(self):
26642666
try:
26652667
if sys.platform != 'win32':
2666-
asyncio.set_child_watcher(None)
2668+
with warnings.catch_warnings():
2669+
warnings.simplefilter('ignore', DeprecationWarning)
2670+
asyncio.set_child_watcher(None)
26672671

26682672
super().tearDown()
26692673
finally:

Lib/test/test_asyncio/test_streams.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,15 +797,19 @@ def test_read_all_from_pipe_reader(self):
797797
watcher = asyncio.SafeChildWatcher()
798798
watcher.attach_loop(self.loop)
799799
try:
800-
asyncio.set_child_watcher(watcher)
800+
with warnings.catch_warnings():
801+
warnings.simplefilter('ignore', DeprecationWarning)
802+
asyncio.set_child_watcher(watcher)
801803
create = asyncio.create_subprocess_exec(
802804
*args,
803805
pass_fds={wfd},
804806
)
805807
proc = self.loop.run_until_complete(create)
806808
self.loop.run_until_complete(proc.wait())
807809
finally:
808-
asyncio.set_child_watcher(None)
810+
with warnings.catch_warnings():
811+
warnings.simplefilter('ignore', DeprecationWarning)
812+
asyncio.set_child_watcher(None)
809813

810814
os.close(wfd)
811815
data = self.loop.run_until_complete(reader.read(-1))

Lib/test/test_asyncio/test_subprocess.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ async def kill_running():
562562
# manually to avoid a warning when the watcher is detached.
563563
if (sys.platform != 'win32' and
564564
isinstance(self, SubprocessFastWatcherTests)):
565-
asyncio.get_child_watcher()._callbacks.clear()
565+
with warnings.catch_warnings():
566+
warnings.simplefilter('ignore', DeprecationWarning)
567+
asyncio.get_child_watcher()._callbacks.clear()
566568

567569
async def _test_popen_error(self, stdin):
568570
if sys.platform == 'win32':
@@ -676,13 +678,17 @@ def setUp(self):
676678

677679
watcher = self._get_watcher()
678680
watcher.attach_loop(self.loop)
679-
policy.set_child_watcher(watcher)
681+
with warnings.catch_warnings():
682+
warnings.simplefilter('ignore', DeprecationWarning)
683+
policy.set_child_watcher(watcher)
680684

681685
def tearDown(self):
682686
super().tearDown()
683687
policy = asyncio.get_event_loop_policy()
684-
watcher = policy.get_child_watcher()
685-
policy.set_child_watcher(None)
688+
with warnings.catch_warnings():
689+
warnings.simplefilter('ignore', DeprecationWarning)
690+
watcher = policy.get_child_watcher()
691+
policy.set_child_watcher(None)
686692
watcher.attach_loop(None)
687693
watcher.close()
688694

@@ -732,7 +738,9 @@ def test_create_subprocess_fails_with_inactive_watcher(self):
732738
)
733739

734740
async def execute():
735-
asyncio.set_child_watcher(watcher)
741+
with warnings.catch_warnings():
742+
warnings.simplefilter('ignore', DeprecationWarning)
743+
asyncio.set_child_watcher(watcher)
736744

737745
with self.assertRaises(RuntimeError):
738746
await subprocess.create_subprocess_exec(
@@ -741,7 +749,9 @@ async def execute():
741749
watcher.add_child_handler.assert_not_called()
742750

743751
with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner:
744-
self.assertIsNone(runner.run(execute()))
752+
with warnings.catch_warnings():
753+
warnings.simplefilter('ignore', DeprecationWarning)
754+
self.assertIsNone(runner.run(execute()))
745755
self.assertListEqual(watcher.mock_calls, [
746756
mock.call.__enter__(),
747757
mock.call.__enter__().is_active(),
@@ -768,15 +778,16 @@ async def main():
768778
with self.assertRaises(RuntimeError):
769779
asyncio.get_event_loop_policy().get_event_loop()
770780
return await asyncio.to_thread(asyncio.run, in_thread())
771-
772-
asyncio.set_child_watcher(asyncio.PidfdChildWatcher())
781+
with self.assertWarns(DeprecationWarning):
782+
asyncio.set_child_watcher(asyncio.PidfdChildWatcher())
773783
try:
774784
with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner:
775785
returncode, stdout = runner.run(main())
776786
self.assertEqual(returncode, 0)
777787
self.assertEqual(stdout, b'some data')
778788
finally:
779-
asyncio.set_child_watcher(None)
789+
with self.assertWarns(DeprecationWarning):
790+
asyncio.set_child_watcher(None)
780791
else:
781792
# Windows
782793
class SubprocessProactorTests(SubprocessMixin, test_utils.TestCase):

Lib/test/test_asyncio/test_unix_events.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,33 +1709,36 @@ def test_get_default_child_watcher(self):
17091709
self.assertIsNone(policy._watcher)
17101710
unix_events.can_use_pidfd = mock.Mock()
17111711
unix_events.can_use_pidfd.return_value = False
1712-
watcher = policy.get_child_watcher()
1712+
with self.assertWarns(DeprecationWarning):
1713+
watcher = policy.get_child_watcher()
17131714
self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher)
17141715

17151716
self.assertIs(policy._watcher, watcher)
1716-
1717-
self.assertIs(watcher, policy.get_child_watcher())
1717+
with self.assertWarns(DeprecationWarning):
1718+
self.assertIs(watcher, policy.get_child_watcher())
17181719

17191720
policy = self.create_policy()
17201721
self.assertIsNone(policy._watcher)
17211722
unix_events.can_use_pidfd = mock.Mock()
17221723
unix_events.can_use_pidfd.return_value = True
1723-
watcher = policy.get_child_watcher()
1724+
with self.assertWarns(DeprecationWarning):
1725+
watcher = policy.get_child_watcher()
17241726
self.assertIsInstance(watcher, asyncio.PidfdChildWatcher)
17251727

17261728
self.assertIs(policy._watcher, watcher)
1727-
1728-
self.assertIs(watcher, policy.get_child_watcher())
1729+
with self.assertWarns(DeprecationWarning):
1730+
self.assertIs(watcher, policy.get_child_watcher())
17291731

17301732
def test_get_child_watcher_after_set(self):
17311733
policy = self.create_policy()
17321734
with warnings.catch_warnings():
17331735
warnings.simplefilter("ignore", DeprecationWarning)
17341736
watcher = asyncio.FastChildWatcher()
1737+
policy.set_child_watcher(watcher)
17351738

1736-
policy.set_child_watcher(watcher)
17371739
self.assertIs(policy._watcher, watcher)
1738-
self.assertIs(watcher, policy.get_child_watcher())
1740+
with self.assertWarns(DeprecationWarning):
1741+
self.assertIs(watcher, policy.get_child_watcher())
17391742

17401743
def test_get_child_watcher_thread(self):
17411744

@@ -1769,7 +1772,7 @@ def test_child_watcher_replace_mainloop_existing(self):
17691772
with warnings.catch_warnings():
17701773
warnings.simplefilter("ignore", DeprecationWarning)
17711774
watcher = asyncio.SafeChildWatcher()
1772-
policy.set_child_watcher(watcher)
1775+
policy.set_child_watcher(watcher)
17731776
watcher.attach_loop(loop)
17741777

17751778
self.assertIs(watcher._loop, loop)

Lib/test/test_asyncio/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import threading
1515
import unittest
1616
import weakref
17-
17+
import warnings
1818
from unittest import mock
1919

2020
from http.server import HTTPServer
@@ -544,7 +544,9 @@ def close_loop(loop):
544544
policy = support.maybe_get_event_loop_policy()
545545
if policy is not None:
546546
try:
547-
watcher = policy.get_child_watcher()
547+
with warnings.catch_warnings():
548+
warnings.simplefilter('ignore', DeprecationWarning)
549+
watcher = policy.get_child_watcher()
548550
except NotImplementedError:
549551
# watcher is not implemented by EventLoopPolicy, e.g. Windows
550552
pass

0 commit comments

Comments
 (0)