This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess.Popen: impossible to show console window when shell=True
Type: enhancement Stage: patch review
Components: Library (Lib), Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, akdor1154, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2020-06-03 04:54 by akdor1154, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 20975 open ZackerySpytz, 2020-06-19 05:53
Messages (2)
msg370639 - (view) Author: akdor1154 (akdor1154) Date: 2020-06-03 04:54
Hi all,
It seems impossible to show a new console window with calling subprocess.Popen on windows with shell=True.

Attempt:
    si = subprocess.STARTUPINFO()
    si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    si.wShowWindow = 5
    proc = Popen(
        cmd, cwd=runFolder, creationflags=subprocess.CREATE_NEW_CONSOLE, shell=True,
        startupinfo=si
    )

In the current source, it looks like this is due to the block in https://github.com/python/cpython/blob/master/Lib/subprocess.py#L1405 , which unreservedly wipes wShowWindow even if I have provided it.

Testing on Python 3.6 but I am assuming this affects all versions.
msg370641 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-06-03 05:33
I think you have the right idea for improving the behavior here. It should skip setting wShowWindow if startupinfo already has the flag STARTF_USESHOWWINDOW. For example:

    if shell:
        comspec = os.environ.get("COMSPEC", "cmd.exe")
        args = '{} /c "{}"'.format (comspec, args)
        if not startupinfo.dwFlags & _winapi.STARTF_USESHOWWINDOW:
            startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = _winapi.SW_HIDE
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 85028
2020-06-19 05:53:00ZackerySpytzsetkeywords: + patch
nosy: + ZackerySpytz

pull_requests: + pull_request20152
stage: needs patch -> patch review
2020-06-03 05:33:48eryksunsettype: enhancement
components: + Library (Lib)
versions: - Python 3.5, Python 3.6, Python 3.7
nosy: + eryksun

messages: + msg370641
stage: needs patch
2020-06-03 04:54:30akdor1154create