mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Upgrade socket from 3.13.11
This commit is contained in:
1 parent
5079ee8fe3
commit
183cebe8a1
3 files changed
+533
-161
No files matched your search
Vendored
+72
-58
@@ -306,7 +306,8 @@ class socket(_socket.socket):
|
||||
"""makefile(...) -> an I/O stream connected to the socket
|
||||
|
||||
The arguments are as for io.open() after the filename, except the only
|
||||
supported mode values are 'r' (default), 'w' and 'b'.
|
||||
supported mode values are 'r' (default), 'w', 'b', or a combination of
|
||||
those.
|
||||
"""
|
||||
# XXX refactor to share code?
|
||||
if not set(mode) <= {"r", "w", "b"}:
|
||||
@@ -591,16 +592,65 @@ if hasattr(_socket.socket, "share"):
|
||||
return socket(0, 0, 0, info)
|
||||
__all__.append("fromshare")
|
||||
|
||||
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
|
||||
# This is used if _socket doesn't natively provide socketpair. It's
|
||||
# always defined so that it can be patched in for testing purposes.
|
||||
def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
|
||||
if family == AF_INET:
|
||||
host = _LOCALHOST
|
||||
elif family == AF_INET6:
|
||||
host = _LOCALHOST_V6
|
||||
else:
|
||||
raise ValueError("Only AF_INET and AF_INET6 socket address families "
|
||||
"are supported")
|
||||
if type != 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(family, type, proto)
|
||||
try:
|
||||
lsock.bind((host, 0))
|
||||
lsock.listen()
|
||||
# On IPv6, ignore flow_info and scope_id
|
||||
addr, port = lsock.getsockname()[:2]
|
||||
csock = socket(family, type, proto)
|
||||
try:
|
||||
csock.setblocking(False)
|
||||
try:
|
||||
csock.connect((addr, port))
|
||||
except (BlockingIOError, InterruptedError):
|
||||
pass
|
||||
csock.setblocking(True)
|
||||
ssock, _ = lsock.accept()
|
||||
except:
|
||||
csock.close()
|
||||
raise
|
||||
finally:
|
||||
lsock.close()
|
||||
|
||||
# Authenticating avoids using a connection from something else
|
||||
# able to connect to {host}:{port} instead of us.
|
||||
# We expect only AF_INET and AF_INET6 families.
|
||||
try:
|
||||
if (
|
||||
ssock.getsockname() != csock.getpeername()
|
||||
or csock.getsockname() != ssock.getpeername()
|
||||
):
|
||||
raise ConnectionError("Unexpected peer connection")
|
||||
except:
|
||||
# getsockname() and getpeername() can fail
|
||||
# if either socket isn't connected.
|
||||
ssock.close()
|
||||
csock.close()
|
||||
raise
|
||||
|
||||
return (ssock, csock)
|
||||
|
||||
if hasattr(_socket, "socketpair"):
|
||||
|
||||
def socketpair(family=None, type=SOCK_STREAM, proto=0):
|
||||
"""socketpair([family[, type[, proto]]]) -> (socket object, socket object)
|
||||
|
||||
Create a pair of socket objects from the sockets returned by the platform
|
||||
socketpair() function.
|
||||
The arguments are the same as for socket() except the default family is
|
||||
AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
|
||||
"""
|
||||
if family is None:
|
||||
try:
|
||||
family = AF_UNIX
|
||||
@@ -612,44 +662,7 @@ if hasattr(_socket, "socketpair"):
|
||||
return a, b
|
||||
|
||||
else:
|
||||
|
||||
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
|
||||
def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
|
||||
if family == AF_INET:
|
||||
host = _LOCALHOST
|
||||
elif family == AF_INET6:
|
||||
host = _LOCALHOST_V6
|
||||
else:
|
||||
raise ValueError("Only AF_INET and AF_INET6 socket address families "
|
||||
"are supported")
|
||||
if type != 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(family, type, proto)
|
||||
try:
|
||||
lsock.bind((host, 0))
|
||||
lsock.listen()
|
||||
# On IPv6, ignore flow_info and scope_id
|
||||
addr, port = lsock.getsockname()[:2]
|
||||
csock = socket(family, type, proto)
|
||||
try:
|
||||
csock.setblocking(False)
|
||||
try:
|
||||
csock.connect((addr, port))
|
||||
except (BlockingIOError, InterruptedError):
|
||||
pass
|
||||
csock.setblocking(True)
|
||||
ssock, _ = lsock.accept()
|
||||
except:
|
||||
csock.close()
|
||||
raise
|
||||
finally:
|
||||
lsock.close()
|
||||
return (ssock, csock)
|
||||
socketpair = _fallback_socketpair
|
||||
__all__.append("socketpair")
|
||||
|
||||
socketpair.__doc__ = """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
|
||||
@@ -702,16 +715,15 @@ class SocketIO(io.RawIOBase):
|
||||
self._checkReadable()
|
||||
if self._timeout_occurred:
|
||||
raise OSError("cannot read from timed out object")
|
||||
while True:
|
||||
try:
|
||||
return self._sock.recv_into(b)
|
||||
except timeout:
|
||||
self._timeout_occurred = True
|
||||
raise
|
||||
except error as e:
|
||||
if e.errno in _blocking_errnos:
|
||||
return None
|
||||
raise
|
||||
try:
|
||||
return self._sock.recv_into(b)
|
||||
except timeout:
|
||||
self._timeout_occurred = True
|
||||
raise
|
||||
except error as e:
|
||||
if e.errno in _blocking_errnos:
|
||||
return None
|
||||
raise
|
||||
|
||||
def write(self, b):
|
||||
"""Write the given bytes or bytearray object *b* to the socket
|
||||
@@ -919,7 +931,9 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
|
||||
# Fail later on bind(), for platforms which may not
|
||||
# support this option.
|
||||
pass
|
||||
if reuse_port:
|
||||
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
|
||||
# on other address families than AF_INET/AF_INET6.
|
||||
if reuse_port and family in (AF_INET, AF_INET6):
|
||||
sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
|
||||
if has_ipv6 and family == AF_INET6:
|
||||
if dualstack_ipv6:
|
||||
|
||||
Vendored
+460
-102
File diff suppressed because it is too large.
Load diff
@@ -2804,7 +2804,7 @@ mod _socket {
|
||||
let encoded =
|
||||
vm.state
|
||||
.codec_registry
|
||||
.encode_text(s.to_owned().into(), "idna", None, vm)?;
|
||||
.encode_text(s.to_owned(), "idna", None, vm)?;
|
||||
let host_str = std::str::from_utf8(encoded.as_bytes())
|
||||
.map_err(|_| vm.new_runtime_error("idna output is not utf8".to_owned()))?;
|
||||
Some(host_str.to_owned())
|
||||
|
||||
Reference in new issue
Block a user