ยปCore Development>Code coverage>Lib/multiprocessing/popen_spawn_win32.py

Python code coverage for Lib/multiprocessing/popen_spawn_win32.py

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