Skip to content

Commit 72c10d3

Browse files
GH-98327: Reduce scope of catch_warnings() in _make_subprocess_transport (#98333)
Alas, warnings.catch_warnings() has global scope, not thread scope, so this is still not perfect, but it reduces the time during which warnings are ignored. Better solution welcome.
1 parent 6da1a2e commit 72c10d3

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

Lib/asyncio/unix_events.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,31 @@ async def _make_subprocess_transport(self, protocol, args, shell,
197197
extra=None, **kwargs):
198198
with warnings.catch_warnings():
199199
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
200+
watcher = events.get_child_watcher()
201+
202+
with watcher:
203+
if not watcher.is_active():
204+
# Check early.
205+
# Raising exception before process creation
206+
# prevents subprocess execution if the watcher
207+
# is not ready to handle it.
208+
raise RuntimeError("asyncio.get_child_watcher() is not activated, "
209+
"subprocess support is not installed.")
210+
waiter = self.create_future()
211+
transp = _UnixSubprocessTransport(self, protocol, args, shell,
212+
stdin, stdout, stderr, bufsize,
213+
waiter=waiter, extra=extra,
214+
**kwargs)
215+
watcher.add_child_handler(transp.get_pid(),
216+
self._child_watcher_callback, transp)
217+
try:
218+
await waiter
219+
except (SystemExit, KeyboardInterrupt):
220+
raise
221+
except BaseException:
222+
transp.close()
223+
await transp._wait()
224+
raise
224225

225226
return transp
226227

Lib/test/test_asyncio/test_subprocess.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -752,15 +752,11 @@ def _get_watcher(self):
752752
class GenericWatcherTests(test_utils.TestCase):
753753

754754
def test_create_subprocess_fails_with_inactive_watcher(self):
755-
watcher = mock.create_autospec(
756-
asyncio.AbstractChildWatcher,
757-
**{"__enter__.return_value.is_active.return_value": False}
758-
)
755+
watcher = mock.create_autospec(asyncio.AbstractChildWatcher)
756+
watcher.is_active.return_value = False
759757

760758
async def execute():
761-
with warnings.catch_warnings():
762-
warnings.simplefilter('ignore', DeprecationWarning)
763-
asyncio.set_child_watcher(watcher)
759+
asyncio.set_child_watcher(watcher)
764760

765761
with self.assertRaises(RuntimeError):
766762
await subprocess.create_subprocess_exec(
@@ -774,9 +770,9 @@ async def execute():
774770
self.assertIsNone(runner.run(execute()))
775771
self.assertListEqual(watcher.mock_calls, [
776772
mock.call.__enter__(),
777-
mock.call.__enter__().is_active(),
773+
mock.call.is_active(),
778774
mock.call.__exit__(RuntimeError, mock.ANY, mock.ANY),
779-
])
775+
], watcher.mock_calls)
780776

781777

782778
@unittest.skipUnless(

0 commit comments

Comments
 (0)