Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ef3365

Browse files
committed
Factor communicate and watchdog logic to helper
The goal is to improve readability by not repeating the platform and kill_after_timeout check three times.
1 parent 4191f7d commit 1ef3365

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

git/cmd.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,12 +1135,12 @@ def execute(
11351135
env.update(inline_env)
11361136

11371137
if sys.platform == "win32":
1138-
cmd_not_found_exception = OSError
11391138
if kill_after_timeout is not None:
11401139
raise GitCommandError(
11411140
redacted_command,
11421141
'"kill_after_timeout" feature is not supported on Windows.',
11431142
)
1143+
cmd_not_found_exception = OSError
11441144
else:
11451145
cmd_not_found_exception = FileNotFoundError
11461146
# END handle
@@ -1209,10 +1209,25 @@ def kill_process(pid: int) -> None:
12091209
pass
12101210
return
12111211

1212-
# END kill_process
1212+
def communicate() -> Tuple[AnyStr, AnyStr]:
1213+
watchdog.start()
1214+
out, err = proc.communicate()
1215+
watchdog.cancel()
1216+
if kill_check.is_set():
1217+
err = 'Timeout: the command "%s" did not complete in %d ' "secs." % (
1218+
" ".join(redacted_command),
1219+
kill_after_timeout,
1220+
)
1221+
if not universal_newlines:
1222+
err = err.encode(defenc)
1223+
return out, err
1224+
1225+
# END helpers
12131226

12141227
kill_check = threading.Event()
12151228
watchdog = threading.Timer(kill_after_timeout, kill_process, args=(proc.pid,))
1229+
else:
1230+
communicate = proc.communicate
12161231

12171232
# Wait for the process to return.
12181233
status = 0
@@ -1221,18 +1236,7 @@ def kill_process(pid: int) -> None:
12211236
newline = "\n" if universal_newlines else b"\n"
12221237
try:
12231238
if output_stream is None:
1224-
if sys.platform != "win32" and kill_after_timeout is not None:
1225-
watchdog.start()
1226-
stdout_value, stderr_value = proc.communicate()
1227-
if sys.platform != "win32" and kill_after_timeout is not None:
1228-
watchdog.cancel()
1229-
if kill_check.is_set():
1230-
stderr_value = 'Timeout: the command "%s" did not complete in %d ' "secs." % (
1231-
" ".join(redacted_command),
1232-
kill_after_timeout,
1233-
)
1234-
if not universal_newlines:
1235-
stderr_value = stderr_value.encode(defenc)
1239+
stdout_value, stderr_value = communicate()
12361240
# Strip trailing "\n".
12371241
if stdout_value.endswith(newline) and strip_newline_in_stdout: # type: ignore
12381242
stdout_value = stdout_value[:-1]

0 commit comments

Comments
 (0)