summaryrefslogtreecommitdiffstats
path: root/Lib/subprocess.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-02-09 09:59:40 (GMT)
committerGitHub <noreply@github.com>2023-02-09 09:59:40 (GMT)
commit04cc4270251e4e76e210d032cea966cedbd486e8 (patch)
tree3ed99369ae91441ba1a80bcd5b0c09221c2008d0 /Lib/subprocess.py
parentc33aaa9d559398bbf2b80e891bf3ae6a716e4b8c (diff)
downloadcpython-04cc4270251e4e76e210d032cea966cedbd486e8.zip
cpython-04cc4270251e4e76e210d032cea966cedbd486e8.tar.gz
cpython-04cc4270251e4e76e210d032cea966cedbd486e8.tar.bz2
[3.9] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) (#101709)
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net> Co-authored-by: Steve Dower <steve.dower@microsoft.com>
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r--Lib/subprocess.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 4effc1d..8579d3f 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1407,7 +1407,23 @@ class Popen(object):
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
+ else:
+ comspec = executable
+
args = '{} /c "{}"'.format (comspec, args)
if cwd is not None: