Skip to content

Commit 7056ca8

Browse files
authored
bpo-34530: Fix distutils find_executable() (GH-9049) (GH-9058)
distutils.spawn.find_executable() now falls back on os.defpath if the PATH environment variable is not set. (cherry picked from commit 3948719)
1 parent 30af2e7 commit 7056ca8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Lib/distutils/spawn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ def find_executable(executable, path=None):
208208
os.environ['PATH']. Returns the complete filename or None if not found.
209209
"""
210210
if path is None:
211-
path = os.environ['PATH']
211+
path = os.environ.get('PATH', os.defpath)
212+
212213
paths = path.split(os.pathsep)
213214
base, ext = os.path.splitext(executable)
214215

Lib/distutils/tests/test_spawn.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""Tests for distutils.spawn."""
2-
import unittest
32
import os
3+
import stat
4+
import sys
45
import time
5-
from test.test_support import captured_stdout, run_unittest
6+
import unittest
7+
from test.support import captured_stdout, run_unittest
8+
from test import support as test_support
69

710
from distutils.spawn import _nt_quote_args
811
from distutils.spawn import spawn, find_executable
@@ -53,6 +56,48 @@ def test_spawn(self):
5356
os.chmod(exe, 0777)
5457
spawn([exe]) # should work without any error
5558

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

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)