1 | n/a | """ |
---|
2 | n/a | Various Windows specific bits and pieces |
---|
3 | n/a | """ |
---|
4 | n/a | |
---|
5 | n/a | import sys |
---|
6 | n/a | |
---|
7 | n/a | if sys.platform != 'win32': # pragma: no cover |
---|
8 | n/a | raise ImportError('win32 only') |
---|
9 | n/a | |
---|
10 | n/a | import _winapi |
---|
11 | n/a | import itertools |
---|
12 | n/a | import msvcrt |
---|
13 | n/a | import os |
---|
14 | n/a | import socket |
---|
15 | n/a | import subprocess |
---|
16 | n/a | import tempfile |
---|
17 | n/a | import warnings |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | __all__ = ['socketpair', 'pipe', 'Popen', 'PIPE', 'PipeHandle'] |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | # Constants/globals |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | BUFSIZE = 8192 |
---|
27 | n/a | PIPE = subprocess.PIPE |
---|
28 | n/a | STDOUT = subprocess.STDOUT |
---|
29 | n/a | _mmap_counter = itertools.count() |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | if hasattr(socket, 'socketpair'): |
---|
33 | n/a | # Since Python 3.5, socket.socketpair() is now also available on Windows |
---|
34 | n/a | socketpair = socket.socketpair |
---|
35 | n/a | else: |
---|
36 | n/a | # Replacement for socket.socketpair() |
---|
37 | n/a | def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): |
---|
38 | n/a | """A socket pair usable as a self-pipe, for Windows. |
---|
39 | n/a | |
---|
40 | n/a | Origin: https://gist.github.com/4325783, by Geert Jansen. |
---|
41 | n/a | Public domain. |
---|
42 | n/a | """ |
---|
43 | n/a | if family == socket.AF_INET: |
---|
44 | n/a | host = '127.0.0.1' |
---|
45 | n/a | elif family == socket.AF_INET6: |
---|
46 | n/a | host = '::1' |
---|
47 | n/a | else: |
---|
48 | n/a | raise ValueError("Only AF_INET and AF_INET6 socket address " |
---|
49 | n/a | "families are supported") |
---|
50 | n/a | if type != socket.SOCK_STREAM: |
---|
51 | n/a | raise ValueError("Only SOCK_STREAM socket type is supported") |
---|
52 | n/a | if proto != 0: |
---|
53 | n/a | raise ValueError("Only protocol zero is supported") |
---|
54 | n/a | |
---|
55 | n/a | # We create a connected TCP socket. Note the trick with setblocking(0) |
---|
56 | n/a | # that prevents us from having to create a thread. |
---|
57 | n/a | lsock = socket.socket(family, type, proto) |
---|
58 | n/a | try: |
---|
59 | n/a | lsock.bind((host, 0)) |
---|
60 | n/a | lsock.listen(1) |
---|
61 | n/a | # On IPv6, ignore flow_info and scope_id |
---|
62 | n/a | addr, port = lsock.getsockname()[:2] |
---|
63 | n/a | csock = socket.socket(family, type, proto) |
---|
64 | n/a | try: |
---|
65 | n/a | csock.setblocking(False) |
---|
66 | n/a | try: |
---|
67 | n/a | csock.connect((addr, port)) |
---|
68 | n/a | except (BlockingIOError, InterruptedError): |
---|
69 | n/a | pass |
---|
70 | n/a | csock.setblocking(True) |
---|
71 | n/a | ssock, _ = lsock.accept() |
---|
72 | n/a | except: |
---|
73 | n/a | csock.close() |
---|
74 | n/a | raise |
---|
75 | n/a | finally: |
---|
76 | n/a | lsock.close() |
---|
77 | n/a | return (ssock, csock) |
---|
78 | n/a | |
---|
79 | n/a | |
---|
80 | n/a | # Replacement for os.pipe() using handles instead of fds |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE): |
---|
84 | n/a | """Like os.pipe() but with overlapped support and using handles not fds.""" |
---|
85 | n/a | address = tempfile.mktemp(prefix=r'\\.\pipe\python-pipe-%d-%d-' % |
---|
86 | n/a | (os.getpid(), next(_mmap_counter))) |
---|
87 | n/a | |
---|
88 | n/a | if duplex: |
---|
89 | n/a | openmode = _winapi.PIPE_ACCESS_DUPLEX |
---|
90 | n/a | access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE |
---|
91 | n/a | obsize, ibsize = bufsize, bufsize |
---|
92 | n/a | else: |
---|
93 | n/a | openmode = _winapi.PIPE_ACCESS_INBOUND |
---|
94 | n/a | access = _winapi.GENERIC_WRITE |
---|
95 | n/a | obsize, ibsize = 0, bufsize |
---|
96 | n/a | |
---|
97 | n/a | openmode |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE |
---|
98 | n/a | |
---|
99 | n/a | if overlapped[0]: |
---|
100 | n/a | openmode |= _winapi.FILE_FLAG_OVERLAPPED |
---|
101 | n/a | |
---|
102 | n/a | if overlapped[1]: |
---|
103 | n/a | flags_and_attribs = _winapi.FILE_FLAG_OVERLAPPED |
---|
104 | n/a | else: |
---|
105 | n/a | flags_and_attribs = 0 |
---|
106 | n/a | |
---|
107 | n/a | h1 = h2 = None |
---|
108 | n/a | try: |
---|
109 | n/a | h1 = _winapi.CreateNamedPipe( |
---|
110 | n/a | address, openmode, _winapi.PIPE_WAIT, |
---|
111 | n/a | 1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL) |
---|
112 | n/a | |
---|
113 | n/a | h2 = _winapi.CreateFile( |
---|
114 | n/a | address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING, |
---|
115 | n/a | flags_and_attribs, _winapi.NULL) |
---|
116 | n/a | |
---|
117 | n/a | ov = _winapi.ConnectNamedPipe(h1, overlapped=True) |
---|
118 | n/a | ov.GetOverlappedResult(True) |
---|
119 | n/a | return h1, h2 |
---|
120 | n/a | except: |
---|
121 | n/a | if h1 is not None: |
---|
122 | n/a | _winapi.CloseHandle(h1) |
---|
123 | n/a | if h2 is not None: |
---|
124 | n/a | _winapi.CloseHandle(h2) |
---|
125 | n/a | raise |
---|
126 | n/a | |
---|
127 | n/a | |
---|
128 | n/a | # Wrapper for a pipe handle |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class PipeHandle: |
---|
132 | n/a | """Wrapper for an overlapped pipe handle which is vaguely file-object like. |
---|
133 | n/a | |
---|
134 | n/a | The IOCP event loop can use these instead of socket objects. |
---|
135 | n/a | """ |
---|
136 | n/a | def __init__(self, handle): |
---|
137 | n/a | self._handle = handle |
---|
138 | n/a | |
---|
139 | n/a | def __repr__(self): |
---|
140 | n/a | if self._handle is not None: |
---|
141 | n/a | handle = 'handle=%r' % self._handle |
---|
142 | n/a | else: |
---|
143 | n/a | handle = 'closed' |
---|
144 | n/a | return '<%s %s>' % (self.__class__.__name__, handle) |
---|
145 | n/a | |
---|
146 | n/a | @property |
---|
147 | n/a | def handle(self): |
---|
148 | n/a | return self._handle |
---|
149 | n/a | |
---|
150 | n/a | def fileno(self): |
---|
151 | n/a | if self._handle is None: |
---|
152 | n/a | raise ValueError("I/O operatioon on closed pipe") |
---|
153 | n/a | return self._handle |
---|
154 | n/a | |
---|
155 | n/a | def close(self, *, CloseHandle=_winapi.CloseHandle): |
---|
156 | n/a | if self._handle is not None: |
---|
157 | n/a | CloseHandle(self._handle) |
---|
158 | n/a | self._handle = None |
---|
159 | n/a | |
---|
160 | n/a | def __del__(self): |
---|
161 | n/a | if self._handle is not None: |
---|
162 | n/a | warnings.warn("unclosed %r" % self, ResourceWarning, |
---|
163 | n/a | source=self) |
---|
164 | n/a | self.close() |
---|
165 | n/a | |
---|
166 | n/a | def __enter__(self): |
---|
167 | n/a | return self |
---|
168 | n/a | |
---|
169 | n/a | def __exit__(self, t, v, tb): |
---|
170 | n/a | self.close() |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | # Replacement for subprocess.Popen using overlapped pipe handles |
---|
174 | n/a | |
---|
175 | n/a | |
---|
176 | n/a | class Popen(subprocess.Popen): |
---|
177 | n/a | """Replacement for subprocess.Popen using overlapped pipe handles. |
---|
178 | n/a | |
---|
179 | n/a | The stdin, stdout, stderr are None or instances of PipeHandle. |
---|
180 | n/a | """ |
---|
181 | n/a | def __init__(self, args, stdin=None, stdout=None, stderr=None, **kwds): |
---|
182 | n/a | assert not kwds.get('universal_newlines') |
---|
183 | n/a | assert kwds.get('bufsize', 0) == 0 |
---|
184 | n/a | stdin_rfd = stdout_wfd = stderr_wfd = None |
---|
185 | n/a | stdin_wh = stdout_rh = stderr_rh = None |
---|
186 | n/a | if stdin == PIPE: |
---|
187 | n/a | stdin_rh, stdin_wh = pipe(overlapped=(False, True), duplex=True) |
---|
188 | n/a | stdin_rfd = msvcrt.open_osfhandle(stdin_rh, os.O_RDONLY) |
---|
189 | n/a | else: |
---|
190 | n/a | stdin_rfd = stdin |
---|
191 | n/a | if stdout == PIPE: |
---|
192 | n/a | stdout_rh, stdout_wh = pipe(overlapped=(True, False)) |
---|
193 | n/a | stdout_wfd = msvcrt.open_osfhandle(stdout_wh, 0) |
---|
194 | n/a | else: |
---|
195 | n/a | stdout_wfd = stdout |
---|
196 | n/a | if stderr == PIPE: |
---|
197 | n/a | stderr_rh, stderr_wh = pipe(overlapped=(True, False)) |
---|
198 | n/a | stderr_wfd = msvcrt.open_osfhandle(stderr_wh, 0) |
---|
199 | n/a | elif stderr == STDOUT: |
---|
200 | n/a | stderr_wfd = stdout_wfd |
---|
201 | n/a | else: |
---|
202 | n/a | stderr_wfd = stderr |
---|
203 | n/a | try: |
---|
204 | n/a | super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd, |
---|
205 | n/a | stderr=stderr_wfd, **kwds) |
---|
206 | n/a | except: |
---|
207 | n/a | for h in (stdin_wh, stdout_rh, stderr_rh): |
---|
208 | n/a | if h is not None: |
---|
209 | n/a | _winapi.CloseHandle(h) |
---|
210 | n/a | raise |
---|
211 | n/a | else: |
---|
212 | n/a | if stdin_wh is not None: |
---|
213 | n/a | self.stdin = PipeHandle(stdin_wh) |
---|
214 | n/a | if stdout_rh is not None: |
---|
215 | n/a | self.stdout = PipeHandle(stdout_rh) |
---|
216 | n/a | if stderr_rh is not None: |
---|
217 | n/a | self.stderr = PipeHandle(stderr_rh) |
---|
218 | n/a | finally: |
---|
219 | n/a | if stdin == PIPE: |
---|
220 | n/a | os.close(stdin_rfd) |
---|
221 | n/a | if stdout == PIPE: |
---|
222 | n/a | os.close(stdout_wfd) |
---|
223 | n/a | if stderr == PIPE: |
---|
224 | n/a | os.close(stderr_wfd) |
---|