diff --git a/git/compat.py b/git/compat.py index a0aea1ac4..c9b83ba46 100644 --- a/git/compat.py +++ b/git/compat.py @@ -18,7 +18,7 @@ # typing -------------------------------------------------------------------- -from typing import Any, AnyStr, Dict, Optional, Type +from typing import IO, Any, AnyStr, Dict, Optional, Type, Union from git.types import TBD # --------------------------------------------------------------------------- @@ -30,7 +30,7 @@ defenc = sys.getfilesystemencoding() -def safe_decode(s: Optional[AnyStr]) -> Optional[str]: +def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]: """Safely decodes a binary string to unicode""" if isinstance(s, str): return s diff --git a/git/exc.py b/git/exc.py index c066e5e2f..358e7ae46 100644 --- a/git/exc.py +++ b/git/exc.py @@ -65,10 +65,12 @@ def __init__(self, command: Union[List[str], Tuple[str, ...], str], status = "'%s'" % s if isinstance(status, str) else s self._cmd = safe_decode(command[0]) - self._cmdline = ' '.join(str(safe_decode(i)) for i in command) + self._cmdline = ' '.join(safe_decode(i) for i in command) self._cause = status and " due to: %s" % status or "!" - self.stdout = stdout and "\n stdout: '%s'" % safe_decode(str(stdout)) or '' - self.stderr = stderr and "\n stderr: '%s'" % safe_decode(str(stderr)) or '' + stdout_decode = safe_decode(stdout) + stderr_decode = safe_decode(stderr) + self.stdout = stdout_decode and "\n stdout: '%s'" % stdout_decode or '' + self.stderr = stderr_decode and "\n stderr: '%s'" % stderr_decode or '' def __str__(self) -> str: return (self._msg + "\n cmdline: %s%s%s") % (