Skip to content

Commit 02c66d4

Browse files
gh-156376: Make test_environ_path_cwd independent of the ambient environment
shutil.which() only inserts the current directory into the search path when _winapi.NeedCurrentDirectoryForExePath() returns true, and that consults the NoDefaultCurrentDirectoryInExePath environment variable. On a machine where that variable is set, TestWhich.test_environ_path_cwd and TestWhichBytes.test_environ_path_cwd failed because which() correctly declined to search the current directory. Unset the variable inside the EnvironmentVarGuard the test already uses, so an ambient value cannot change what the test exercises.
1 parent 04242c0 commit 02c66d4

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Lib/test/test_shutil.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,12 @@ def test_environ_path_cwd(self):
26262626
# PATH=':': explicitly looks in the current directory
26272627
with os_helper.EnvironmentVarGuard() as env:
26282628
env['PATH'] = os.pathsep
2629+
if sys.platform == "win32":
2630+
# shutil.which() only searches the current directory when
2631+
# _winapi.NeedCurrentDirectoryForExePath() returns true, which
2632+
# in turn consults NoDefaultCurrentDirectoryInExePath. Unset
2633+
# it so an ambient value cannot change what is searched.
2634+
env.unset('NoDefaultCurrentDirectoryInExePath')
26292635
with unittest.mock.patch('os.confstr', return_value=self.dir, \
26302636
create=True), \
26312637
support.swap_attr(os, 'defpath', self.dir):

0 commit comments

Comments
 (0)