Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions Lib/distutils/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
executable name.
"""

import sys
import os
import shutil
import sys

from distutils.errors import DistutilsPlatformError, DistutilsExecError
from distutils.debug import DEBUG
Expand Down Expand Up @@ -172,21 +173,4 @@ def find_executable(executable, path=None):
A string listing directories separated by 'os.pathsep'; defaults to
os.environ['PATH']. Returns the complete filename or None if not found.
"""
if path is None:
path = os.environ['PATH']

paths = path.split(os.pathsep)
base, ext = os.path.splitext(executable)

if (sys.platform == 'win32') and (ext != '.exe'):
executable = executable + '.exe'

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
else:
return executable
return shutil.which(executable, path=path)
23 changes: 20 additions & 3 deletions Lib/distutils/tests/test_spawn.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Tests for distutils.spawn."""
import unittest
import sys
import os
from test.support import run_unittest, unix_shell
import stat
import sys
import unittest
from test.support import run_unittest, unix_shell, temp_dir

from distutils.spawn import find_executable
from distutils.spawn import _nt_quote_args
from distutils.spawn import spawn
from distutils.errors import DistutilsExecError
Expand Down Expand Up @@ -51,6 +53,21 @@ def test_spawn(self):
os.chmod(exe, 0o777)
spawn([exe]) # should work without any error

def test_find_executable(self):
with temp_dir() as tmp_dir:
# Give the temporary program an ".exe" suffix for all.
# It's needed on Windows and not harmful on other platforms.
program = "testprogram" + ".exe"

filename = os.path.join(tmp_dir, program)
with open(filename, "wb"):
pass
os.chmod(filename, stat.S_IXUSR)

rv = find_executable(program, path=tmp_dir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add also a negative test. find_executable(program) should return None.

self.assertEqual(rv, filename)


def test_suite():
return unittest.makeSuite(SpawnTestCase)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
distutils.spawn.find_executable() has been reimplemented using

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add links? :func:`distutils.spawn.find_executable`

shutil.which() to fix two issues: fallback on os.defpath if the PATH
environment variable is not set, and respect the PATHEXT environment
variable on Windows.