Skip to content

Commit 7aa3ead

Browse files
bpo-34530: Fix distutils find_executable() (GH-9049)
distutils.spawn.find_executable() now falls back on os.defpath if the PATH environment variable is not set. (cherry picked from commit 3948719) Co-authored-by: Victor Stinner <[email protected]>
1 parent 1e92123 commit 7aa3ead

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

Lib/distutils/spawn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def find_executable(executable, path=None):
173173
os.environ['PATH']. Returns the complete filename or None if not found.
174174
"""
175175
if path is None:
176-
path = os.environ['PATH']
176+
path = os.environ.get('PATH', os.defpath)
177177

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

Lib/distutils/tests/test_spawn.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Tests for distutils.spawn."""
2-
import unittest
3-
import sys
42
import os
3+
import stat
4+
import sys
5+
import unittest
6+
from unittest import mock
57
from test.support import run_unittest, unix_shell
8+
from test import support as test_support
69

10+
from distutils.spawn import find_executable
711
from distutils.spawn import _nt_quote_args
812
from distutils.spawn import spawn
913
from distutils.errors import DistutilsExecError
@@ -51,6 +55,47 @@ def test_spawn(self):
5155
os.chmod(exe, 0o777)
5256
spawn([exe]) # should work without any error
5357

58+
def test_find_executable(self):
59+
with test_support.temp_dir() as tmp_dir:
60+
# use TESTFN to get a pseudo-unique filename
61+
program_noeext = test_support.TESTFN
62+
# Give the temporary program an ".exe" suffix for all.
63+
# It's needed on Windows and not harmful on other platforms.
64+
program = program_noeext + ".exe"
65+
66+
filename = os.path.join(tmp_dir, program)
67+
with open(filename, "wb"):
68+
pass
69+
os.chmod(filename, stat.S_IXUSR)
70+
71+
# test path parameter
72+
rv = find_executable(program, path=tmp_dir)
73+
self.assertEqual(rv, filename)
74+
75+
if sys.platform == 'win32':
76+
# test without ".exe" extension
77+
rv = find_executable(program_noeext, path=tmp_dir)
78+
self.assertEqual(rv, filename)
79+
80+
# test find in the current directory
81+
with test_support.change_cwd(tmp_dir):
82+
rv = find_executable(program)
83+
self.assertEqual(rv, program)
84+
85+
# test non-existent program
86+
dont_exist_program = "dontexist_" + program
87+
rv = find_executable(dont_exist_program , path=tmp_dir)
88+
self.assertIsNone(rv)
89+
90+
# test os.defpath: missing PATH environment variable
91+
with test_support.EnvironmentVarGuard() as env:
92+
with mock.patch('distutils.spawn.os.defpath', tmp_dir):
93+
env.pop('PATH')
94+
95+
rv = find_executable(program)
96+
self.assertEqual(rv, filename)
97+
98+
5499
def test_suite():
55100
return unittest.makeSuite(SpawnTestCase)
56101

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``distutils.spawn.find_executable()`` now falls back on :data:`os.defpath`
2+
if the ``PATH`` environment variable is not set.

0 commit comments

Comments
 (0)