Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def __init__(self, args, bufsize=-1, executable=None,
else:
# POSIX
if pass_fds and not close_fds:
warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)
warnings.warn("pass_fds overriding close_fds.", RuntimeWarning, stacklevel=2)
close_fds = True
if startupinfo is not None:
raise ValueError("startupinfo is only supported on Windows "
Expand Down Expand Up @@ -1567,7 +1567,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
if handle_list:
if not close_fds:
warnings.warn("startupinfo.lpAttributeList['handle_list'] "
"overriding close_fds", RuntimeWarning)
"overriding close_fds", RuntimeWarning,
stacklevel=3)

# When using the handle_list we always request to inherit
# handles but the only handles that will be inherited are
Expand Down
18 changes: 16 additions & 2 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3177,6 +3177,18 @@ def test_pass_fds(self):
close_fds=False, pass_fds=(fd, )))
self.assertIn('overriding close_fds', str(context.warning))

def test_pass_fds_overriding_close_fds_warning_location(self):
# gh-148402: the warning should point to the caller, not subprocess.py
fd = os.dup(1)
self.addCleanup(os.close, fd)
os.set_inheritable(fd, True)
with self.assertWarns(RuntimeWarning) as context:
p = subprocess.Popen(ZERO_RETURN_CMD,
close_fds=False, pass_fds=(fd,))
p.wait()
self.assertIn('overriding close_fds', str(context.warning))
self.assertEqual(context.filename, __file__)

def test_pass_fds_inheritable(self):
script = support.findfile("fd_status.py", subdir="subprocessdata")

Expand Down Expand Up @@ -3762,8 +3774,7 @@ def test_close_fds_with_stdio(self):
self.assertIn(b"OSError", stderr)

# Check for a warning due to using handle_list and close_fds=False
with warnings_helper.check_warnings((".*overriding close_fds",
RuntimeWarning)):
with self.assertWarns(RuntimeWarning) as context:
startupinfo = subprocess.STARTUPINFO()
startupinfo.lpAttributeList = {"handle_list": handles[:]}
p = subprocess.Popen([sys.executable, "-c",
Expand All @@ -3772,6 +3783,9 @@ def test_close_fds_with_stdio(self):
startupinfo=startupinfo, close_fds=False)
stdout, stderr = p.communicate()
self.assertEqual(p.returncode, 0)
self.assertIn('overriding close_fds', str(context.warning))
# gh-148402: warning should point to the caller, not subprocess.py
self.assertEqual(context.filename, __file__)

def test_empty_attribute_list(self):
startupinfo = subprocess.STARTUPINFO()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add missing ``stacklevel`` to two :func:`warnings.warn` calls in
:class:`subprocess.Popen` so that warnings correctly point to the caller's
code instead of ``subprocess.py`` internals.
Loading