Skip to content

Commit d557332

Browse files
committed
fixup! Use subprocess's timeout feature instead of reinventing the wheel
1 parent 333fe0e commit d557332

1 file changed

Lines changed: 4 additions & 45 deletions

File tree

git/cmd.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import threading
2121
import warnings
2222

23-
from git.compat import defenc, force_bytes, safe_decode
23+
from git.compat import defenc, safe_decode
2424
from git.exc import (
2525
CommandError,
2626
GitCommandError,
@@ -109,7 +109,7 @@ def handle_process_output(
109109
stderr_handler: Union[None, Callable[[AnyStr], None], Callable[[List[AnyStr]], None]],
110110
finalizer: Union[None, Callable[[Union[Popen, "Git.AutoInterrupt"]], None]] = None,
111111
decode_streams: bool = True,
112-
kill_after_timeout: float | int | None = None,
112+
kill_after_timeout: float | None = None,
113113
) -> None:
114114
R"""Register for notifications to learn that process output is ready to read, and
115115
dispatch lines to the respective line handlers.
@@ -335,7 +335,7 @@ def __init__(
335335
self,
336336
proc: subprocess.Popen | None,
337337
args: Any,
338-
timeout: float | int | None = None,
338+
timeout: float | None = None,
339339
) -> None:
340340
self.proc = proc
341341
self.args = args
@@ -387,47 +387,6 @@ def __del__(self) -> None:
387387
def __getattr__(self, attr: str) -> Any:
388388
return getattr(self.proc, attr)
389389

390-
# TODO: Bad choice to mimic `proc.wait()` but with different args.
391-
def wait(self, stderr: Union[None, str, bytes] = b"") -> int:
392-
"""Wait for the process and return its status code.
393-
394-
:param stderr:
395-
Previously read value of stderr, in case stderr is already closed.
396-
397-
:warn:
398-
May deadlock if output or error pipes are used and not handled separately.
399-
400-
:raise git.exc.GitCommandError:
401-
If the return status is not 0.
402-
"""
403-
if stderr is None:
404-
stderr_b = b""
405-
stderr_b = force_bytes(data=stderr, encoding="utf-8")
406-
status: Union[int, None]
407-
if self.proc is not None:
408-
status = self.proc.wait(timeout=self.timeout)
409-
p_stderr = self.proc.stderr
410-
else: # Assume the underlying proc was killed earlier or never existed.
411-
status = self.status
412-
p_stderr = None
413-
414-
def read_all_from_possibly_closed_stream(stream: Union[IO[bytes], None]) -> bytes:
415-
if stream:
416-
try:
417-
return stderr_b + force_bytes(stream.read())
418-
except (OSError, ValueError):
419-
return stderr_b or b""
420-
else:
421-
return stderr_b or b""
422-
423-
# END status handling
424-
425-
if status != 0:
426-
errstr = read_all_from_possibly_closed_stream(p_stderr)
427-
_logger.debug("AutoInterrupt wait stderr: %r" % (errstr,))
428-
raise GitCommandError(remove_password_if_present(self.args), status, errstr)
429-
return status
430-
431390

432391
_AutoInterrupt.__name__ = "AutoInterrupt"
433392
_AutoInterrupt.__qualname__ = "Git.AutoInterrupt"
@@ -1111,7 +1070,7 @@ def execute(
11111070
as_process: bool = False,
11121071
output_stream: Union[None, BinaryIO] = None,
11131072
stdout_as_string: bool = True,
1114-
kill_after_timeout: float | int | None = None,
1073+
kill_after_timeout: float | None = None,
11151074
with_stdout: bool = True,
11161075
universal_newlines: bool = False,
11171076
shell: Union[None, bool] = None,

0 commit comments

Comments
 (0)