blob: 3b53068be4113183a064363e7829c8d62986573d [file] [log] [blame]
Richard Oudkerk84ed9a62013-08-14 15:35:41 +01001import os
2import msvcrt
3import signal
4import sys
5import _winapi
6
Richard Oudkerkb1694cf2013-10-16 16:41:56 +01007from . import context
Richard Oudkerk84ed9a62013-08-14 15:35:41 +01008from . import spawn
Richard Oudkerk84ed9a62013-08-14 15:35:41 +01009from . import reduction
10from . import util
11
12__all__ = ['Popen']
13
14#
15#
16#
17
18TERMINATE = 0x10000
19WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
20WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
21
22#
23# We define a Popen class similar to the one from subprocess, but
24# whose constructor takes a process object as its argument.
25#
26
27class Popen(object):
28 '''
29 Start a subprocess to run the code of a process object
30 '''
31 method = 'spawn'
32
33 def __init__(self, process_obj):
34 prep_data = spawn.get_preparation_data(process_obj._name)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010035
36 # read end of pipe will be "stolen" by the child process
37 # -- see spawn_main() in spawn.py.
38 rhandle, whandle = _winapi.CreatePipe(None, 0)
39 wfd = msvcrt.open_osfhandle(whandle, 0)
Richard Oudkerk7d2d43c2013-08-22 11:38:57 +010040 cmd = spawn.get_command_line(parent_pid=os.getpid(),
41 pipe_handle=rhandle)
42 cmd = ' '.join('"%s"' % x for x in cmd)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010043
44 with open(wfd, 'wb', closefd=True) as to_child:
45 # start process
46 try:
47 hp, ht, pid, tid = _winapi.CreateProcess(
48 spawn.get_executable(), cmd,
49 None, None, False, 0, None, None, None)
50 _winapi.CloseHandle(ht)
51 except:
52 _winapi.CloseHandle(rhandle)
53 raise
54
55 # set attributes of self
56 self.pid = pid
57 self.returncode = None
58 self._handle = hp
59 self.sentinel = int(hp)
60 util.Finalize(self, _winapi.CloseHandle, (self.sentinel,))
61
62 # send information to child
Richard Oudkerkb1694cf2013-10-16 16:41:56 +010063 context.set_spawning_popen(self)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010064 try:
65 reduction.dump(prep_data, to_child)
66 reduction.dump(process_obj, to_child)
67 finally:
Richard Oudkerkb1694cf2013-10-16 16:41:56 +010068 context.set_spawning_popen(None)
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010069
70 def duplicate_for_child(self, handle):
Richard Oudkerkb1694cf2013-10-16 16:41:56 +010071 assert self is context.get_spawning_popen()
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010072 return reduction.duplicate(handle, self.sentinel)
73
74 def wait(self, timeout=None):
75 if self.returncode is None:
76 if timeout is None:
77 msecs = _winapi.INFINITE
78 else:
79 msecs = max(0, int(timeout * 1000 + 0.5))
80
81 res = _winapi.WaitForSingleObject(int(self._handle), msecs)
82 if res == _winapi.WAIT_OBJECT_0:
83 code = _winapi.GetExitCodeProcess(self._handle)
84 if code == TERMINATE:
85 code = -signal.SIGTERM
86 self.returncode = code
87
88 return self.returncode
89
90 def poll(self):
91 return self.wait(timeout=0)
92
93 def terminate(self):
94 if self.returncode is None:
95 try:
96 _winapi.TerminateProcess(int(self._handle), TERMINATE)
97 except OSError:
98 if self.wait(timeout=1.0) is None:
99 raise