@@ -304,6 +304,18 @@ def test_executable(self):
304304 "doesnotexist" )
305305 self ._assert_python ([doesnotexist , "-c" ], executable = sys .executable )
306306
307+ def test_bytes_executable (self ):
308+ doesnotexist = os .path .join (os .path .dirname (sys .executable ),
309+ "doesnotexist" )
310+ self ._assert_python ([doesnotexist , "-c" ],
311+ executable = os .fsencode (sys .executable ))
312+
313+ def test_pathlike_executable (self ):
314+ doesnotexist = os .path .join (os .path .dirname (sys .executable ),
315+ "doesnotexist" )
316+ self ._assert_python ([doesnotexist , "-c" ],
317+ executable = FakePath (sys .executable ))
318+
307319 def test_executable_takes_precedence (self ):
308320 # Check that the executable argument takes precedence over args[0].
309321 #
@@ -320,6 +332,16 @@ def test_executable_replaces_shell(self):
320332 # when shell=True.
321333 self ._assert_python ([], executable = sys .executable , shell = True )
322334
335+ @unittest .skipIf (mswindows , "executable argument replaces shell" )
336+ def test_bytes_executable_replaces_shell (self ):
337+ self ._assert_python ([], executable = os .fsencode (sys .executable ),
338+ shell = True )
339+
340+ @unittest .skipIf (mswindows , "executable argument replaces shell" )
341+ def test_pathlike_executable_replaces_shell (self ):
342+ self ._assert_python ([], executable = FakePath (sys .executable ),
343+ shell = True )
344+
323345 # For use in the test_cwd* tests below.
324346 def _normalize_cwd (self , cwd ):
325347 # Normalize an expected cwd (for Tru64 support).
@@ -358,6 +380,11 @@ def test_cwd(self):
358380 temp_dir = self ._normalize_cwd (temp_dir )
359381 self ._assert_cwd (temp_dir , sys .executable , cwd = temp_dir )
360382
383+ def test_cwd_with_bytes (self ):
384+ temp_dir = tempfile .gettempdir ()
385+ temp_dir = self ._normalize_cwd (temp_dir )
386+ self ._assert_cwd (temp_dir , sys .executable , cwd = os .fsencode (temp_dir ))
387+
361388 def test_cwd_with_pathlike (self ):
362389 temp_dir = tempfile .gettempdir ()
363390 temp_dir = self ._normalize_cwd (temp_dir )
@@ -1473,6 +1500,34 @@ def test_run_kwargs(self):
14731500 env = newenv )
14741501 self .assertEqual (cp .returncode , 33 )
14751502
1503+ def test_run_with_pathlike_path (self ):
1504+ # bpo-31961: test run(pathlike_object)
1505+ # the name of a command that can be run without
1506+ # any argumenets that exit fast
1507+ prog = 'tree.com' if mswindows else 'ls'
1508+ path = shutil .which (prog )
1509+ if path is None :
1510+ self .skipTest (f'{ prog } required for this test' )
1511+ path = FakePath (path )
1512+ res = subprocess .run (path , stdout = subprocess .DEVNULL )
1513+ self .assertEqual (res .returncode , 0 )
1514+ with self .assertRaises (TypeError ):
1515+ subprocess .run (path , stdout = subprocess .DEVNULL , shell = True )
1516+
1517+ def test_run_with_bytes_path_and_arguments (self ):
1518+ # bpo-31961: test run([bytes_object, b'additional arguments'])
1519+ path = os .fsencode (sys .executable )
1520+ args = [path , '-c' , b'import sys; sys.exit(57)' ]
1521+ res = subprocess .run (args )
1522+ self .assertEqual (res .returncode , 57 )
1523+
1524+ def test_run_with_pathlike_path_and_arguments (self ):
1525+ # bpo-31961: test run([pathlike_object, 'additional arguments'])
1526+ path = FakePath (sys .executable )
1527+ args = [path , '-c' , 'import sys; sys.exit(57)' ]
1528+ res = subprocess .run (args )
1529+ self .assertEqual (res .returncode , 57 )
1530+
14761531 def test_capture_output (self ):
14771532 cp = self .run_python (("import sys;"
14781533 "sys.stdout.write('BDFL'); "
0 commit comments