Skip to content

Commit d2506c7

Browse files
committed
Make Git.execute a bit simpler and very slightly more robust
This covers the rare unpatched python/cpython#101283 case the previous commit added tests for, that only applies in the unusual situation that the ComSpec environment variable is unset and an old build (but this includes downloadable builds and current actions/setup-python builds) of Python <=3.9 for Windows is in use. The main benefit of this change is really to slightly simplify the code under test. (It might even be justified to remove the use_shell_impostor=True test cases at some point.)
1 parent 865c6e8 commit d2506c7

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Diff for: git/cmd.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -988,25 +988,22 @@ def execute(
988988
if shell is None:
989989
shell = self.USE_SHELL
990990

991-
cmd_not_found_exception = FileNotFoundError
992-
maybe_patch_caller_env = contextlib.nullcontext()
993-
994991
if os.name == "nt":
992+
cmd_not_found_exception = OSError
995993
if kill_after_timeout is not None:
996994
raise GitCommandError(
997995
redacted_command,
998996
'"kill_after_timeout" feature is not supported on Windows.',
999997
)
1000-
1001-
cmd_not_found_exception = OSError
1002-
1003-
# Search PATH, but do not search CWD. The "1" can be any value.
998+
# Search PATH but not CWD. The "1" can be any value. We'll patch just before
999+
# the Popen call and unpatch just after, or we get a worse race condition.
1000+
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
10041001
if shell:
1005-
# If the direct subprocess is a shell, this must go in its environment.
1002+
# Modify the direct shell subprocess's own search behavior accordingly.
10061003
env["NoDefaultCurrentDirectoryInExePath"] = "1"
1007-
else:
1008-
# If we're not using a shell, the variable goes in our own environment.
1009-
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
1004+
else:
1005+
cmd_not_found_exception = FileNotFoundError
1006+
maybe_patch_caller_env = contextlib.nullcontext()
10101007
# END handle
10111008

10121009
stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb")

0 commit comments

Comments
 (0)