15
15
from git .odict import OrderedDict
16
16
from contextlib import contextmanager
17
17
import signal
18
+ import subprocess
18
19
from subprocess import (
19
20
call ,
20
21
Popen ,
@@ -229,6 +230,15 @@ def dict_to_slots_and__excluded_are_none(self, d, excluded=()):
229
230
230
231
## -- End Utilities -- @}
231
232
233
+ # value of Windows process creation flag taken from MSDN
234
+ CREATE_NO_WINDOW = 0x08000000
235
+
236
+ ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
237
+ # seehttps://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
238
+ PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP
239
+ if sys .platform == 'win32'
240
+ else 0 )
241
+
232
242
233
243
class Git (LazyMixin ):
234
244
@@ -267,9 +277,6 @@ def __setstate__(self, d):
267
277
# Enables debugging of GitPython's git commands
268
278
GIT_PYTHON_TRACE = os .environ .get ("GIT_PYTHON_TRACE" , False )
269
279
270
- # value of Windows process creation flag taken from MSDN
271
- CREATE_NO_WINDOW = 0x08000000
272
-
273
280
# Provide the full path to the git executable. Otherwise it assumes git is in the path
274
281
_git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
275
282
GIT_PYTHON_GIT_EXECUTABLE = os .environ .get (_git_exec_env_var , git_exec_name )
@@ -317,7 +324,7 @@ def __del__(self):
317
324
318
325
# try to kill it
319
326
try :
320
- os . kill ( proc .pid , 2 ) # interrupt signal
327
+ proc .terminate ()
321
328
proc .wait () # ensure process goes away
322
329
except (OSError , WindowsError ):
323
330
pass # ignore error when process already died
@@ -632,7 +639,6 @@ def execute(self, command,
632
639
cmd_not_found_exception = OSError
633
640
# end handle
634
641
635
- creationflags = self .CREATE_NO_WINDOW if sys .platform == 'win32' else 0
636
642
try :
637
643
proc = Popen (command ,
638
644
env = env ,
@@ -644,7 +650,7 @@ def execute(self, command,
644
650
shell = self .USE_SHELL ,
645
651
close_fds = (os .name == 'posix' ), # unsupported on windows
646
652
universal_newlines = universal_newlines ,
647
- creationflags = creationflags ,
653
+ creationflags = PROC_CREATIONFLAGS ,
648
654
** subprocess_kwargs
649
655
)
650
656
except cmd_not_found_exception as err :
@@ -655,7 +661,8 @@ def execute(self, command,
655
661
656
662
def _kill_process (pid ):
657
663
""" Callback method to kill a process. """
658
- p = Popen (['ps' , '--ppid' , str (pid )], stdout = PIPE , creationflags = creationflags )
664
+ p = Popen (['ps' , '--ppid' , str (pid )], stdout = PIPE ,
665
+ creationflags = PROC_CREATIONFLAGS )
659
666
child_pids = []
660
667
for line in p .stdout :
661
668
if len (line .split ()) > 0 :
0 commit comments