From 02c66d4da8864140c5d3ca6be2affe8ef853e5fb Mon Sep 17 00:00:00 2001 From: Samrat Kafle Date: Tue, 25 Aug 2026 13:17:09 -0500 Subject: [PATCH] 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. --- Lib/test/test_shutil.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 06ebdf9b68f20f..288673752d488a 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -2626,6 +2626,12 @@ def test_environ_path_cwd(self): # PATH=':': explicitly looks in the current directory with os_helper.EnvironmentVarGuard() as env: env['PATH'] = os.pathsep + if sys.platform == "win32": + # shutil.which() only searches the current directory when + # _winapi.NeedCurrentDirectoryForExePath() returns true, which + # in turn consults NoDefaultCurrentDirectoryInExePath. Unset + # it so an ambient value cannot change what is searched. + env.unset('NoDefaultCurrentDirectoryInExePath') with unittest.mock.patch('os.confstr', return_value=self.dir, \ create=True), \ support.swap_attr(os, 'defpath', self.dir):