Merge pull request #2727 from pypa/windows-run-non-executable

Improve "run" behavior on Windows
This commit is contained in:
Dan Ryan
2018-08-24 21:51:29 -04:00
committed by GitHub
2 changed files with 22 additions and 5 deletions
+1
View File
@@ -0,0 +1 @@
Fallback to shell mode if `run` fails with Windows error 193 to handle non-executable commands. This should improve usability on Windows, where some users run non-executable files without specifying a command, relying on Windows file association to choose the current command.
+21 -5
View File
@@ -2091,15 +2091,31 @@ def inline_activate_virtual_environment():
os.environ["VIRTUAL_ENV"] = root
def do_run_nt(script):
def _launch_windows_subprocess(script):
import subprocess
command = system_which(script.command)
options = {"universal_newlines": True}
if command: # Try to use CreateProcess directly if possible.
p = subprocess.Popen([command] + script.args, **options)
else: # Command not found, maybe this is a shell built-in?
p = subprocess.Popen(script.cmdify(), shell=True, **options)
# Command not found, maybe this is a shell built-in?
if not command:
return subprocess.Popen(script.cmdify(), shell=True, **options)
# Try to use CreateProcess directly if possible. Specifically catch
# Windows error 193 "Command is not a valid Win32 application" to handle
# a "command" that is non-executable. See pypa/pipenv#2727.
try:
return subprocess.Popen([command] + script.args, **options)
except WindowsError as e:
if e.winerror != 193:
raise
# Try shell mode to use Windows's file association for file launch.
return subprocess.Popen(script.cmdify(), shell=True, **options)
def do_run_nt(script):
p = _launch_windows_subprocess(script)
p.communicate()
sys.exit(p.returncode)