| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,3 +22,4 @@ nbproject | |||
| 22 | 22 | .pytest_cache/ | |
| 23 | 23 | monkeytype.sqlite3 | |
| 24 | 24 | output.txt | |
| 25 | + tox.ini | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | # | |
| 4 | 4 | # This module is part of GitPython and is released under | |
| 5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 6 | - | ||
| 6 | + from __future__ import annotations | ||
| 7 | 7 | from contextlib import contextmanager | |
| 8 | 8 | import io | |
| 9 | 9 | import logging | |
@@ -68,7 +68,7 @@ | |||
| 68 | 68 | # Documentation | |
| 69 | 69 | ## @{ | |
| 70 | 70 | ||
| 71 | - def handle_process_output(process: Union[subprocess.Popen, 'Git.AutoInterrupt'], | ||
| 71 | + def handle_process_output(process: 'Git.AutoInterrupt' | Popen, | ||
| 72 | 72 | stdout_handler: Union[None, | |
| 73 | 73 | Callable[[AnyStr], None], | |
| 74 | 74 | Callable[[List[AnyStr]], None], | |
@@ -78,7 +78,8 @@ def handle_process_output(process: Union[subprocess.Popen, 'Git.AutoInterrupt'], | |||
| 78 | 78 | Callable[[List[AnyStr]], None]], | |
| 79 | 79 | finalizer: Union[None, | |
| 80 | 80 | Callable[[Union[subprocess.Popen, 'Git.AutoInterrupt']], None]] = None, | |
| 81 | - decode_streams: bool = True) -> None: | ||
| 81 | + decode_streams: bool = True, | ||
| 82 | + timeout: float = 10.0) -> None: | ||
| 82 | 83 | """Registers for notifications to learn that process output is ready to read, and dispatches lines to | |
| 83 | 84 | the respective line handlers. | |
| 84 | 85 | This function returns once the finalizer returns | |
@@ -93,9 +94,10 @@ def handle_process_output(process: Union[subprocess.Popen, 'Git.AutoInterrupt'], | |||
| 93 | 94 | their contents to handlers. | |
| 94 | 95 | Set it to False if `universal_newline == True` (then streams are in text-mode) | |
| 95 | 96 | or if decoding must happen later (i.e. for Diffs). | |
| 97 | + :param timeout: float, timeout to pass to t.join() in case it hangs. Default = 10.0 seconds | ||
| 96 | 98 | """ | |
| 97 | 99 | # Use 2 "pump" threads and wait for both to finish. | |
| 98 | - def pump_stream(cmdline: str, name: str, stream: Union[BinaryIO, TextIO], is_decode: bool, | ||
| 100 | + def pump_stream(cmdline: List[str], name: str, stream: Union[BinaryIO, TextIO], is_decode: bool, | ||
| 99 | 101 | handler: Union[None, Callable[[Union[bytes, str]], None]]) -> None: | |
| 100 | 102 | try: | |
| 101 | 103 | for line in stream: | |
@@ -107,22 +109,34 @@ def pump_stream(cmdline: str, name: str, stream: Union[BinaryIO, TextIO], is_dec | |||
| 107 | 109 | else: | |
| 108 | 110 | handler(line) | |
| 109 | 111 | except Exception as ex: | |
| 110 | - log.error("Pumping %r of cmd(%s) failed due to: %r", name, remove_password_if_present(cmdline), ex) | ||
| 111 | - raise CommandError(['<%s-pump>' % name] + remove_password_if_present(cmdline), ex) from ex | ||
| 112 | + log.error(f"Pumping {name!r} of cmd({remove_password_if_present(cmdline)})} failed due to: {ex!r}") | ||
| 113 | + raise CommandError([f'<{name}-pump>'] + remove_password_if_present(cmdline), ex) from ex | ||
| 112 | 114 | finally: | |
| 113 | 115 | stream.close() | |
| 114 | 116 | ||
| 115 | - cmdline = getattr(process, 'args', '') # PY3+ only | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + if hasattr(process, 'proc'): | ||
| 120 | + process = cast('Git.AutoInterrupt', process) | ||
| 121 | + cmdline: str | Tuple[str, ...] | List[str] = getattr(process.proc, 'args', '') | ||
| 122 | + p_stdout = process.proc.stdout | ||
| 123 | + p_stderr = process.proc.stderr | ||
| 124 | + else: | ||
| 125 | + process = cast(Popen, process) | ||
| 126 | + cmdline = getattr(process, 'args', '') | ||
| 127 | + p_stdout = process.stdout | ||
| 128 | + p_stderr = process.stderr | ||
| 129 | + | ||
| 116 | 130 | if not isinstance(cmdline, (tuple, list)): | |
| 117 | 131 | cmdline = cmdline.split() | |
| 118 | 132 | ||
| 119 | - pumps = [] | ||
| 120 | - if process.stdout: | ||
| 121 | - pumps.append(('stdout', process.stdout, stdout_handler)) | ||
| 122 | - if process.stderr: | ||
| 123 | - pumps.append(('stderr', process.stderr, stderr_handler)) | ||
| 133 | + pumps: List[Tuple[str, IO, Callable[..., None] | None]] = [] | ||
| 134 | + if p_stdout: | ||
| 135 | + pumps.append(('stdout', p_stdout, stdout_handler)) | ||
| 136 | + if p_stderr: | ||
| 137 | + pumps.append(('stderr', p_stderr, stderr_handler)) | ||
| 124 | 138 | ||
| 125 | - threads = [] | ||
| 139 | + threads: List[threading.Thread] = [] | ||
| 126 | 140 | ||
| 127 | 141 | for name, stream, handler in pumps: | |
| 128 | 142 | t = threading.Thread(target=pump_stream, | |
@@ -134,7 +148,7 @@ def pump_stream(cmdline: str, name: str, stream: Union[BinaryIO, TextIO], is_dec | |||
| 134 | 148 | ## FIXME: Why Join?? Will block if `stdin` needs feeding... | |
| 135 | 149 | # | |
| 136 | 150 | for t in threads: | |
| 137 | - t.join() | ||
| 151 | + t.join(timeout=timeout) | ||
| 138 | 152 | ||
| 139 | 153 | if finalizer: | |
| 140 | 154 | return finalizer(process) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,3 +12,6 @@ flake8-type-checking;python_version>="3.8" # checks for TYPE_CHECKING only | |||
| 12 | 12 | # pytest-flake8 | |
| 13 | 13 | pytest-icdiff | |
| 14 | 14 | # pytest-profiling | |
| 15 | + | ||
| 16 | + | ||
| 17 | + tox | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -113,12 +113,12 @@ def build_py_modules(basedir: str, excludes: Sequence = ()) -> Sequence: | |||
| 113 | 113 | "Operating System :: POSIX", | |
| 114 | 114 | "Operating System :: Microsoft :: Windows", | |
| 115 | 115 | "Operating System :: MacOS :: MacOS X", | |
| 116 | - "Typing:: Typed", | ||
| 116 | + "Typing :: Typed", | ||
| 117 | 117 | "Programming Language :: Python", | |
| 118 | 118 | "Programming Language :: Python :: 3", | |
| 119 | 119 | "Programming Language :: Python :: 3.7", | |
| 120 | 120 | "Programming Language :: Python :: 3.8", | |
| 121 | - "Programming Language :: Python :: 3.9" | ||
| 121 | + "Programming Language :: Python :: 3.9", | ||
| 122 | 122 | "Programming Language :: Python :: 3.10" | |
| 123 | 123 | ] | |
| 124 | 124 | ) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments