Commit 1d1c4629 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-35755: shutil.which() uses os.confstr("CS_PATH") (GH-12858)

shutil.which() and distutils.spawn.find_executable() now use
os.confstr("CS_PATH") if available instead of os.defpath, if the PATH
environment variable is not set.

Don't use os.confstr("CS_PATH") nor os.defpath if the PATH
environment variable is set to an empty string to mimick Unix 'which'
command behavior.

Changes:

* find_executable() now starts by checking for the executable in the
  current working directly case. Add an explicit
  "if not path: return None".
* Add tests for PATH='' (empty string), PATH=':' and for PATHEXT.
parent ef2792fe
......@@ -172,21 +172,32 @@ 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.get('PATH', os.defpath)
paths = path.split(os.pathsep)
base, ext = os.path.splitext(executable)
_, 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:
if os.path.isfile(executable):
return executable
if path is None:
path = os.environ.get('PATH', None)
if path is None:
try:
path = os.confstr("CS_PATH")
except (AttributeError, ValueError):
# os.confstr() or CS_PATH is not available
path = os.defpath
# bpo-35755: Don't use os.defpath if the PATH environment variable is
# set to an empty string to mimick Unix which command behavior
# PATH='' doesn't match, whereas PATH=':' looks in the current directory
if not path:
return None
paths = path.split(os.pathsep)
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
......@@ -87,11 +87,52 @@ class SpawnTestCase(support.TempdirManager,
rv = find_executable(dont_exist_program , path=tmp_dir)
self.assertIsNone(rv)
# test os.defpath: missing PATH environment variable
# PATH='': no match, except in the current directory
with test_support.EnvironmentVarGuard() as env:
with mock.patch('distutils.spawn.os.defpath', tmp_dir):
env.pop('PATH')
env['PATH'] = ''
with unittest.mock.patch('distutils.spawn.os.confstr',
return_value=tmp_dir, create=True), \
unittest.mock.patch('distutils.spawn.os.defpath',
tmp_dir):
rv = find_executable(program)
self.assertIsNone(rv)
# look in current directory
with test_support.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)
# PATH=':': explicitly looks in the current directory
with test_support.EnvironmentVarGuard() as env:
env['PATH'] = os.pathsep
with unittest.mock.patch('distutils.spawn.os.confstr',
return_value='', create=True), \
unittest.mock.patch('distutils.spawn.os.defpath', ''):
rv = find_executable(program)
self.assertIsNone(rv)
# look in current directory
with test_support.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)
# missing PATH: test os.confstr("CS_PATH") and os.defpath
with test_support.EnvironmentVarGuard() as env:
env.pop('PATH', None)
# without confstr
with unittest.mock.patch('distutils.spawn.os.confstr',
side_effect=ValueError,
create=True), \
unittest.mock.patch('distutils.spawn.os.defpath',
tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, filename)
# with confstr
with unittest.mock.patch('distutils.spawn.os.confstr',
return_value=tmp_dir, create=True), \
unittest.mock.patch('distutils.spawn.os.defpath', ''):
rv = find_executable(program)
self.assertEqual(rv, filename)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment