Skip to content

Commit 23bba4c

Browse files
committed
Issue #11750: The Windows API functions scattered in the _subprocess and
_multiprocessing.win32 modules now live in a single module "_winapi". Patch by sbt.
1 parent c51b7fd commit 23bba4c

File tree

15 files changed

+1562
-1665
lines changed

15 files changed

+1562
-1665
lines changed

Lib/multiprocessing/connection.py

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
from multiprocessing.util import (
5252
get_temp_dir, Finalize, sub_debug, debug, _eintr_retry)
5353
try:
54-
from _multiprocessing import win32
55-
from _subprocess import WAIT_OBJECT_0, WAIT_TIMEOUT, INFINITE
54+
import _winapi
55+
from _winapi import WAIT_OBJECT_0, WAIT_TIMEOUT, INFINITE
5656
except ImportError:
5757
if sys.platform == 'win32':
5858
raise
59-
win32 = None
59+
_winapi = None
6060

6161
#
6262
#
@@ -282,7 +282,7 @@ def poll(self, timeout=0.0):
282282
return self._poll(timeout)
283283

284284

285-
if win32:
285+
if _winapi:
286286

287287
class PipeConnection(_ConnectionBase):
288288
"""
@@ -292,14 +292,14 @@ class PipeConnection(_ConnectionBase):
292292
"""
293293
_got_empty_message = False
294294

295-
def _close(self, _CloseHandle=win32.CloseHandle):
295+
def _close(self, _CloseHandle=_winapi.CloseHandle):
296296
_CloseHandle(self._handle)
297297

298298
def _send_bytes(self, buf):
299-
ov, err = win32.WriteFile(self._handle, buf, overlapped=True)
299+
ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
300300
try:
301-
if err == win32.ERROR_IO_PENDING:
302-
waitres = win32.WaitForMultipleObjects(
301+
if err == _winapi.ERROR_IO_PENDING:
302+
waitres = _winapi.WaitForMultipleObjects(
303303
[ov.event], False, INFINITE)
304304
assert waitres == WAIT_OBJECT_0
305305
except:
@@ -317,11 +317,11 @@ def _recv_bytes(self, maxsize=None):
317317
else:
318318
bsize = 128 if maxsize is None else min(maxsize, 128)
319319
try:
320-
ov, err = win32.ReadFile(self._handle, bsize,
321-
overlapped=True)
320+
ov, err = _winapi.ReadFile(self._handle, bsize,
321+
overlapped=True)
322322
try:
323-
if err == win32.ERROR_IO_PENDING:
324-
waitres = win32.WaitForMultipleObjects(
323+
if err == _winapi.ERROR_IO_PENDING:
324+
waitres = _winapi.WaitForMultipleObjects(
325325
[ov.event], False, INFINITE)
326326
assert waitres == WAIT_OBJECT_0
327327
except:
@@ -333,18 +333,18 @@ def _recv_bytes(self, maxsize=None):
333333
f = io.BytesIO()
334334
f.write(ov.getbuffer())
335335
return f
336-
elif err == win32.ERROR_MORE_DATA:
336+
elif err == _winapi.ERROR_MORE_DATA:
337337
return self._get_more_data(ov, maxsize)
338338
except IOError as e:
339-
if e.winerror == win32.ERROR_BROKEN_PIPE:
339+
if e.winerror == _winapi.ERROR_BROKEN_PIPE:
340340
raise EOFError
341341
else:
342342
raise
343343
raise RuntimeError("shouldn't get here; expected KeyboardInterrupt")
344344

345345
def _poll(self, timeout):
346346
if (self._got_empty_message or
347-
win32.PeekNamedPipe(self._handle)[0] != 0):
347+
_winapi.PeekNamedPipe(self._handle)[0] != 0):
348348
return True
349349
if timeout < 0:
350350
timeout = None
@@ -354,11 +354,11 @@ def _get_more_data(self, ov, maxsize):
354354
buf = ov.getbuffer()
355355
f = io.BytesIO()
356356
f.write(buf)
357-
left = win32.PeekNamedPipe(self._handle)[1]
357+
left = _winapi.PeekNamedPipe(self._handle)[1]
358358
assert left > 0
359359
if maxsize is not None and len(buf) + left > maxsize:
360360
self._bad_message_length()
361-
ov, err = win32.ReadFile(self._handle, left, overlapped=True)
361+
ov, err = _winapi.ReadFile(self._handle, left, overlapped=True)
362362
rbytes, err = ov.GetOverlappedResult(True)
363363
assert err == 0
364364
assert rbytes == left
@@ -372,11 +372,11 @@ class Connection(_ConnectionBase):
372372
a socket handle (Windows).
373373
"""
374374

375-
if win32:
376-
def _close(self, _close=win32.closesocket):
375+
if _winapi:
376+
def _close(self, _close=_multiprocessing.closesocket):
377377
_close(self._handle)
378-
_write = win32.send
379-
_read = win32.recv
378+
_write = _multiprocessing.send
379+
_read = _multiprocessing.recv
380380
else:
381381
def _close(self, _close=os.close):
382382
_close(self._handle)
@@ -526,30 +526,30 @@ def Pipe(duplex=True):
526526
'''
527527
address = arbitrary_address('AF_PIPE')
528528
if duplex:
529-
openmode = win32.PIPE_ACCESS_DUPLEX
530-
access = win32.GENERIC_READ | win32.GENERIC_WRITE
529+
openmode = _winapi.PIPE_ACCESS_DUPLEX
530+
access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
531531
obsize, ibsize = BUFSIZE, BUFSIZE
532532
else:
533-
openmode = win32.PIPE_ACCESS_INBOUND
534-
access = win32.GENERIC_WRITE
533+
openmode = _winapi.PIPE_ACCESS_INBOUND
534+
access = _winapi.GENERIC_WRITE
535535
obsize, ibsize = 0, BUFSIZE
536536

537-
h1 = win32.CreateNamedPipe(
538-
address, openmode | win32.FILE_FLAG_OVERLAPPED |
539-
win32.FILE_FLAG_FIRST_PIPE_INSTANCE,
540-
win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
541-
win32.PIPE_WAIT,
542-
1, obsize, ibsize, win32.NMPWAIT_WAIT_FOREVER, win32.NULL
537+
h1 = _winapi.CreateNamedPipe(
538+
address, openmode | _winapi.FILE_FLAG_OVERLAPPED |
539+
_winapi.FILE_FLAG_FIRST_PIPE_INSTANCE,
540+
_winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
541+
_winapi.PIPE_WAIT,
542+
1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL
543543
)
544-
h2 = win32.CreateFile(
545-
address, access, 0, win32.NULL, win32.OPEN_EXISTING,
546-
win32.FILE_FLAG_OVERLAPPED, win32.NULL
544+
h2 = _winapi.CreateFile(
545+
address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING,
546+
_winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
547547
)
548-
win32.SetNamedPipeHandleState(
549-
h2, win32.PIPE_READMODE_MESSAGE, None, None
548+
_winapi.SetNamedPipeHandleState(
549+
h2, _winapi.PIPE_READMODE_MESSAGE, None, None
550550
)
551551

552-
overlapped = win32.ConnectNamedPipe(h1, overlapped=True)
552+
overlapped = _winapi.ConnectNamedPipe(h1, overlapped=True)
553553
_, err = overlapped.GetOverlappedResult(True)
554554
assert err == 0
555555

@@ -630,26 +630,26 @@ def __init__(self, address, backlog=None):
630630
)
631631

632632
def _new_handle(self, first=False):
633-
flags = win32.PIPE_ACCESS_DUPLEX | win32.FILE_FLAG_OVERLAPPED
633+
flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED
634634
if first:
635-
flags |= win32.FILE_FLAG_FIRST_PIPE_INSTANCE
636-
return win32.CreateNamedPipe(
635+
flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
636+
return _winapi.CreateNamedPipe(
637637
self._address, flags,
638-
win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
639-
win32.PIPE_WAIT,
640-
win32.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
641-
win32.NMPWAIT_WAIT_FOREVER, win32.NULL
638+
_winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
639+
_winapi.PIPE_WAIT,
640+
_winapi.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
641+
_winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL
642642
)
643643

644644
def accept(self):
645645
self._handle_queue.append(self._new_handle())
646646
handle = self._handle_queue.pop(0)
647-
ov = win32.ConnectNamedPipe(handle, overlapped=True)
647+
ov = _winapi.ConnectNamedPipe(handle, overlapped=True)
648648
try:
649-
res = win32.WaitForMultipleObjects([ov.event], False, INFINITE)
649+
res = _winapi.WaitForMultipleObjects([ov.event], False, INFINITE)
650650
except:
651651
ov.cancel()
652-
win32.CloseHandle(handle)
652+
_winapi.CloseHandle(handle)
653653
raise
654654
finally:
655655
_, err = ov.GetOverlappedResult(True)
@@ -660,7 +660,7 @@ def accept(self):
660660
def _finalize_pipe_listener(queue, address):
661661
sub_debug('closing listener with address=%r', address)
662662
for handle in queue:
663-
win32.CloseHandle(handle)
663+
_winapi.CloseHandle(handle)
664664

665665
def PipeClient(address):
666666
'''
@@ -669,23 +669,23 @@ def PipeClient(address):
669669
t = _init_timeout()
670670
while 1:
671671
try:
672-
win32.WaitNamedPipe(address, 1000)
673-
h = win32.CreateFile(
674-
address, win32.GENERIC_READ | win32.GENERIC_WRITE,
675-
0, win32.NULL, win32.OPEN_EXISTING,
676-
win32.FILE_FLAG_OVERLAPPED, win32.NULL
672+
_winapi.WaitNamedPipe(address, 1000)
673+
h = _winapi.CreateFile(
674+
address, _winapi.GENERIC_READ | _winapi.GENERIC_WRITE,
675+
0, _winapi.NULL, _winapi.OPEN_EXISTING,
676+
_winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
677677
)
678678
except WindowsError as e:
679-
if e.winerror not in (win32.ERROR_SEM_TIMEOUT,
680-
win32.ERROR_PIPE_BUSY) or _check_timeout(t):
679+
if e.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
680+
_winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
681681
raise
682682
else:
683683
break
684684
else:
685685
raise
686686

687-
win32.SetNamedPipeHandleState(
688-
h, win32.PIPE_READMODE_MESSAGE, None, None
687+
_winapi.SetNamedPipeHandleState(
688+
h, _winapi.PIPE_READMODE_MESSAGE, None, None
689689
)
690690
return PipeConnection(h)
691691

@@ -774,7 +774,7 @@ def _exhaustive_wait(handles, timeout):
774774
L = list(handles)
775775
ready = []
776776
while L:
777-
res = win32.WaitForMultipleObjects(L, False, timeout)
777+
res = _winapi.WaitForMultipleObjects(L, False, timeout)
778778
if res == WAIT_TIMEOUT:
779779
break
780780
elif WAIT_OBJECT_0 <= res < WAIT_OBJECT_0 + len(L):
@@ -788,7 +788,7 @@ def _exhaustive_wait(handles, timeout):
788788
timeout = 0
789789
return ready
790790

791-
_ready_errors = {win32.ERROR_BROKEN_PIPE, win32.ERROR_NETNAME_DELETED}
791+
_ready_errors = {_winapi.ERROR_BROKEN_PIPE, _winapi.ERROR_NETNAME_DELETED}
792792

793793
def wait(object_list, timeout=None):
794794
'''
@@ -818,12 +818,12 @@ def wait(object_list, timeout=None):
818818
else:
819819
# start an overlapped read of length zero
820820
try:
821-
ov, err = win32.ReadFile(fileno(), 0, True)
821+
ov, err = _winapi.ReadFile(fileno(), 0, True)
822822
except OSError as e:
823823
err = e.winerror
824824
if err not in _ready_errors:
825825
raise
826-
if err == win32.ERROR_IO_PENDING:
826+
if err == _winapi.ERROR_IO_PENDING:
827827
ov_list.append(ov)
828828
waithandle_to_obj[ov.event] = o
829829
else:
@@ -847,7 +847,7 @@ def wait(object_list, timeout=None):
847847
err = e.winerror
848848
if err not in _ready_errors:
849849
raise
850-
if err != win32.ERROR_OPERATION_ABORTED:
850+
if err != _winapi.ERROR_OPERATION_ABORTED:
851851
o = waithandle_to_obj[ov.event]
852852
ready_objects.add(o)
853853
if err == 0:

Lib/multiprocessing/forking.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,9 @@ def thread_is_spawning():
181181
else:
182182
import _thread
183183
import msvcrt
184-
import _subprocess
184+
import _winapi
185185

186186
from pickle import load, HIGHEST_PROTOCOL
187-
from _multiprocessing import win32
188187

189188
def dump(obj, file, protocol=None):
190189
ForkingPickler(file, protocol).dump(obj)
@@ -197,8 +196,8 @@ def dump(obj, file, protocol=None):
197196
WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
198197
WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
199198

200-
exit = win32.ExitProcess
201-
close = win32.CloseHandle
199+
exit = _winapi.ExitProcess
200+
close = _winapi.CloseHandle
202201

203202
#
204203
# _python_exe is the assumed path to the python executable.
@@ -220,11 +219,11 @@ def set_executable(exe):
220219

221220
def duplicate(handle, target_process=None, inheritable=False):
222221
if target_process is None:
223-
target_process = _subprocess.GetCurrentProcess()
224-
return _subprocess.DuplicateHandle(
225-
_subprocess.GetCurrentProcess(), handle, target_process,
226-
0, inheritable, _subprocess.DUPLICATE_SAME_ACCESS
227-
).Detach()
222+
target_process = _winapi.GetCurrentProcess()
223+
return _winapi.DuplicateHandle(
224+
_winapi.GetCurrentProcess(), handle, target_process,
225+
0, inheritable, _winapi.DUPLICATE_SAME_ACCESS
226+
)
228227

229228
#
230229
# We define a Popen class similar to the one from subprocess, but
@@ -248,10 +247,10 @@ def __init__(self, process_obj):
248247
# start process
249248
cmd = get_command_line() + [rhandle]
250249
cmd = ' '.join('"%s"' % x for x in cmd)
251-
hp, ht, pid, tid = _subprocess.CreateProcess(
250+
hp, ht, pid, tid = _winapi.CreateProcess(
252251
_python_exe, cmd, None, None, 1, 0, None, None, None
253252
)
254-
ht.Close()
253+
_winapi.CloseHandle(ht)
255254
close(rhandle)
256255

257256
# set attributes of self
@@ -282,13 +281,13 @@ def duplicate_for_child(handle):
282281
def wait(self, timeout=None):
283282
if self.returncode is None:
284283
if timeout is None:
285-
msecs = _subprocess.INFINITE
284+
msecs = _winapi.INFINITE
286285
else:
287286
msecs = max(0, int(timeout * 1000 + 0.5))
288287

289-
res = _subprocess.WaitForSingleObject(int(self._handle), msecs)
290-
if res == _subprocess.WAIT_OBJECT_0:
291-
code = _subprocess.GetExitCodeProcess(self._handle)
288+
res = _winapi.WaitForSingleObject(int(self._handle), msecs)
289+
if res == _winapi.WAIT_OBJECT_0:
290+
code = _winapi.GetExitCodeProcess(self._handle)
292291
if code == TERMINATE:
293292
code = -signal.SIGTERM
294293
self.returncode = code
@@ -301,7 +300,7 @@ def poll(self):
301300
def terminate(self):
302301
if self.returncode is None:
303302
try:
304-
_subprocess.TerminateProcess(int(self._handle), TERMINATE)
303+
_winapi.TerminateProcess(int(self._handle), TERMINATE)
305304
except WindowsError:
306305
if self.wait(timeout=0.1) is None:
307306
raise

Lib/multiprocessing/heap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
if sys.platform == 'win32':
5353

54-
from _multiprocessing import win32
54+
import _winapi
5555

5656
class Arena(object):
5757

@@ -61,7 +61,7 @@ def __init__(self, size):
6161
self.size = size
6262
self.name = 'pym-%d-%d' % (os.getpid(), next(Arena._counter))
6363
self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
64-
assert win32.GetLastError() == 0, 'tagname already in use'
64+
assert _winapi.GetLastError() == 0, 'tagname already in use'
6565
self._state = (self.size, self.name)
6666

6767
def __getstate__(self):
@@ -71,7 +71,7 @@ def __getstate__(self):
7171
def __setstate__(self, state):
7272
self.size, self.name = self._state = state
7373
self.buffer = mmap.mmap(-1, self.size, tagname=self.name)
74-
assert win32.GetLastError() == win32.ERROR_ALREADY_EXISTS
74+
assert _winapi.GetLastError() == _winapi.ERROR_ALREADY_EXISTS
7575

7676
else:
7777

0 commit comments

Comments
 (0)