1616import threading
1717from textwrap import dedent
1818
19- from git .compat import (
20- defenc ,
21- force_bytes ,
22- safe_decode ,
23- is_posix ,
24- is_win ,
19+ from git .compat import defenc , force_bytes , safe_decode
20+ from git .exc import (
21+ CommandError ,
22+ GitCommandError ,
23+ GitCommandNotFound ,
24+ UnsafeOptionError ,
25+ UnsafeProtocolError ,
2526)
26- from git .exc import CommandError
27- from git .util import is_cygwin_git , cygpath , expand_path , remove_password_if_present , patch_env
28-
29- from .exc import GitCommandError , GitCommandNotFound , UnsafeOptionError , UnsafeProtocolError
30- from .util import (
27+ from git .util import (
3128 LazyMixin ,
29+ cygpath ,
30+ expand_path ,
31+ is_cygwin_git ,
32+ patch_env ,
33+ remove_password_if_present ,
3234 stream_copy ,
3335)
3436
@@ -179,14 +181,13 @@ def pump_stream(
179181 t .start ()
180182 threads .append (t )
181183
182- ## FIXME: Why Join?? Will block if `stdin` needs feeding...
183- #
184+ # FIXME: Why join? Will block if stdin needs feeding...
184185 for t in threads :
185186 t .join (timeout = kill_after_timeout )
186187 if t .is_alive ():
187188 if isinstance (process , Git .AutoInterrupt ):
188189 process ._terminate ()
189- else : # Don't want to deal with the other case
190+ else : # Don't want to deal with the other case.
190191 raise RuntimeError (
191192 "Thread join() timed out in cmd.handle_process_output()."
192193 f" kill_after_timeout={ kill_after_timeout } seconds"
@@ -196,11 +197,11 @@ def pump_stream(
196197 "error: process killed because it timed out." f" kill_after_timeout={ kill_after_timeout } seconds"
197198 )
198199 if not decode_streams and isinstance (p_stderr , BinaryIO ):
199- # Assume stderr_handler needs binary input
200+ # Assume stderr_handler needs binary input.
200201 error_str = cast (str , error_str )
201202 error_str = error_str .encode ()
202203 # We ignore typing on the next line because mypy does not like
203- # the way we inferred that stderr takes str or bytes
204+ # the way we inferred that stderr takes str or bytes.
204205 stderr_handler (error_str ) # type: ignore
205206
206207 if finalizer :
@@ -227,14 +228,12 @@ def dict_to_slots_and__excluded_are_none(self: object, d: Mapping[str, Any], exc
227228## -- End Utilities -- @}
228229
229230
230- # value of Windows process creation flag taken from MSDN
231- CREATE_NO_WINDOW = 0x08000000
232-
233- ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
234- # see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
235- PROC_CREATIONFLAGS = (
236- CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP if is_win else 0 # type: ignore[attr-defined]
237- ) # mypy error if not Windows.
231+ if os .name == "nt" :
232+ # CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards. See:
233+ # https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
234+ PROC_CREATIONFLAGS = subprocess .CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP
235+ else :
236+ PROC_CREATIONFLAGS = 0
238237
239238
240239class Git (LazyMixin ):
@@ -550,7 +549,7 @@ def _terminate(self) -> None:
550549 # For some reason, providing None for stdout/stderr still prints something. This is why
551550 # we simply use the shell and redirect to nul. Slower than CreateProcess. The question
552551 # is whether we really want to see all these messages. It's annoying no matter what.
553- if is_win :
552+ if os . name == "nt" :
554553 call (
555554 ("TASKKILL /F /T /PID %s 2>nul 1>nul" % str (proc .pid )),
556555 shell = True ,
@@ -966,7 +965,7 @@ def execute(
966965 if inline_env is not None :
967966 env .update (inline_env )
968967
969- if is_win :
968+ if os . name == "nt" :
970969 cmd_not_found_exception = OSError
971970 if kill_after_timeout is not None :
972971 raise GitCommandError (
@@ -998,11 +997,11 @@ def execute(
998997 env = env ,
999998 cwd = cwd ,
1000999 bufsize = - 1 ,
1001- stdin = istream or DEVNULL ,
1000+ stdin = ( istream or DEVNULL ) ,
10021001 stderr = PIPE ,
10031002 stdout = stdout_sink ,
10041003 shell = shell ,
1005- close_fds = is_posix , # Unsupported on Windows.
1004+ close_fds = ( os . name == "posix" ) , # Unsupported on Windows.
10061005 universal_newlines = universal_newlines ,
10071006 creationflags = PROC_CREATIONFLAGS ,
10081007 ** subprocess_kwargs ,
@@ -1072,7 +1071,7 @@ def kill_process(pid: int) -> None:
10721071 )
10731072 if not universal_newlines :
10741073 stderr_value = stderr_value .encode (defenc )
1075- # strip trailing "\n"
1074+ # Strip trailing "\n".
10761075 if stdout_value .endswith (newline ) and strip_newline_in_stdout : # type: ignore
10771076 stdout_value = stdout_value [:- 1 ]
10781077 if stderr_value .endswith (newline ): # type: ignore
@@ -1146,11 +1145,11 @@ def update_environment(self, **kwargs: Any) -> Dict[str, Union[str, None]]:
11461145 """
11471146 old_env = {}
11481147 for key , value in kwargs .items ():
1149- # set value if it is None
1148+ # Set value if it is None.
11501149 if value is not None :
11511150 old_env [key ] = self ._environment .get (key )
11521151 self ._environment [key ] = value
1153- # remove key from environment if its value is None
1152+ # Remove key from environment if its value is None.
11541153 elif key in self ._environment :
11551154 old_env [key ] = self ._environment [key ]
11561155 del self ._environment [key ]
@@ -1329,7 +1328,8 @@ def _parse_object_header(self, header_line: str) -> Tuple[str, str, int]:
13291328 :return: (hex_sha, type_string, size_as_int)
13301329
13311330 :raise ValueError: If the header contains indication for an error due to
1332- incorrect input sha"""
1331+ incorrect input sha
1332+ """
13331333 tokens = header_line .split ()
13341334 if len (tokens ) != 3 :
13351335 if not tokens :
0 commit comments