2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# A higher level module for using sockets (or Windows named pipes)
|
|
|
|
#
|
|
|
|
# multiprocessing/connection.py
|
|
|
|
#
|
2010-12-13 21:38:16 -04:00
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
2012-04-30 08:13:55 -03:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
__all__ = [ 'Client', 'Listener', 'Pipe', 'wait' ]
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
import io
|
2008-06-11 13:44:04 -03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import socket
|
2011-05-09 12:04:27 -03:00
|
|
|
import struct
|
2008-06-11 13:44:04 -03:00
|
|
|
import time
|
|
|
|
import tempfile
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
import _multiprocessing
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
from . import reduction
|
|
|
|
from . import util
|
|
|
|
|
|
|
|
from . import AuthenticationError, BufferTooShort
|
|
|
|
from .reduction import ForkingPickler
|
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
import _winapi
|
2014-03-20 04:50:52 -03:00
|
|
|
from _winapi import WAIT_OBJECT_0, WAIT_ABANDONED_0, WAIT_TIMEOUT, INFINITE
|
2013-07-04 18:43:24 -03:00
|
|
|
except ImportError:
|
2011-05-09 12:04:27 -03:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
raise
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi = None
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
BUFSIZE = 8192
|
2009-11-13 18:35:18 -04:00
|
|
|
# A very generous timeout when it comes to local connections...
|
|
|
|
CONNECTION_TIMEOUT = 20.
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
_mmap_counter = itertools.count()
|
|
|
|
|
|
|
|
default_family = 'AF_INET'
|
|
|
|
families = ['AF_INET']
|
|
|
|
|
|
|
|
if hasattr(socket, 'AF_UNIX'):
|
|
|
|
default_family = 'AF_UNIX'
|
|
|
|
families += ['AF_UNIX']
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
default_family = 'AF_PIPE'
|
|
|
|
families += ['AF_PIPE']
|
|
|
|
|
2009-11-13 18:35:18 -04:00
|
|
|
|
|
|
|
def _init_timeout(timeout=CONNECTION_TIMEOUT):
|
|
|
|
return time.time() + timeout
|
|
|
|
|
|
|
|
def _check_timeout(t):
|
|
|
|
return time.time() > t
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def arbitrary_address(family):
|
|
|
|
'''
|
|
|
|
Return an arbitrary free address for the given family
|
|
|
|
'''
|
|
|
|
if family == 'AF_INET':
|
|
|
|
return ('localhost', 0)
|
|
|
|
elif family == 'AF_UNIX':
|
2013-08-14 11:35:41 -03:00
|
|
|
return tempfile.mktemp(prefix='listener-', dir=util.get_temp_dir())
|
2008-06-11 13:44:04 -03:00
|
|
|
elif family == 'AF_PIPE':
|
|
|
|
return tempfile.mktemp(prefix=r'\\.\pipe\pyc-%d-%d-' %
|
2014-04-14 13:24:37 -03:00
|
|
|
(os.getpid(), next(_mmap_counter)), dir="")
|
2008-06-11 13:44:04 -03:00
|
|
|
else:
|
|
|
|
raise ValueError('unrecognized family')
|
|
|
|
|
2012-04-01 12:19:09 -03:00
|
|
|
def _validate_family(family):
|
|
|
|
'''
|
|
|
|
Checks if the family is valid for the current environment.
|
|
|
|
'''
|
|
|
|
if sys.platform != 'win32' and family == 'AF_PIPE':
|
|
|
|
raise ValueError('Family %s is not recognized.' % family)
|
|
|
|
|
2012-04-03 15:12:23 -03:00
|
|
|
if sys.platform == 'win32' and family == 'AF_UNIX':
|
|
|
|
# double check
|
|
|
|
if not hasattr(socket, family):
|
|
|
|
raise ValueError('Family %s is not recognized.' % family)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def address_type(address):
|
|
|
|
'''
|
|
|
|
Return the types of the address
|
|
|
|
|
|
|
|
This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE'
|
|
|
|
'''
|
|
|
|
if type(address) == tuple:
|
|
|
|
return 'AF_INET'
|
|
|
|
elif type(address) is str and address.startswith('\\\\'):
|
|
|
|
return 'AF_PIPE'
|
|
|
|
elif type(address) is str:
|
|
|
|
return 'AF_UNIX'
|
|
|
|
else:
|
|
|
|
raise ValueError('address type of %r unrecognized' % address)
|
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
#
|
|
|
|
# Connection classes
|
|
|
|
#
|
|
|
|
|
|
|
|
class _ConnectionBase:
|
|
|
|
_handle = None
|
|
|
|
|
|
|
|
def __init__(self, handle, readable=True, writable=True):
|
|
|
|
handle = handle.__index__()
|
|
|
|
if handle < 0:
|
|
|
|
raise ValueError("invalid handle")
|
|
|
|
if not readable and not writable:
|
|
|
|
raise ValueError(
|
|
|
|
"at least one of `readable` and `writable` must be True")
|
|
|
|
self._handle = handle
|
|
|
|
self._readable = readable
|
|
|
|
self._writable = writable
|
|
|
|
|
2011-07-08 20:03:46 -03:00
|
|
|
# XXX should we use util.Finalize instead of a __del__?
|
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
def __del__(self):
|
|
|
|
if self._handle is not None:
|
|
|
|
self._close()
|
|
|
|
|
|
|
|
def _check_closed(self):
|
|
|
|
if self._handle is None:
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError("handle is closed")
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def _check_readable(self):
|
|
|
|
if not self._readable:
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError("connection is write-only")
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def _check_writable(self):
|
|
|
|
if not self._writable:
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError("connection is read-only")
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def _bad_message_length(self):
|
|
|
|
if self._writable:
|
|
|
|
self._readable = False
|
|
|
|
else:
|
|
|
|
self.close()
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError("bad message length")
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def closed(self):
|
|
|
|
"""True if the connection is closed"""
|
|
|
|
return self._handle is None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def readable(self):
|
|
|
|
"""True if the connection is readable"""
|
|
|
|
return self._readable
|
|
|
|
|
|
|
|
@property
|
|
|
|
def writable(self):
|
|
|
|
"""True if the connection is writable"""
|
|
|
|
return self._writable
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
"""File descriptor or handle of the connection"""
|
|
|
|
self._check_closed()
|
|
|
|
return self._handle
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Close the connection"""
|
|
|
|
if self._handle is not None:
|
|
|
|
try:
|
|
|
|
self._close()
|
|
|
|
finally:
|
|
|
|
self._handle = None
|
|
|
|
|
|
|
|
def send_bytes(self, buf, offset=0, size=None):
|
|
|
|
"""Send the bytes data from a bytes-like object"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_writable()
|
|
|
|
m = memoryview(buf)
|
|
|
|
# HACK for byte-indexing of non-bytewise buffers (e.g. array.array)
|
|
|
|
if m.itemsize > 1:
|
|
|
|
m = memoryview(bytes(m))
|
|
|
|
n = len(m)
|
|
|
|
if offset < 0:
|
|
|
|
raise ValueError("offset is negative")
|
|
|
|
if n < offset:
|
|
|
|
raise ValueError("buffer length < offset")
|
|
|
|
if size is None:
|
|
|
|
size = n - offset
|
|
|
|
elif size < 0:
|
|
|
|
raise ValueError("size is negative")
|
|
|
|
elif offset + size > n:
|
|
|
|
raise ValueError("buffer length < offset + size")
|
|
|
|
self._send_bytes(m[offset:offset + size])
|
|
|
|
|
|
|
|
def send(self, obj):
|
|
|
|
"""Send a (picklable) object"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_writable()
|
2013-03-24 11:21:49 -03:00
|
|
|
self._send_bytes(ForkingPickler.dumps(obj))
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def recv_bytes(self, maxlength=None):
|
|
|
|
"""
|
|
|
|
Receive bytes data as a bytes object.
|
|
|
|
"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_readable()
|
|
|
|
if maxlength is not None and maxlength < 0:
|
|
|
|
raise ValueError("negative maxlength")
|
|
|
|
buf = self._recv_bytes(maxlength)
|
|
|
|
if buf is None:
|
|
|
|
self._bad_message_length()
|
|
|
|
return buf.getvalue()
|
|
|
|
|
|
|
|
def recv_bytes_into(self, buf, offset=0):
|
|
|
|
"""
|
2014-12-05 16:25:22 -04:00
|
|
|
Receive bytes data into a writeable bytes-like object.
|
2011-05-09 12:04:27 -03:00
|
|
|
Return the number of bytes read.
|
|
|
|
"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_readable()
|
|
|
|
with memoryview(buf) as m:
|
|
|
|
# Get bytesize of arbitrary buffer
|
|
|
|
itemsize = m.itemsize
|
|
|
|
bytesize = itemsize * len(m)
|
|
|
|
if offset < 0:
|
|
|
|
raise ValueError("negative offset")
|
|
|
|
elif offset > bytesize:
|
|
|
|
raise ValueError("offset too large")
|
|
|
|
result = self._recv_bytes()
|
|
|
|
size = result.tell()
|
|
|
|
if bytesize < offset + size:
|
|
|
|
raise BufferTooShort(result.getvalue())
|
|
|
|
# Message can fit in dest
|
|
|
|
result.seek(0)
|
|
|
|
result.readinto(m[offset // itemsize :
|
|
|
|
(offset + size) // itemsize])
|
|
|
|
return size
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def recv(self):
|
2011-05-09 12:04:27 -03:00
|
|
|
"""Receive a (picklable) object"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_readable()
|
2012-03-05 14:28:37 -04:00
|
|
|
buf = self._recv_bytes()
|
2013-03-24 11:21:49 -03:00
|
|
|
return ForkingPickler.loads(buf.getbuffer())
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def poll(self, timeout=0.0):
|
|
|
|
"""Whether there is any input available to be read"""
|
|
|
|
self._check_closed()
|
|
|
|
self._check_readable()
|
|
|
|
return self._poll(timeout)
|
|
|
|
|
2012-06-18 13:47:52 -03:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_tb):
|
|
|
|
self.close()
|
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
if _winapi:
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
class PipeConnection(_ConnectionBase):
|
|
|
|
"""
|
|
|
|
Connection class based on a Windows named pipe.
|
2011-06-08 12:21:55 -03:00
|
|
|
Overlapped I/O is used, so the handles must have been created
|
|
|
|
with FILE_FLAG_OVERLAPPED.
|
2011-05-09 12:04:27 -03:00
|
|
|
"""
|
2012-03-05 14:28:37 -04:00
|
|
|
_got_empty_message = False
|
2011-05-09 12:04:27 -03:00
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
def _close(self, _CloseHandle=_winapi.CloseHandle):
|
2011-07-08 20:03:00 -03:00
|
|
|
_CloseHandle(self._handle)
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def _send_bytes(self, buf):
|
2012-04-18 15:51:15 -03:00
|
|
|
ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
|
2012-03-05 14:28:37 -04:00
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
if err == _winapi.ERROR_IO_PENDING:
|
|
|
|
waitres = _winapi.WaitForMultipleObjects(
|
2012-03-05 14:28:37 -04:00
|
|
|
[ov.event], False, INFINITE)
|
|
|
|
assert waitres == WAIT_OBJECT_0
|
|
|
|
except:
|
|
|
|
ov.cancel()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
nwritten, err = ov.GetOverlappedResult(True)
|
|
|
|
assert err == 0
|
2011-05-09 12:04:27 -03:00
|
|
|
assert nwritten == len(buf)
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def _recv_bytes(self, maxsize=None):
|
|
|
|
if self._got_empty_message:
|
|
|
|
self._got_empty_message = False
|
|
|
|
return io.BytesIO()
|
2011-06-08 12:21:55 -03:00
|
|
|
else:
|
2012-03-05 14:28:37 -04:00
|
|
|
bsize = 128 if maxsize is None else min(maxsize, 128)
|
2011-06-08 12:21:55 -03:00
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
ov, err = _winapi.ReadFile(self._handle, bsize,
|
|
|
|
overlapped=True)
|
2012-03-05 14:28:37 -04:00
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
if err == _winapi.ERROR_IO_PENDING:
|
|
|
|
waitres = _winapi.WaitForMultipleObjects(
|
2012-03-05 14:28:37 -04:00
|
|
|
[ov.event], False, INFINITE)
|
|
|
|
assert waitres == WAIT_OBJECT_0
|
|
|
|
except:
|
|
|
|
ov.cancel()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
nread, err = ov.GetOverlappedResult(True)
|
|
|
|
if err == 0:
|
|
|
|
f = io.BytesIO()
|
|
|
|
f.write(ov.getbuffer())
|
|
|
|
return f
|
2012-04-18 15:51:15 -03:00
|
|
|
elif err == _winapi.ERROR_MORE_DATA:
|
2012-03-05 14:28:37 -04:00
|
|
|
return self._get_more_data(ov, maxsize)
|
2012-12-25 10:47:37 -04:00
|
|
|
except OSError as e:
|
2012-04-18 15:51:15 -03:00
|
|
|
if e.winerror == _winapi.ERROR_BROKEN_PIPE:
|
2011-06-08 12:21:55 -03:00
|
|
|
raise EOFError
|
2012-03-05 14:28:37 -04:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
raise RuntimeError("shouldn't get here; expected KeyboardInterrupt")
|
|
|
|
|
|
|
|
def _poll(self, timeout):
|
|
|
|
if (self._got_empty_message or
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.PeekNamedPipe(self._handle)[0] != 0):
|
2011-06-08 12:21:55 -03:00
|
|
|
return True
|
2012-03-05 14:28:37 -04:00
|
|
|
return bool(wait([self], timeout))
|
|
|
|
|
|
|
|
def _get_more_data(self, ov, maxsize):
|
|
|
|
buf = ov.getbuffer()
|
|
|
|
f = io.BytesIO()
|
|
|
|
f.write(buf)
|
2012-04-18 15:51:15 -03:00
|
|
|
left = _winapi.PeekNamedPipe(self._handle)[1]
|
2012-03-05 14:28:37 -04:00
|
|
|
assert left > 0
|
|
|
|
if maxsize is not None and len(buf) + left > maxsize:
|
|
|
|
self._bad_message_length()
|
2012-04-18 15:51:15 -03:00
|
|
|
ov, err = _winapi.ReadFile(self._handle, left, overlapped=True)
|
2012-03-05 14:28:37 -04:00
|
|
|
rbytes, err = ov.GetOverlappedResult(True)
|
|
|
|
assert err == 0
|
|
|
|
assert rbytes == left
|
|
|
|
f.write(ov.getbuffer())
|
|
|
|
return f
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
|
|
|
|
class Connection(_ConnectionBase):
|
|
|
|
"""
|
|
|
|
Connection class based on an arbitrary file descriptor (Unix only), or
|
|
|
|
a socket handle (Windows).
|
|
|
|
"""
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
if _winapi:
|
|
|
|
def _close(self, _close=_multiprocessing.closesocket):
|
2011-07-08 20:03:00 -03:00
|
|
|
_close(self._handle)
|
2012-04-18 15:51:15 -03:00
|
|
|
_write = _multiprocessing.send
|
|
|
|
_read = _multiprocessing.recv
|
2011-05-09 12:04:27 -03:00
|
|
|
else:
|
2011-07-08 20:03:00 -03:00
|
|
|
def _close(self, _close=os.close):
|
|
|
|
_close(self._handle)
|
2011-05-09 12:04:27 -03:00
|
|
|
_write = os.write
|
|
|
|
_read = os.read
|
|
|
|
|
|
|
|
def _send(self, buf, write=_write):
|
|
|
|
remaining = len(buf)
|
|
|
|
while True:
|
2015-02-07 09:27:50 -04:00
|
|
|
n = write(self._handle, buf)
|
2011-05-09 12:04:27 -03:00
|
|
|
remaining -= n
|
|
|
|
if remaining == 0:
|
|
|
|
break
|
|
|
|
buf = buf[n:]
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def _recv(self, size, read=_read):
|
2011-05-09 12:04:27 -03:00
|
|
|
buf = io.BytesIO()
|
2011-06-08 12:21:55 -03:00
|
|
|
handle = self._handle
|
2011-05-09 12:04:27 -03:00
|
|
|
remaining = size
|
|
|
|
while remaining > 0:
|
2015-02-07 09:27:50 -04:00
|
|
|
chunk = read(handle, remaining)
|
2011-05-09 12:04:27 -03:00
|
|
|
n = len(chunk)
|
|
|
|
if n == 0:
|
|
|
|
if remaining == size:
|
|
|
|
raise EOFError
|
|
|
|
else:
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError("got end of file during message")
|
2011-05-09 12:04:27 -03:00
|
|
|
buf.write(chunk)
|
|
|
|
remaining -= n
|
|
|
|
return buf
|
|
|
|
|
|
|
|
def _send_bytes(self, buf):
|
|
|
|
n = len(buf)
|
2014-02-08 18:03:56 -04:00
|
|
|
# For wire compatibility with 3.2 and lower
|
|
|
|
header = struct.pack("!i", n)
|
|
|
|
if n > 16384:
|
|
|
|
# The payload is large so Nagle's algorithm won't be triggered
|
|
|
|
# and we'd better avoid the cost of concatenation.
|
2014-07-31 19:41:57 -03:00
|
|
|
self._send(header)
|
|
|
|
self._send(buf)
|
|
|
|
else:
|
2015-12-18 07:05:04 -04:00
|
|
|
# Issue #20540: concatenate before sending, to avoid delays due
|
2014-02-08 18:03:56 -04:00
|
|
|
# to Nagle's algorithm on a TCP socket.
|
2014-07-31 19:41:57 -03:00
|
|
|
# Also note we want to avoid sending a 0-length buffer separately,
|
|
|
|
# to avoid "broken pipe" errors if the other end closed the pipe.
|
|
|
|
self._send(header + buf)
|
2011-05-09 12:04:27 -03:00
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def _recv_bytes(self, maxsize=None):
|
|
|
|
buf = self._recv(4)
|
2011-09-20 14:27:39 -03:00
|
|
|
size, = struct.unpack("!i", buf.getvalue())
|
2011-05-09 12:04:27 -03:00
|
|
|
if maxsize is not None and size > maxsize:
|
|
|
|
return None
|
2012-03-05 14:28:37 -04:00
|
|
|
return self._recv(size)
|
2011-05-09 12:04:27 -03:00
|
|
|
|
|
|
|
def _poll(self, timeout):
|
2013-01-13 18:46:48 -04:00
|
|
|
r = wait([self], timeout)
|
2011-05-09 12:04:27 -03:00
|
|
|
return bool(r)
|
|
|
|
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Public functions
|
|
|
|
#
|
|
|
|
|
|
|
|
class Listener(object):
|
|
|
|
'''
|
|
|
|
Returns a listener object.
|
|
|
|
|
|
|
|
This is a wrapper for a bound socket which is 'listening' for
|
|
|
|
connections, or for a Windows named pipe.
|
|
|
|
'''
|
|
|
|
def __init__(self, address=None, family=None, backlog=1, authkey=None):
|
|
|
|
family = family or (address and address_type(address)) \
|
|
|
|
or default_family
|
|
|
|
address = address or arbitrary_address(family)
|
|
|
|
|
2012-04-01 12:19:09 -03:00
|
|
|
_validate_family(family)
|
2008-06-11 13:44:04 -03:00
|
|
|
if family == 'AF_PIPE':
|
|
|
|
self._listener = PipeListener(address, backlog)
|
|
|
|
else:
|
|
|
|
self._listener = SocketListener(address, family, backlog)
|
|
|
|
|
|
|
|
if authkey is not None and not isinstance(authkey, bytes):
|
|
|
|
raise TypeError('authkey should be a byte string')
|
|
|
|
|
|
|
|
self._authkey = authkey
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
'''
|
|
|
|
Accept a connection on the bound socket or named pipe of `self`.
|
|
|
|
|
|
|
|
Returns a `Connection` object.
|
|
|
|
'''
|
2012-06-18 13:47:52 -03:00
|
|
|
if self._listener is None:
|
2012-12-25 10:47:37 -04:00
|
|
|
raise OSError('listener is closed')
|
2008-06-11 13:44:04 -03:00
|
|
|
c = self._listener.accept()
|
|
|
|
if self._authkey:
|
|
|
|
deliver_challenge(c, self._authkey)
|
|
|
|
answer_challenge(c, self._authkey)
|
|
|
|
return c
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
'''
|
|
|
|
Close the bound socket or named pipe of `self`.
|
|
|
|
'''
|
2015-04-10 07:24:41 -03:00
|
|
|
listener = self._listener
|
|
|
|
if listener is not None:
|
2012-06-18 13:47:52 -03:00
|
|
|
self._listener = None
|
2015-04-10 07:24:41 -03:00
|
|
|
listener.close()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
address = property(lambda self: self._listener._address)
|
|
|
|
last_accepted = property(lambda self: self._listener._last_accepted)
|
|
|
|
|
2012-06-18 13:47:52 -03:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_tb):
|
|
|
|
self.close()
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def Client(address, family=None, authkey=None):
|
|
|
|
'''
|
|
|
|
Returns a connection to the address of a `Listener`
|
|
|
|
'''
|
|
|
|
family = family or address_type(address)
|
2012-04-01 12:19:09 -03:00
|
|
|
_validate_family(family)
|
2008-06-11 13:44:04 -03:00
|
|
|
if family == 'AF_PIPE':
|
|
|
|
c = PipeClient(address)
|
|
|
|
else:
|
|
|
|
c = SocketClient(address)
|
|
|
|
|
|
|
|
if authkey is not None and not isinstance(authkey, bytes):
|
|
|
|
raise TypeError('authkey should be a byte string')
|
|
|
|
|
|
|
|
if authkey is not None:
|
|
|
|
answer_challenge(c, authkey)
|
|
|
|
deliver_challenge(c, authkey)
|
|
|
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
|
|
|
|
def Pipe(duplex=True):
|
|
|
|
'''
|
|
|
|
Returns pair of connection objects at either end of a pipe
|
|
|
|
'''
|
|
|
|
if duplex:
|
|
|
|
s1, s2 = socket.socketpair()
|
2012-07-27 10:19:00 -03:00
|
|
|
s1.setblocking(True)
|
|
|
|
s2.setblocking(True)
|
2011-05-09 16:00:28 -03:00
|
|
|
c1 = Connection(s1.detach())
|
|
|
|
c2 = Connection(s2.detach())
|
2008-06-11 13:44:04 -03:00
|
|
|
else:
|
2013-08-27 19:53:59 -03:00
|
|
|
fd1, fd2 = os.pipe()
|
2011-05-09 12:04:27 -03:00
|
|
|
c1 = Connection(fd1, writable=False)
|
|
|
|
c2 = Connection(fd2, readable=False)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
return c1, c2
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
def Pipe(duplex=True):
|
|
|
|
'''
|
|
|
|
Returns pair of connection objects at either end of a pipe
|
|
|
|
'''
|
|
|
|
address = arbitrary_address('AF_PIPE')
|
|
|
|
if duplex:
|
2012-04-18 15:51:15 -03:00
|
|
|
openmode = _winapi.PIPE_ACCESS_DUPLEX
|
|
|
|
access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
|
2008-06-11 13:44:04 -03:00
|
|
|
obsize, ibsize = BUFSIZE, BUFSIZE
|
|
|
|
else:
|
2012-04-18 15:51:15 -03:00
|
|
|
openmode = _winapi.PIPE_ACCESS_INBOUND
|
|
|
|
access = _winapi.GENERIC_WRITE
|
2008-06-11 13:44:04 -03:00
|
|
|
obsize, ibsize = 0, BUFSIZE
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
h1 = _winapi.CreateNamedPipe(
|
|
|
|
address, openmode | _winapi.FILE_FLAG_OVERLAPPED |
|
|
|
|
_winapi.FILE_FLAG_FIRST_PIPE_INSTANCE,
|
|
|
|
_winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
|
|
|
|
_winapi.PIPE_WAIT,
|
2013-08-27 19:53:59 -03:00
|
|
|
1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER,
|
|
|
|
# default security descriptor: the handle cannot be inherited
|
|
|
|
_winapi.NULL
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
2012-04-18 15:51:15 -03:00
|
|
|
h2 = _winapi.CreateFile(
|
|
|
|
address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING,
|
|
|
|
_winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.SetNamedPipeHandleState(
|
|
|
|
h2, _winapi.PIPE_READMODE_MESSAGE, None, None
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
overlapped = _winapi.ConnectNamedPipe(h1, overlapped=True)
|
2012-03-05 14:28:37 -04:00
|
|
|
_, err = overlapped.GetOverlappedResult(True)
|
|
|
|
assert err == 0
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2011-05-09 12:04:27 -03:00
|
|
|
c1 = PipeConnection(h1, writable=duplex)
|
|
|
|
c2 = PipeConnection(h2, readable=duplex)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
return c1, c2
|
|
|
|
|
|
|
|
#
|
|
|
|
# Definitions for connections based on sockets
|
|
|
|
#
|
|
|
|
|
|
|
|
class SocketListener(object):
|
|
|
|
'''
|
Merged revisions 65437,65469,65476,65480,65502,65528,65539,65543,65558,65561-65562,65565,65591,65601,65608,65610,65639 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65437 | georg.brandl | 2008-08-03 22:28:55 +0000 (Sun, 03 Aug 2008) | 2 lines
Note the removal of several committers.
........
r65469 | gregory.p.smith | 2008-08-04 01:03:50 +0000 (Mon, 04 Aug 2008) | 3 lines
issue1606: Add warnings to the subprocess documentation about common pitfalls
of using pipes that cause deadlocks.
........
r65476 | georg.brandl | 2008-08-04 06:29:36 +0000 (Mon, 04 Aug 2008) | 2 lines
Fix markup.
........
r65480 | georg.brandl | 2008-08-04 07:31:50 +0000 (Mon, 04 Aug 2008) | 3 lines
Clarify the meaning of the select() parameters and sync
names with docstring.
........
r65502 | gregory.p.smith | 2008-08-04 18:34:07 +0000 (Mon, 04 Aug 2008) | 2 lines
more cleanup ups of the recently added warnings in the subprocess docs.
........
r65528 | brett.cannon | 2008-08-04 21:52:25 +0000 (Mon, 04 Aug 2008) | 4 lines
Add a note about all the modules/packages changed to silence -3 warnings. More
changes are needed once some decisions are made, but this is the work up to this
point.
........
r65539 | andrew.kuchling | 2008-08-05 01:38:08 +0000 (Tue, 05 Aug 2008) | 6 lines
#3367 from Kristjan Valur Jonsson:
If a PyTokenizer_FromString() is called with an empty string, the
tokenizer's line_start member never gets initialized. Later, it is
compared with the token pointer 'a' in parsetok.c:193 and that behavior
can result in undefined behavior.
........
r65543 | andrew.kuchling | 2008-08-05 02:05:23 +0000 (Tue, 05 Aug 2008) | 1 line
#3367: revert rev. 65539: this change causes test_parser to fail
........
r65558 | georg.brandl | 2008-08-06 17:20:41 +0000 (Wed, 06 Aug 2008) | 2 lines
Fix longstringitem definition. #3505.
........
r65561 | mark.dickinson | 2008-08-06 20:12:30 +0000 (Wed, 06 Aug 2008) | 2 lines
Docstring typo
........
r65562 | mark.dickinson | 2008-08-06 21:36:57 +0000 (Wed, 06 Aug 2008) | 2 lines
Remove duplicate import
........
r65565 | andrew.kuchling | 2008-08-07 01:47:34 +0000 (Thu, 07 Aug 2008) | 1 line
Add some items
........
r65591 | georg.brandl | 2008-08-08 06:42:20 +0000 (Fri, 08 Aug 2008) | 2 lines
#3519: callee is an expression too.
........
r65601 | georg.brandl | 2008-08-08 15:34:34 +0000 (Fri, 08 Aug 2008) | 2 lines
Remove mention of backquotes in the tutorial.
........
r65608 | guido.van.rossum | 2008-08-09 14:55:34 +0000 (Sat, 09 Aug 2008) | 2 lines
Add news item about _sre.compile() re-bytecode validator.
........
r65610 | antoine.pitrou | 2008-08-09 17:27:23 +0000 (Sat, 09 Aug 2008) | 3 lines
move NEWS entry to the appropriate section (oops!)
........
r65639 | georg.brandl | 2008-08-11 10:27:31 +0000 (Mon, 11 Aug 2008) | 2 lines
#3540: fix exception name.
........
2008-08-12 05:18:18 -03:00
|
|
|
Representation of a socket which is bound to an address and listening
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
def __init__(self, address, family, backlog=1):
|
|
|
|
self._socket = socket.socket(getattr(socket, family))
|
2012-02-04 10:12:08 -04:00
|
|
|
try:
|
2012-02-08 16:15:58 -04:00
|
|
|
# SO_REUSEADDR has different semantics on Windows (issue #2550).
|
|
|
|
if os.name == 'posix':
|
|
|
|
self._socket.setsockopt(socket.SOL_SOCKET,
|
|
|
|
socket.SO_REUSEADDR, 1)
|
2012-07-27 10:19:00 -03:00
|
|
|
self._socket.setblocking(True)
|
2012-02-04 10:12:08 -04:00
|
|
|
self._socket.bind(address)
|
|
|
|
self._socket.listen(backlog)
|
|
|
|
self._address = self._socket.getsockname()
|
|
|
|
except OSError:
|
|
|
|
self._socket.close()
|
|
|
|
raise
|
2008-06-11 13:44:04 -03:00
|
|
|
self._family = family
|
|
|
|
self._last_accepted = None
|
|
|
|
|
|
|
|
if family == 'AF_UNIX':
|
2013-08-14 11:35:41 -03:00
|
|
|
self._unlink = util.Finalize(
|
Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64722 | georg.brandl | 2008-07-05 12:13:36 +0200 (Sat, 05 Jul 2008) | 4 lines
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
This is a new feature, but Barry authorized adding it in the beta period.
........
r64729 | mark.dickinson | 2008-07-05 13:33:52 +0200 (Sat, 05 Jul 2008) | 5 lines
Issue 3188: accept float('infinity') as well as float('inf'). This
makes the float constructor behave in the same way as specified
by various other language standards, including C99, IEEE 754r,
and the IBM Decimal standard.
........
r64753 | gregory.p.smith | 2008-07-06 05:35:58 +0200 (Sun, 06 Jul 2008) | 4 lines
- Issue #2862: Make int and float freelist management consistent with other
freelists. Changes their CompactFreeList apis into ClearFreeList apis and
calls them via gc.collect().
........
r64845 | raymond.hettinger | 2008-07-10 16:03:19 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3301: Bisect functions behaved badly when lo was negative.
........
r64846 | raymond.hettinger | 2008-07-10 16:34:57 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
........
r64849 | andrew.kuchling | 2008-07-10 16:43:31 +0200 (Thu, 10 Jul 2008) | 1 line
Wording changes
........
r64871 | raymond.hettinger | 2008-07-11 14:00:21 +0200 (Fri, 11 Jul 2008) | 1 line
Add cautionary note on the use of PySequence_Fast_ITEMS.
........
r64880 | amaury.forgeotdarc | 2008-07-11 23:28:25 +0200 (Fri, 11 Jul 2008) | 5 lines
#3317 in zipfile module, restore the previous names of global variables:
some applications relied on them.
Also remove duplicated lines.
........
r64881 | amaury.forgeotdarc | 2008-07-11 23:45:06 +0200 (Fri, 11 Jul 2008) | 3 lines
#3342: In tracebacks, printed source lines were not indented since r62555.
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.
........
r64882 | josiah.carlson | 2008-07-12 00:17:14 +0200 (Sat, 12 Jul 2008) | 2 lines
Fix for the AttributeError in test_asynchat.
........
r64885 | josiah.carlson | 2008-07-12 01:26:59 +0200 (Sat, 12 Jul 2008) | 2 lines
Fixed test for asyncore.
........
r64888 | matthias.klose | 2008-07-12 09:51:48 +0200 (Sat, 12 Jul 2008) | 2 lines
- Fix bashisms in Tools/faqwiz/move-faqwiz.sh
........
r64897 | benjamin.peterson | 2008-07-12 22:16:19 +0200 (Sat, 12 Jul 2008) | 1 line
fix various doc typos #3320
........
r64900 | alexandre.vassalotti | 2008-07-13 00:06:53 +0200 (Sun, 13 Jul 2008) | 2 lines
Fixed typo.
........
r64901 | benjamin.peterson | 2008-07-13 01:41:19 +0200 (Sun, 13 Jul 2008) | 1 line
#1778443 robotparser fixes from Aristotelis Mikropoulos
........
r64915 | nick.coghlan | 2008-07-13 16:52:36 +0200 (Sun, 13 Jul 2008) | 1 line
Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
........
r64926 | martin.v.loewis | 2008-07-13 22:31:49 +0200 (Sun, 13 Jul 2008) | 2 lines
Add turtle into the module index.
........
r64927 | alexandre.vassalotti | 2008-07-13 22:42:44 +0200 (Sun, 13 Jul 2008) | 3 lines
Issue #3274: Use a less common identifier for the temporary variable
in Py_CLEAR().
........
r64928 | andrew.kuchling | 2008-07-13 23:43:25 +0200 (Sun, 13 Jul 2008) | 1 line
Re-word
........
r64929 | andrew.kuchling | 2008-07-13 23:43:52 +0200 (Sun, 13 Jul 2008) | 1 line
Add various items; move ctypes items into a subsection of their own
........
r64938 | andrew.kuchling | 2008-07-14 02:35:32 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fixes
........
r64939 | andrew.kuchling | 2008-07-14 02:40:55 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64940 | andrew.kuchling | 2008-07-14 03:18:16 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64941 | andrew.kuchling | 2008-07-14 03:18:31 +0200 (Mon, 14 Jul 2008) | 1 line
Expand the multiprocessing section
........
r64944 | gregory.p.smith | 2008-07-14 08:06:48 +0200 (Mon, 14 Jul 2008) | 7 lines
Fix posix.fork1() / os.fork1() to only call PyOS_AfterFork() in the child
process rather than both parent and child.
Does anyone actually use fork1()? It appears to be a Solaris thing
but if Python is built with pthreads on Solaris, fork1() and fork()
should be the same.
........
r64961 | jesse.noller | 2008-07-15 15:47:33 +0200 (Tue, 15 Jul 2008) | 1 line
multiprocessing/connection.py patch to remove fqdn oddness for issue 3270
........
r64966 | nick.coghlan | 2008-07-15 17:40:22 +0200 (Tue, 15 Jul 2008) | 1 line
Add missing NEWS entry for r64962
........
r64973 | jesse.noller | 2008-07-15 20:29:18 +0200 (Tue, 15 Jul 2008) | 1 line
Revert 3270 patch: self._address is in pretty widespread use, need to revisit
........
2008-07-16 09:55:28 -03:00
|
|
|
self, os.unlink, args=(address,), exitpriority=0
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._unlink = None
|
|
|
|
|
|
|
|
def accept(self):
|
2015-02-07 09:27:50 -04:00
|
|
|
s, self._last_accepted = self._socket.accept()
|
2012-07-27 10:19:00 -03:00
|
|
|
s.setblocking(True)
|
2012-04-07 17:38:52 -03:00
|
|
|
return Connection(s.detach())
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def close(self):
|
2015-04-10 07:24:41 -03:00
|
|
|
try:
|
|
|
|
self._socket.close()
|
|
|
|
finally:
|
|
|
|
unlink = self._unlink
|
|
|
|
if unlink is not None:
|
|
|
|
self._unlink = None
|
|
|
|
unlink()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
|
|
|
|
def SocketClient(address):
|
|
|
|
'''
|
|
|
|
Return a connection object connected to the socket given by `address`
|
|
|
|
'''
|
|
|
|
family = address_type(address)
|
2011-01-03 11:47:59 -04:00
|
|
|
with socket.socket( getattr(socket, family) ) as s:
|
2012-07-27 10:19:00 -03:00
|
|
|
s.setblocking(True)
|
2011-11-19 04:59:43 -04:00
|
|
|
s.connect(address)
|
2012-04-07 17:38:52 -03:00
|
|
|
return Connection(s.detach())
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Definitions for connections based on named pipes
|
|
|
|
#
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
|
|
|
|
class PipeListener(object):
|
|
|
|
'''
|
|
|
|
Representation of a named pipe
|
|
|
|
'''
|
|
|
|
def __init__(self, address, backlog=None):
|
|
|
|
self._address = address
|
2012-03-05 14:28:37 -04:00
|
|
|
self._handle_queue = [self._new_handle(first=True)]
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
self._last_accepted = None
|
2013-08-14 11:35:41 -03:00
|
|
|
util.sub_debug('listener created with address=%r', self._address)
|
|
|
|
self.close = util.Finalize(
|
2008-06-11 13:44:04 -03:00
|
|
|
self, PipeListener._finalize_pipe_listener,
|
|
|
|
args=(self._handle_queue, self._address), exitpriority=0
|
|
|
|
)
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def _new_handle(self, first=False):
|
2012-04-18 15:51:15 -03:00
|
|
|
flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED
|
2012-03-05 14:28:37 -04:00
|
|
|
if first:
|
2012-04-18 15:51:15 -03:00
|
|
|
flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
|
|
|
|
return _winapi.CreateNamedPipe(
|
2012-03-05 14:28:37 -04:00
|
|
|
self._address, flags,
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
|
|
|
|
_winapi.PIPE_WAIT,
|
|
|
|
_winapi.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
|
|
|
|
_winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
2012-03-05 14:28:37 -04:00
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
self._handle_queue.append(self._new_handle())
|
2008-06-11 13:44:04 -03:00
|
|
|
handle = self._handle_queue.pop(0)
|
|
|
|
try:
|
2012-05-05 15:45:37 -03:00
|
|
|
ov = _winapi.ConnectNamedPipe(handle, overlapped=True)
|
|
|
|
except OSError as e:
|
|
|
|
if e.winerror != _winapi.ERROR_NO_DATA:
|
|
|
|
raise
|
|
|
|
# ERROR_NO_DATA can occur if a client has already connected,
|
|
|
|
# written data and then disconnected -- see Issue 14725.
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
res = _winapi.WaitForMultipleObjects(
|
|
|
|
[ov.event], False, INFINITE)
|
|
|
|
except:
|
|
|
|
ov.cancel()
|
|
|
|
_winapi.CloseHandle(handle)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
_, err = ov.GetOverlappedResult(True)
|
|
|
|
assert err == 0
|
2011-05-09 12:04:27 -03:00
|
|
|
return PipeConnection(handle)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _finalize_pipe_listener(queue, address):
|
2013-08-14 11:35:41 -03:00
|
|
|
util.sub_debug('closing listener with address=%r', address)
|
2008-06-11 13:44:04 -03:00
|
|
|
for handle in queue:
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.CloseHandle(handle)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def PipeClient(address):
|
|
|
|
'''
|
|
|
|
Return a connection object connected to the pipe given by `address`
|
|
|
|
'''
|
2009-11-13 18:35:18 -04:00
|
|
|
t = _init_timeout()
|
2008-06-11 13:44:04 -03:00
|
|
|
while 1:
|
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.WaitNamedPipe(address, 1000)
|
|
|
|
h = _winapi.CreateFile(
|
|
|
|
address, _winapi.GENERIC_READ | _winapi.GENERIC_WRITE,
|
|
|
|
0, _winapi.NULL, _winapi.OPEN_EXISTING,
|
|
|
|
_winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
2012-12-19 08:33:35 -04:00
|
|
|
except OSError as e:
|
2012-04-18 15:51:15 -03:00
|
|
|
if e.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
|
|
|
|
_winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
|
2008-06-11 13:44:04 -03:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
_winapi.SetNamedPipeHandleState(
|
|
|
|
h, _winapi.PIPE_READMODE_MESSAGE, None, None
|
2008-06-11 13:44:04 -03:00
|
|
|
)
|
2011-05-09 12:04:27 -03:00
|
|
|
return PipeConnection(h)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Authentication stuff
|
|
|
|
#
|
|
|
|
|
|
|
|
MESSAGE_LENGTH = 20
|
|
|
|
|
2008-06-25 09:54:22 -03:00
|
|
|
CHALLENGE = b'#CHALLENGE#'
|
|
|
|
WELCOME = b'#WELCOME#'
|
|
|
|
FAILURE = b'#FAILURE#'
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def deliver_challenge(connection, authkey):
|
|
|
|
import hmac
|
|
|
|
assert isinstance(authkey, bytes)
|
|
|
|
message = os.urandom(MESSAGE_LENGTH)
|
|
|
|
connection.send_bytes(CHALLENGE + message)
|
2013-11-20 12:23:06 -04:00
|
|
|
digest = hmac.new(authkey, message, 'md5').digest()
|
2008-06-11 13:44:04 -03:00
|
|
|
response = connection.recv_bytes(256) # reject large message
|
|
|
|
if response == digest:
|
|
|
|
connection.send_bytes(WELCOME)
|
|
|
|
else:
|
|
|
|
connection.send_bytes(FAILURE)
|
|
|
|
raise AuthenticationError('digest received was wrong')
|
|
|
|
|
|
|
|
def answer_challenge(connection, authkey):
|
|
|
|
import hmac
|
|
|
|
assert isinstance(authkey, bytes)
|
|
|
|
message = connection.recv_bytes(256) # reject large message
|
|
|
|
assert message[:len(CHALLENGE)] == CHALLENGE, 'message = %r' % message
|
|
|
|
message = message[len(CHALLENGE):]
|
2013-11-20 12:23:06 -04:00
|
|
|
digest = hmac.new(authkey, message, 'md5').digest()
|
2008-06-11 13:44:04 -03:00
|
|
|
connection.send_bytes(digest)
|
|
|
|
response = connection.recv_bytes(256) # reject large message
|
|
|
|
if response != WELCOME:
|
|
|
|
raise AuthenticationError('digest sent was rejected')
|
|
|
|
|
|
|
|
#
|
|
|
|
# Support for using xmlrpclib for serialization
|
|
|
|
#
|
|
|
|
|
|
|
|
class ConnectionWrapper(object):
|
|
|
|
def __init__(self, conn, dumps, loads):
|
|
|
|
self._conn = conn
|
|
|
|
self._dumps = dumps
|
|
|
|
self._loads = loads
|
|
|
|
for attr in ('fileno', 'close', 'poll', 'recv_bytes', 'send_bytes'):
|
|
|
|
obj = getattr(conn, attr)
|
|
|
|
setattr(self, attr, obj)
|
|
|
|
def send(self, obj):
|
|
|
|
s = self._dumps(obj)
|
|
|
|
self._conn.send_bytes(s)
|
|
|
|
def recv(self):
|
|
|
|
s = self._conn.recv_bytes()
|
|
|
|
return self._loads(s)
|
|
|
|
|
|
|
|
def _xml_dumps(obj):
|
2011-02-25 11:42:01 -04:00
|
|
|
return xmlrpclib.dumps((obj,), None, None, None, 1).encode('utf-8')
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def _xml_loads(s):
|
2011-02-25 11:42:01 -04:00
|
|
|
(obj,), method = xmlrpclib.loads(s.decode('utf-8'))
|
2008-06-11 13:44:04 -03:00
|
|
|
return obj
|
|
|
|
|
|
|
|
class XmlListener(Listener):
|
|
|
|
def accept(self):
|
|
|
|
global xmlrpclib
|
|
|
|
import xmlrpc.client as xmlrpclib
|
|
|
|
obj = Listener.accept(self)
|
|
|
|
return ConnectionWrapper(obj, _xml_dumps, _xml_loads)
|
|
|
|
|
|
|
|
def XmlClient(*args, **kwds):
|
|
|
|
global xmlrpclib
|
|
|
|
import xmlrpc.client as xmlrpclib
|
|
|
|
return ConnectionWrapper(Client(*args, **kwds), _xml_dumps, _xml_loads)
|
2011-05-09 12:04:27 -03:00
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
#
|
|
|
|
# Wait
|
|
|
|
#
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
|
|
|
|
def _exhaustive_wait(handles, timeout):
|
|
|
|
# Return ALL handles which are currently signalled. (Only
|
|
|
|
# returning the first signalled might create starvation issues.)
|
|
|
|
L = list(handles)
|
|
|
|
ready = []
|
|
|
|
while L:
|
2012-04-18 15:51:15 -03:00
|
|
|
res = _winapi.WaitForMultipleObjects(L, False, timeout)
|
2012-03-05 14:28:37 -04:00
|
|
|
if res == WAIT_TIMEOUT:
|
|
|
|
break
|
|
|
|
elif WAIT_OBJECT_0 <= res < WAIT_OBJECT_0 + len(L):
|
|
|
|
res -= WAIT_OBJECT_0
|
|
|
|
elif WAIT_ABANDONED_0 <= res < WAIT_ABANDONED_0 + len(L):
|
|
|
|
res -= WAIT_ABANDONED_0
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Should not get here')
|
|
|
|
ready.append(L[res])
|
|
|
|
L = L[res+1:]
|
|
|
|
timeout = 0
|
|
|
|
return ready
|
|
|
|
|
2012-04-18 15:51:15 -03:00
|
|
|
_ready_errors = {_winapi.ERROR_BROKEN_PIPE, _winapi.ERROR_NETNAME_DELETED}
|
2012-03-05 14:28:37 -04:00
|
|
|
|
|
|
|
def wait(object_list, timeout=None):
|
|
|
|
'''
|
|
|
|
Wait till an object in object_list is ready/readable.
|
|
|
|
|
|
|
|
Returns list of those objects in object_list which are ready/readable.
|
|
|
|
'''
|
|
|
|
if timeout is None:
|
|
|
|
timeout = INFINITE
|
|
|
|
elif timeout < 0:
|
|
|
|
timeout = 0
|
|
|
|
else:
|
|
|
|
timeout = int(timeout * 1000 + 0.5)
|
|
|
|
|
|
|
|
object_list = list(object_list)
|
|
|
|
waithandle_to_obj = {}
|
|
|
|
ov_list = []
|
|
|
|
ready_objects = set()
|
|
|
|
ready_handles = set()
|
|
|
|
|
|
|
|
try:
|
|
|
|
for o in object_list:
|
|
|
|
try:
|
|
|
|
fileno = getattr(o, 'fileno')
|
|
|
|
except AttributeError:
|
|
|
|
waithandle_to_obj[o.__index__()] = o
|
|
|
|
else:
|
|
|
|
# start an overlapped read of length zero
|
|
|
|
try:
|
2012-04-18 15:51:15 -03:00
|
|
|
ov, err = _winapi.ReadFile(fileno(), 0, True)
|
2012-03-05 14:28:37 -04:00
|
|
|
except OSError as e:
|
2015-03-02 12:05:27 -04:00
|
|
|
ov, err = None, e.winerror
|
2012-03-05 14:28:37 -04:00
|
|
|
if err not in _ready_errors:
|
|
|
|
raise
|
2012-04-18 15:51:15 -03:00
|
|
|
if err == _winapi.ERROR_IO_PENDING:
|
2012-03-05 14:28:37 -04:00
|
|
|
ov_list.append(ov)
|
|
|
|
waithandle_to_obj[ov.event] = o
|
|
|
|
else:
|
|
|
|
# If o.fileno() is an overlapped pipe handle and
|
|
|
|
# err == 0 then there is a zero length message
|
2015-03-02 12:05:27 -04:00
|
|
|
# in the pipe, but it HAS NOT been consumed...
|
|
|
|
if ov and sys.getwindowsversion()[:2] >= (6, 2):
|
|
|
|
# ... except on Windows 8 and later, where
|
|
|
|
# the message HAS been consumed.
|
|
|
|
try:
|
|
|
|
_, err = ov.GetOverlappedResult(False)
|
|
|
|
except OSError as e:
|
|
|
|
err = e.winerror
|
|
|
|
if not err and hasattr(o, '_got_empty_message'):
|
|
|
|
o._got_empty_message = True
|
2012-03-05 14:28:37 -04:00
|
|
|
ready_objects.add(o)
|
|
|
|
timeout = 0
|
|
|
|
|
|
|
|
ready_handles = _exhaustive_wait(waithandle_to_obj.keys(), timeout)
|
|
|
|
finally:
|
|
|
|
# request that overlapped reads stop
|
|
|
|
for ov in ov_list:
|
|
|
|
ov.cancel()
|
|
|
|
|
|
|
|
# wait for all overlapped reads to stop
|
|
|
|
for ov in ov_list:
|
|
|
|
try:
|
|
|
|
_, err = ov.GetOverlappedResult(True)
|
|
|
|
except OSError as e:
|
|
|
|
err = e.winerror
|
|
|
|
if err not in _ready_errors:
|
|
|
|
raise
|
2012-04-18 15:51:15 -03:00
|
|
|
if err != _winapi.ERROR_OPERATION_ABORTED:
|
2012-03-05 14:28:37 -04:00
|
|
|
o = waithandle_to_obj[ov.event]
|
|
|
|
ready_objects.add(o)
|
|
|
|
if err == 0:
|
|
|
|
# If o.fileno() is an overlapped pipe handle then
|
|
|
|
# a zero length message HAS been consumed.
|
|
|
|
if hasattr(o, '_got_empty_message'):
|
|
|
|
o._got_empty_message = True
|
|
|
|
|
|
|
|
ready_objects.update(waithandle_to_obj[h] for h in ready_handles)
|
|
|
|
return [o for o in object_list if o in ready_objects]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2013-09-05 15:46:49 -03:00
|
|
|
import selectors
|
2013-01-13 21:24:05 -04:00
|
|
|
|
2013-09-08 06:30:53 -03:00
|
|
|
# poll/select have the advantage of not requiring any extra file
|
|
|
|
# descriptor, contrarily to epoll/kqueue (also, they require a single
|
|
|
|
# syscall).
|
|
|
|
if hasattr(selectors, 'PollSelector'):
|
|
|
|
_WaitSelector = selectors.PollSelector
|
|
|
|
else:
|
|
|
|
_WaitSelector = selectors.SelectSelector
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
def wait(object_list, timeout=None):
|
|
|
|
'''
|
|
|
|
Wait till an object in object_list is ready/readable.
|
|
|
|
|
|
|
|
Returns list of those objects in object_list which are ready/readable.
|
|
|
|
'''
|
2013-09-08 06:30:53 -03:00
|
|
|
with _WaitSelector() as selector:
|
2013-09-05 15:46:49 -03:00
|
|
|
for obj in object_list:
|
|
|
|
selector.register(obj, selectors.EVENT_READ)
|
|
|
|
|
2012-03-05 14:28:37 -04:00
|
|
|
if timeout is not None:
|
2013-09-05 15:46:49 -03:00
|
|
|
deadline = time.time() + timeout
|
|
|
|
|
|
|
|
while True:
|
|
|
|
ready = selector.select(timeout)
|
|
|
|
if ready:
|
|
|
|
return [key.fileobj for (key, events) in ready]
|
|
|
|
else:
|
|
|
|
if timeout is not None:
|
|
|
|
timeout = deadline - time.time()
|
|
|
|
if timeout < 0:
|
|
|
|
return ready
|
2012-04-24 17:56:57 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Make connection and socket objects sharable if possible
|
|
|
|
#
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
2013-08-14 11:35:41 -03:00
|
|
|
def reduce_connection(conn):
|
|
|
|
handle = conn.fileno()
|
|
|
|
with socket.fromfd(handle, socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
from . import resource_sharer
|
|
|
|
ds = resource_sharer.DupSocket(s)
|
|
|
|
return rebuild_connection, (ds, conn.readable, conn.writable)
|
|
|
|
def rebuild_connection(ds, readable, writable):
|
|
|
|
sock = ds.detach()
|
|
|
|
return Connection(sock.detach(), readable, writable)
|
|
|
|
reduction.register(Connection, reduce_connection)
|
|
|
|
|
|
|
|
def reduce_pipe_connection(conn):
|
|
|
|
access = ((_winapi.FILE_GENERIC_READ if conn.readable else 0) |
|
|
|
|
(_winapi.FILE_GENERIC_WRITE if conn.writable else 0))
|
|
|
|
dh = reduction.DupHandle(conn.fileno(), access)
|
|
|
|
return rebuild_pipe_connection, (dh, conn.readable, conn.writable)
|
|
|
|
def rebuild_pipe_connection(dh, readable, writable):
|
|
|
|
handle = dh.detach()
|
|
|
|
return PipeConnection(handle, readable, writable)
|
|
|
|
reduction.register(PipeConnection, reduce_pipe_connection)
|
|
|
|
|
2012-04-24 17:56:57 -03:00
|
|
|
else:
|
2013-08-14 11:35:41 -03:00
|
|
|
def reduce_connection(conn):
|
|
|
|
df = reduction.DupFd(conn.fileno())
|
|
|
|
return rebuild_connection, (df, conn.readable, conn.writable)
|
|
|
|
def rebuild_connection(df, readable, writable):
|
|
|
|
fd = df.detach()
|
|
|
|
return Connection(fd, readable, writable)
|
|
|
|
reduction.register(Connection, reduce_connection)
|