Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,8 @@ 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 +1568,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,19 @@ 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):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2. The warning message was already tested in test_pass_fds (line 3174: assertIn('overriding close_fds', str(context.warning))).

If the warning message was already tested, why not simply add an assertion for filename there?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — done. I removed the separate test and added the context.filename == __file__ assertion directly in test_pass_fds, right after the existing warning-message check.

# Verify the warning points to the caller, not subprocess.py.
r, w = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
os.set_inheritable(r, True)
with self.assertWarns(RuntimeWarning) as cm:
p = subprocess.Popen(
ZERO_RETURN_CMD,
close_fds=False, pass_fds=(r,))
p.wait()
self.assertEqual(cm.filename, __file__)

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

Expand Down Expand Up @@ -3762,8 +3775,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 cm:
startupinfo = subprocess.STARTUPINFO()
startupinfo.lpAttributeList = {"handle_list": handles[:]}
p = subprocess.Popen([sys.executable, "-c",
Expand All @@ -3772,6 +3784,8 @@ 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(cm.warning))
self.assertEqual(cm.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,2 @@
Warnings emitted by :class:`subprocess.Popen` now correctly report the
caller's source location instead of a line inside :mod:`subprocess`.
Loading