From f1ace258417deae329880754987851b1b8fc0a7a Mon Sep 17 00:00:00 2001 From: yobmod Date: Wed, 12 May 2021 18:10:37 +0100 Subject: Add types to cmd.py AutoInterrupt --- git/cmd.py | 37 ++++++++++++++++++++----------------- git/exc.py | 2 +- 2 files changed, 21 insertions(+), 18 deletions(-) (limited to 'git') diff --git a/git/cmd.py b/git/cmd.py index ac4cdf30..74113ce8 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -357,11 +357,11 @@ class Git(LazyMixin): __slots__ = ("proc", "args") - def __init__(self, proc, args): + def __init__(self, proc: Union[None, subprocess.Popen], args: Any) -> None: self.proc = proc self.args = args - def __del__(self): + def __del__(self) -> None: if self.proc is None: return @@ -377,13 +377,13 @@ class Git(LazyMixin): # did the process finish already so we have a return code ? try: if proc.poll() is not None: - return + return None except OSError as ex: log.info("Ignored error after process had died: %r", ex) # can be that nothing really exists anymore ... if os is None or getattr(os, 'kill', None) is None: - return + return None # try to kill it try: @@ -400,10 +400,11 @@ class Git(LazyMixin): call(("TASKKILL /F /T /PID %s 2>nul 1>nul" % str(proc.pid)), shell=True) # END exception handling - def __getattr__(self, attr): + def __getattr__(self, attr: str) -> Any: return getattr(self.proc, attr) - def wait(self, stderr=b''): # TODO: Bad choice to mimic `proc.wait()` but with different args. + # TODO: Bad choice to mimic `proc.wait()` but with different args. + def wait(self, stderr: Union[None, bytes] = b'') -> int: """Wait for the process and return its status code. :param stderr: Previously read value of stderr, in case stderr is already closed. @@ -413,20 +414,22 @@ class Git(LazyMixin): stderr = b'' stderr = force_bytes(data=stderr, encoding='utf-8') - status = self.proc.wait() - - def read_all_from_possibly_closed_stream(stream): - try: - return stderr + force_bytes(stream.read()) - except ValueError: - return stderr or b'' + if self.proc is not None: + status = self.proc.wait() - if status != 0: - errstr = read_all_from_possibly_closed_stream(self.proc.stderr) - log.debug('AutoInterrupt wait stderr: %r' % (errstr,)) - raise GitCommandError(remove_password_if_present(self.args), status, errstr) + def read_all_from_possibly_closed_stream(stream): + try: + return stderr + force_bytes(stream.read()) + except ValueError: + return stderr or b'' + + if status != 0: + errstr = read_all_from_possibly_closed_stream(self.proc.stderr) + log.debug('AutoInterrupt wait stderr: %r' % (errstr,)) + raise GitCommandError(remove_password_if_present(self.args), status, errstr) # END status handling return status + # END auto interrupt class CatFileContentStream(object): diff --git a/git/exc.py b/git/exc.py index 6e646921..bcf5aabb 100644 --- a/git/exc.py +++ b/git/exc.py @@ -91,7 +91,7 @@ class GitCommandError(CommandError): """ Thrown if execution of the git command fails with non-zero status code. """ def __init__(self, command: Union[List[str], Tuple[str, ...], str], - status: Union[str, None, Exception] = None, + status: Union[str, int, None, Exception] = None, stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None, ) -> None: -- cgit v1.2.1