-
Notifications
You must be signed in to change notification settings - Fork 840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not monkey-patch socket, ensure socketpair returns non-blocking sockets #918
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,51 +167,35 @@ def get_linux_version(release_str): | |
_LOCALHOST = '127.0.0.1' | ||
_LOCALHOST_V6 = '::1' | ||
|
||
if not hasattr(socket, 'socketpair'): | ||
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain. | ||
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): | ||
if family == socket.AF_INET: | ||
host = _LOCALHOST | ||
elif family == socket.AF_INET6: | ||
host = _LOCALHOST_V6 | ||
else: | ||
raise ValueError( | ||
'Only AF_INET and AF_INET6 socket address families ' | ||
'are supported') | ||
if type != socket.SOCK_STREAM: | ||
raise ValueError('Only SOCK_STREAM socket type is supported') | ||
if proto != 0: | ||
raise ValueError('Only protocol zero is supported') | ||
|
||
# We create a connected TCP socket. Note the trick with | ||
# setblocking(False) that prevents us from having to create a thread. | ||
lsock = socket.socket(family, type, proto) | ||
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): | ||
if family == socket.AF_INET: | ||
host = _LOCALHOST | ||
elif family == socket.AF_INET6: | ||
host = _LOCALHOST_V6 | ||
else: | ||
raise ValueError( | ||
'Only AF_INET and AF_INET6 socket address families ' | ||
'are supported') | ||
if type != socket.SOCK_STREAM: | ||
raise ValueError('Only SOCK_STREAM socket type is supported') | ||
if proto != 0: | ||
raise ValueError('Only protocol zero is supported') | ||
|
||
lsock = socket.socket(family, type, proto) | ||
try: | ||
lsock.bind((host, 0)) | ||
lsock.listen(min(socket.SOMAXCONN, 128)) | ||
# On IPv6, ignore flow_info and scope_id | ||
addr, port = lsock.getsockname()[:2] | ||
csock = socket.socket(family, type, proto) | ||
try: | ||
lsock.bind((host, 0)) | ||
lsock.listen(min(socket.SOMAXCONN, 128)) | ||
# On IPv6, ignore flow_info and scope_id | ||
addr, port = lsock.getsockname()[:2] | ||
csock = socket.socket(family, type, proto) | ||
try: | ||
csock.setblocking(False) | ||
if _sys.version_info >= (3, 0): | ||
try: | ||
csock.connect((addr, port)) | ||
except (BlockingIOError, InterruptedError): | ||
pass | ||
else: | ||
try: | ||
csock.connect((addr, port)) | ||
except SOCKET_ERROR as e: | ||
if e.errno != errno.EWOULDBLOCK: | ||
raise | ||
csock.setblocking(True) | ||
ssock, _ = lsock.accept() | ||
except Exception: | ||
csock.close() | ||
raise | ||
finally: | ||
lsock.close() | ||
return (ssock, csock) | ||
|
||
socket.socketpair = socketpair | ||
csock.connect((addr, port)) | ||
ssock, _ = lsock.accept() | ||
except Exception: | ||
csock.close() | ||
raise | ||
finally: | ||
lsock.close() | ||
csock.setblocking(False) | ||
ssock.setblocking(False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My 2 cents: this function's name is exactly like the real deal in |
||
return (ssock, csock) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a function comment that explains the reasons for having this method - Windows doesn't provide
socket.socketpair
.