Skip to content

gh-148402: show caller's location of subprocess.Popen's warnings#148400

Open
stef41 wants to merge 2 commits intopython:mainfrom
stef41:fix-subprocess-missing-stacklevel
Open

gh-148402: show caller's location of subprocess.Popen's warnings#148400
stef41 wants to merge 2 commits intopython:mainfrom
stef41:fix-subprocess-missing-stacklevel

Conversation

@stef41
Copy link
Copy Markdown

@stef41 stef41 commented Apr 11, 2026

Summary

Both warnings.warn() calls in Popen.__init__ are missing stacklevel=2:

  1. POSIX path (line 915): Warning when pass_fds overrides close_fds
  2. Windows path (line 1569): Warning when startupinfo.lpAttributeList['handle_list'] overrides close_fds

Without stacklevel=2, the warning message points to an internal line inside subprocess.py rather than the caller's Popen() invocation, making it harder for users to locate the source of the warning in their own code.

Example

Before (without stacklevel):

/usr/lib/python3.14/subprocess.py:915: RuntimeWarning: pass_fds overriding close_fds.

After (with stacklevel=2):

my_script.py:42: RuntimeWarning: pass_fds overriding close_fds.

Changes

  • Add stacklevel=2 to both warnings.warn() calls in Popen.__init__

Both warnings.warn() calls in Popen.__init__ (one on POSIX for
pass_fds overriding close_fds, one on Windows for handle_list
overriding close_fds) were missing stacklevel=2.

Without stacklevel=2, the warning points to an internal line in
subprocess.py rather than the caller's Popen() invocation, making
it harder for users to locate the source of the warning in their
own code.
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 11, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Please add a regression test.

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 11, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz picnixz changed the title Add missing stacklevel to warnings.warn() in subprocess.Popen gh-148402: Add missing stacklevel to warnings.warn() in subprocess.Popen Apr 11, 2026
@picnixz picnixz changed the title gh-148402: Add missing stacklevel to warnings.warn() in subprocess.Popen gh-148402: show caller's location of subprocess.Popen's warnings Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants