Skip to content

Commit 255dbd2

Browse files
miss-islingtonvstinner
authored andcommitted
bpo-32667: Fix tests when $PATH contains a file (GH-5322) (#5323)
Some tests failed when the PATH environment variable contained a path to an existing file. Fix tests to ignore also NotADirectoryError, not only FileNotFoundError and PermissionError. (cherry picked from commit b31206a)
1 parent 196b8cb commit 255dbd2

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

Lib/test/test_dtrace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def assert_usable(self):
7979
try:
8080
output = self.trace(abspath("assert_usable" + self.EXTENSION))
8181
output = output.strip()
82-
except (FileNotFoundError, PermissionError) as fnfe:
82+
except (FileNotFoundError, NotADirectoryError, PermissionError) as fnfe:
8383
output = str(fnfe)
8484
if output != "probe: success":
8585
raise unittest.SkipTest(

Lib/test/test_subprocess.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
SETBINARY = ''
5151

5252
NONEXISTING_CMD = ('nonexisting_i_hope',)
53+
# Ignore errors that indicate the command was not found
54+
NONEXISTING_ERRORS = (FileNotFoundError, NotADirectoryError, PermissionError)
5355

5456

5557
class BaseTestCase(unittest.TestCase):
@@ -310,9 +312,9 @@ def test_executable_takes_precedence(self):
310312
# Verify first that the call succeeds without the executable arg.
311313
pre_args = [sys.executable, "-c"]
312314
self._assert_python(pre_args)
313-
self.assertRaises((FileNotFoundError, PermissionError),
315+
self.assertRaises(NONEXISTING_ERRORS,
314316
self._assert_python, pre_args,
315-
executable="doesnotexist")
317+
executable=NONEXISTING_CMD[0])
316318

317319
@unittest.skipIf(mswindows, "executable argument replaces shell")
318320
def test_executable_replaces_shell(self):
@@ -1150,13 +1152,10 @@ def test_leaking_fds_on_error(self):
11501152
# value for that limit, but Windows has 2048, so we loop
11511153
# 1024 times (each call leaked two fds).
11521154
for i in range(1024):
1153-
with self.assertRaises(OSError) as c:
1155+
with self.assertRaises(NONEXISTING_ERRORS):
11541156
subprocess.Popen(NONEXISTING_CMD,
11551157
stdout=subprocess.PIPE,
11561158
stderr=subprocess.PIPE)
1157-
# ignore errors that indicate the command was not found
1158-
if c.exception.errno not in (errno.ENOENT, errno.EACCES):
1159-
raise c.exception
11601159

11611160
def test_nonexisting_with_pipes(self):
11621161
# bpo-30121: Popen with pipes must close properly pipes on error.
@@ -2539,7 +2538,7 @@ def test_leak_fast_process_del_killed(self):
25392538
# let some time for the process to exit, and create a new Popen: this
25402539
# should trigger the wait() of p
25412540
time.sleep(0.2)
2542-
with self.assertRaises(OSError) as c:
2541+
with self.assertRaises(OSError):
25432542
with subprocess.Popen(NONEXISTING_CMD,
25442543
stdout=subprocess.PIPE,
25452544
stderr=subprocess.PIPE) as proc:
@@ -2978,7 +2977,7 @@ def test_communicate_stdin(self):
29782977
self.assertEqual(proc.returncode, 1)
29792978

29802979
def test_invalid_args(self):
2981-
with self.assertRaises((FileNotFoundError, PermissionError)) as c:
2980+
with self.assertRaises(NONEXISTING_ERRORS):
29822981
with subprocess.Popen(NONEXISTING_CMD,
29832982
stdout=subprocess.PIPE,
29842983
stderr=subprocess.PIPE) as proc:

0 commit comments

Comments
 (0)