diff options
author | Oleg Iarygin <oleg@arhadthedev.net> | 2023-02-08 22:12:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-08 22:12:19 (GMT) |
commit | 23751ed826ee63fb486e874ec25934ea87dd8519 (patch) | |
tree | 216a34dc2d9f780eb8c3b46b22a6d232dd986907 /Lib/subprocess.py | |
parent | de3669ebcb33ca8e3373fbbaed646c5f287979b8 (diff) | |
download | cpython-23751ed826ee63fb486e874ec25934ea87dd8519.zip cpython-23751ed826ee63fb486e874ec25934ea87dd8519.tar.gz cpython-23751ed826ee63fb486e874ec25934ea87dd8519.tar.bz2 |
gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286)
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 9cadd1b..fa527d5 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1480,7 +1480,21 @@ class Popen: if shell: startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW startupinfo.wShowWindow = _winapi.SW_HIDE - comspec = os.environ.get("COMSPEC", "cmd.exe") + if not executable: + # gh-101283: without a fully-qualified path, before Windows + # checks the system directories, it first looks in the + # application directory, and also the current directory if + # NeedCurrentDirectoryForExePathW(ExeName) is true, so try + # to avoid executing unqualified "cmd.exe". + comspec = os.environ.get('ComSpec') + if not comspec: + system_root = os.environ.get('SystemRoot', '') + comspec = os.path.join(system_root, 'System32', 'cmd.exe') + if not os.path.isabs(comspec): + raise FileNotFoundError('shell not found: neither %ComSpec% nor %SystemRoot% is set') + if os.path.isabs(comspec): + executable = comspec + args = '{} /c "{}"'.format (comspec, args) if cwd is not None: |