diff options
author | sroet <sanderroet@hotmail.com> | 2021-09-15 11:55:17 +0200 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-09-18 09:26:28 +0800 |
commit | aa5076626ca9f2ff1279c6b8e67408be9d0fa690 (patch) | |
tree | ff103d77b0f1aa5a254726a7ff2abfdde0ed6e7f /git/cmd.py | |
parent | 893ddabd312535bfd906822e42f0223c40655163 (diff) | |
download | gitpython-aa5076626ca9f2ff1279c6b8e67408be9d0fa690.tar.gz |
Add a way to force status codes inside AutoInterrupt._terminate, and let tests use it
Diffstat (limited to 'git/cmd.py')
-rw-r--r-- | git/cmd.py | 19 |
1 files changed, 12 insertions, 7 deletions
@@ -409,6 +409,10 @@ class Git(LazyMixin): __slots__ = ("proc", "args", "status") + # If this is non-zero it will override any status code during + # _terminate, used to prevent race conditions in testing + _status_code_if_terminate: int = 0 + def __init__(self, proc: Union[None, subprocess.Popen], args: Any) -> None: self.proc = proc self.args = args @@ -427,11 +431,10 @@ class Git(LazyMixin): proc.stdout.close() if proc.stderr: proc.stderr.close() - # did the process finish already so we have a return code ? try: if proc.poll() is not None: - self.status = proc.poll() + self.status = self._status_code_if_terminate or proc.poll() return None except OSError as ex: log.info("Ignored error after process had died: %r", ex) @@ -443,7 +446,9 @@ class Git(LazyMixin): # try to kill it try: proc.terminate() - self.status = proc.wait() # ensure process goes away + status = proc.wait() # ensure process goes away + + self.status = self._status_code_if_terminate or status except OSError as ex: log.info("Ignored error after process had died: %r", ex) except AttributeError: @@ -849,7 +854,7 @@ class Git(LazyMixin): if is_win: cmd_not_found_exception = OSError - if kill_after_timeout: + if kill_after_timeout is not None: raise GitCommandError(redacted_command, '"kill_after_timeout" feature is not supported on Windows.') else: cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable @@ -916,7 +921,7 @@ class Git(LazyMixin): return # end - if kill_after_timeout: + if kill_after_timeout is not None: kill_check = threading.Event() watchdog = threading.Timer(kill_after_timeout, _kill_process, args=(proc.pid,)) @@ -927,10 +932,10 @@ class Git(LazyMixin): newline = "\n" if universal_newlines else b"\n" try: if output_stream is None: - if kill_after_timeout: + if kill_after_timeout is not None: watchdog.start() stdout_value, stderr_value = proc.communicate() - if kill_after_timeout: + if kill_after_timeout is not None: watchdog.cancel() if kill_check.is_set(): stderr_value = ('Timeout: the command "%s" did not complete in %d ' |