From 4b12de0ca768f12b5874fa837fd1a2478ee9e1e6 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Wed, 16 Aug 2017 11:07:08 +0100
Subject: [PATCH 05/11] Win32: distutils: Also look for executable.bat

This patch should probably not exist, or at least be
rewritten. See the comments.
---
 Lib/distutils/spawn.py | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/Lib/distutils/spawn.py b/Lib/distutils/spawn.py
index 321344a3a5..117c93441c 100644
--- a/Lib/distutils/spawn.py
+++ b/Lib/distutils/spawn.py
@@ -210,17 +210,33 @@ def find_executable(executable, path=None):
     if path is None:
         path = os.environ['PATH']
     paths = path.split(os.pathsep)
-    base, ext = os.path.splitext(executable)
-
-    if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
-        executable = executable + '.exe'
+    # base, ext = os.path.splitext(executable)
+
+    if (sys.platform == 'win32' or os.name == 'os2'):
+	# This should probably be:
+        # try:
+        #     exts = os.environ['PATHEXT'].lower().split(';')+['']
+        # except:
+        #     exts = ['.exe.', '.bat', '']
+        # but IMHO:
+        # 1. The original `ext` should appear as the first entry.
+        # 2. We should be adding `exts` to `base`, not `executable`
+        # 3. Why even bother? We do not add '.sh' for Unix.
+        exts = ['.exe', '.bat', '']
+    else:
+        exts = ['']
 
     if not os.path.isfile(executable):
-        for p in paths:
-            f = os.path.join(p, executable)
-            if os.path.isfile(f):
-                # the file exists, we have a shot at spawn working
-                return f
-        return None
+        for ext in exts:
+            newexe = executable + ext
+            if os.path.isfile(newexe):
+                return newexe
+            else:
+                for p in paths:
+                    f = os.path.join(p, newexe)
+                    if os.path.isfile(f):
+                        # the file exists, we have a shot at spawn working
+                        return f
     else:
         return executable
+    return None
-- 
2.13.6

