From e36cd994e877a740faf17c8026fb34d790087bae Mon Sep 17 00:00:00 2001 From: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 13 May 2026 13:52:56 +0300 Subject: [PATCH] Update `socket.py` to 3.14.5 (#7866) * Update `socket.py` to 3.14.5 * Don't have socket.sethostname on android * Mark failing test --- Lib/socket.py | 28 ++++++++++++++++------------ Lib/test/test_socket.py | 34 +++++++++++++++++++++++++++++++--- crates/stdlib/src/socket.rs | 2 +- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Lib/socket.py b/Lib/socket.py index 727b0e75f..bbb476f2f 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -640,18 +640,22 @@ def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0): # 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 + # + # Note that we skip this on WASI because on that platorm the client socket + # may not have finished connecting by the time we've reached this point (gh-146139). + if sys.platform != "wasi": + 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) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index fe1fc94b6..ccc06ceba 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -561,8 +561,8 @@ class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest): @unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL') @unittest.skipUnless(HAVE_SOCKET_VSOCK, 'VSOCK sockets required for this test.') -@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST - "This test can only be run on a virtual guest.") +@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2), + "This test can only be run on a virtual guest.") class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): @@ -572,7 +572,16 @@ class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest): def setUp(self): self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) self.addCleanup(self.serv.close) - self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT)) + cid = get_cid() + if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY): + cid = socket.VMADDR_CID_LOCAL + try: + self.serv.bind((cid, VSOCKPORT)) + except OSError as exc: + if exc.errno == errno.EADDRNOTAVAIL: + self.skipTest(f"bind() failed with {exc!r}") + else: + raise self.serv.listen() self.serverExplicitReady() self.serv.settimeout(support.LOOPBACK_TIMEOUT) @@ -2180,6 +2189,25 @@ class GeneralModuleTests(unittest.TestCase): source=_socket) enum._test_simple_enum(CheckedAddressInfo, socket.AddressInfo) + @unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'Mut' found + @unittest.skipUnless(hasattr(socket.socket, "sendmsg"),"sendmsg not supported") + def test_sendmsg_reentrant_ancillary_mutation(self): + + class Mut: + def __index__(self): + seq.clear() + return socket.SCM_RIGHTS + + seq = [ + (socket.SOL_SOCKET, Mut(), b'xxxx'), + (socket.SOL_SOCKET, socket.SCM_RIGHTS, b'xxxx'), + ] + + left, right = socket.socketpair() + self.addCleanup(left.close) + self.addCleanup(right.close) + self.assertRaises(OSError, left.sendmsg, [b'x'], seq) + @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase): diff --git a/crates/stdlib/src/socket.rs b/crates/stdlib/src/socket.rs index b19ae7e0e..910512f74 100644 --- a/crates/stdlib/src/socket.rs +++ b/crates/stdlib/src/socket.rs @@ -2652,7 +2652,7 @@ mod _socket { .map_err(|err| vm.new_os_error(err.into_string().unwrap())) } - #[cfg(all(unix, not(target_os = "redox")))] + #[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))] #[pyfunction] fn sethostname(hostname: PyUtf8StrRef) -> nix::Result<()> { nix::unistd::sethostname(hostname.as_str())