@@ -2636,12 +2636,50 @@ def mock_execve(name, *args):
26362636
26372637@unittest .skipUnless (hasattr (os , 'execv' ),
26382638 "need os.execv()" )
2639+ @unittest .skipIf (support .is_emscripten ,
2640+ "Emscripten always fails with ENOEXEC" )
2641+ @unittest .skipIf (support .is_android ,
2642+ "PATH contains an inaccessible directory on Android" )
26392643class ExecTests (unittest .TestCase ):
2640- @unittest .skipIf (USING_LINUXTHREADS ,
2641- "avoid triggering a linuxthreads bug: see issue #4970" )
2644+ def _test_bad_program (self , do_exec , exc_type = OSError ):
2645+ bad_filenames = ['nosuchapp' , FakePath ('nosuchapp' )]
2646+ if os .name != 'nt' :
2647+ # Bytes program names are not supported on Windows.
2648+ bad_filenames += [b'nosuchapp' , FakePath (b'nosuchapp' )]
2649+ for bad_filename in bad_filenames :
2650+ with self .subTest (bad_filename ):
2651+ with self .assertRaises (exc_type ) as ctx :
2652+ do_exec (bad_filename )
2653+ self .assertEqual (ctx .exception .filename ,
2654+ os .fspath (bad_filename ))
2655+ self .assertIn ('nosuchapp' , str (ctx .exception ))
2656+
2657+ @unittest .skipIf (USING_LINUXTHREADS , "linuxthreads bug: see issue #4970" )
2658+ def test_execv_with_bad_program (self ):
2659+ self ._test_bad_program (lambda name : os .execv (name , ['nosuchapp' ]))
2660+
2661+ @unittest .skipIf (USING_LINUXTHREADS , "linuxthreads bug: see issue #4970" )
2662+ def test_execvp_with_bad_program (self ):
2663+ self ._test_bad_program (lambda name : os .execvp (name , ['nosuchapp' ]))
2664+
2665+ @unittest .skipIf (USING_LINUXTHREADS , "linuxthreads bug: see issue #4970" )
2666+ def test_execve_with_bad_program (self ):
2667+ self ._test_bad_program (lambda name : os .execve (name , ['nosuchapp' ], {}))
2668+
2669+ @unittest .skipIf (USING_LINUXTHREADS , "linuxthreads bug: see issue #4970" )
26422670 def test_execvpe_with_bad_program (self ):
2643- self .assertRaises (OSError , os .execvpe , 'no such app-' ,
2644- ['no such app-' ], None )
2671+ self ._test_bad_program (lambda name : os .execvpe (name , ['nosuchapp' ], {}))
2672+
2673+ @unittest .skipUnless (os .name == 'posix' , 'POSIX specific test' )
2674+ @unittest .skipIf (USING_LINUXTHREADS , "linuxthreads bug: see issue #4970" )
2675+ def test_execvp_with_bad_path_entry (self ):
2676+ # A regular file in PATH makes the exec fail with ENOTDIR.
2677+ create_file (os_helper .TESTFN )
2678+ self .addCleanup (os_helper .unlink , os_helper .TESTFN )
2679+ with os_helper .EnvironmentVarGuard () as env :
2680+ env ['PATH' ] = os .path .abspath (os_helper .TESTFN )
2681+ self ._test_bad_program (lambda name : os .execvp (name , ['nosuchapp' ]),
2682+ NotADirectoryError )
26452683
26462684 def test_execv_with_bad_arglist (self ):
26472685 self .assertRaises (ValueError , os .execv , 'notepad' , ())
0 commit comments