Skip to content

Commit 5680f65

Browse files
csabellavstinner
authored andcommitted
bpo-18283: Add support for bytes to shutil.which (GH-11818)
1 parent cfd31f0 commit 5680f65

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎Doc/library/shutil.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ Directory and files operations
396396

397397
.. versionadded:: 3.3
398398

399+
.. versionchanged:: 3.8
400+
The :class:`bytes` type is now accepted. If *cmd* type is
401+
:class:`bytes`, the result type is also :class:`bytes`.
399402

400403
.. exception:: Error
401404

‎Lib/shutil.py‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,15 @@ def get_terminal_size(fallback=(80, 24)):
12791279

12801280
return os.terminal_size((columns, lines))
12811281

1282+
1283+
# Check that a given file can be accessed with the correct mode.
1284+
# Additionally check that `file` is not a directory, as on Windows
1285+
# directories pass the os.access check.
1286+
def _access_check(fn, mode):
1287+
return (os.path.exists(fn) and os.access(fn, mode)
1288+
and not os.path.isdir(fn))
1289+
1290+
12821291
def which(cmd, mode=os.F_OK | os.X_OK, path=None):
12831292
"""Given a command, mode, and a PATH string, return the path which
12841293
conforms to the given mode on the PATH, or None if there is no such
@@ -1289,13 +1298,6 @@ def which(cmd, mode=os.F_OK | os.X_OK, path=None):
12891298
path.
12901299
12911300
"""
1292-
# Check that a given file can be accessed with the correct mode.
1293-
# Additionally check that `file` is not a directory, as on Windows
1294-
# directories pass the os.access check.
1295-
def _access_check(fn, mode):
1296-
return (os.path.exists(fn) and os.access(fn, mode)
1297-
and not os.path.isdir(fn))
1298-
12991301
# If we're given a path with a directory part, look it up directly rather
13001302
# than referring to PATH directories. This includes checking relative to the
13011303
# current directory, e.g. ./script
@@ -1304,19 +1306,31 @@ def _access_check(fn, mode):
13041306
return cmd
13051307
return None
13061308

1309+
use_bytes = isinstance(cmd, bytes)
1310+
13071311
if path is None:
13081312
path = os.environ.get("PATH", os.defpath)
13091313
if not path:
13101314
return None
1311-
path = path.split(os.pathsep)
1315+
if use_bytes:
1316+
path = os.fsencode(path)
1317+
path = path.split(os.fsencode(os.pathsep))
1318+
else:
1319+
path = os.fsdecode(path)
1320+
path = path.split(os.pathsep)
13121321

13131322
if sys.platform == "win32":
13141323
# The current directory takes precedence on Windows.
1315-
if not os.curdir in path:
1316-
path.insert(0, os.curdir)
1324+
curdir = os.curdir
1325+
if use_bytes:
1326+
curdir = os.fsencode(curdir)
1327+
if curdir not in path:
1328+
path.insert(0, curdir)
13171329

13181330
# PATHEXT is necessary to check on Windows.
13191331
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
1332+
if use_bytes:
1333+
pathext = [os.fsencode(ext) for ext in pathext]
13201334
# See if the given file matches any of the expected path extensions.
13211335
# This will allow us to short circuit when given "python.exe".
13221336
# If it does match, only test that one, otherwise we have to try

‎Lib/test/test_shutil.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,9 @@ def setUp(self):
15171517
os.chmod(self.temp_file.name, stat.S_IXUSR)
15181518
self.addCleanup(self.temp_file.close)
15191519
self.dir, self.file = os.path.split(self.temp_file.name)
1520+
self.env_path = self.dir
1521+
self.curdir = os.curdir
1522+
self.ext = ".EXE"
15201523

15211524
def test_basic(self):
15221525
# Given an EXE in a directory, it should be returned.
@@ -1549,7 +1552,7 @@ def test_cwd(self):
15491552
rv = shutil.which(self.file, path=base_dir)
15501553
if sys.platform == "win32":
15511554
# Windows: current directory implicitly on PATH
1552-
self.assertEqual(rv, os.path.join(os.curdir, self.file))
1555+
self.assertEqual(rv, os.path.join(self.curdir, self.file))
15531556
else:
15541557
# Other platforms: shouldn't match in the current directory.
15551558
self.assertIsNone(rv)
@@ -1581,19 +1584,19 @@ def test_pathext_checking(self):
15811584
# Ask for the file without the ".exe" extension, then ensure that
15821585
# it gets found properly with the extension.
15831586
rv = shutil.which(self.file[:-4], path=self.dir)
1584-
self.assertEqual(rv, self.temp_file.name[:-4] + ".EXE")
1587+
self.assertEqual(rv, self.temp_file.name[:-4] + self.ext)
15851588

15861589
def test_environ_path(self):
15871590
with support.EnvironmentVarGuard() as env:
1588-
env['PATH'] = self.dir
1591+
env['PATH'] = self.env_path
15891592
rv = shutil.which(self.file)
15901593
self.assertEqual(rv, self.temp_file.name)
15911594

15921595
def test_empty_path(self):
15931596
base_dir = os.path.dirname(self.dir)
15941597
with support.change_cwd(path=self.dir), \
15951598
support.EnvironmentVarGuard() as env:
1596-
env['PATH'] = self.dir
1599+
env['PATH'] = self.env_path
15971600
rv = shutil.which(self.file, path='')
15981601
self.assertIsNone(rv)
15991602

@@ -1604,6 +1607,16 @@ def test_empty_path_no_PATH(self):
16041607
self.assertIsNone(rv)
16051608

16061609

1610+
class TestWhichBytes(TestWhich):
1611+
def setUp(self):
1612+
TestWhich.setUp(self)
1613+
self.dir = os.fsencode(self.dir)
1614+
self.file = os.fsencode(self.file)
1615+
self.temp_file.name = os.fsencode(self.temp_file.name)
1616+
self.curdir = os.fsencode(self.curdir)
1617+
self.ext = os.fsencode(self.ext)
1618+
1619+
16071620
class TestMove(unittest.TestCase):
16081621

16091622
def setUp(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for bytes to :func:`shutil.which`.

0 commit comments

Comments
 (0)