]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127840: pass flags and address from send_fds (GH-127841)
authorMarcin Bachry <hegel666@gmail.com>
Thu, 22 May 2025 02:38:01 +0000 (04:38 +0200)
committerGitHub <noreply@github.com>
Thu, 22 May 2025 02:38:01 +0000 (19:38 -0700)
socket: pass flags and address from send_fds

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/socket.py
Lib/test/test_socket.py
Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst [new file with mode: 0644]

index 727b0e75f0359553b37cc0641cb10b2da8932842..2cbaf221a59deaea98ca4665078df193656c70bb 100644 (file)
@@ -563,7 +563,7 @@ if hasattr(_socket.socket, "sendmsg"):
         import array
 
         return sock.sendmsg(buffers, [(_socket.SOL_SOCKET,
-            _socket.SCM_RIGHTS, array.array("i", fds))])
+            _socket.SCM_RIGHTS, array.array("i", fds))], flags, address)
     __all__.append("send_fds")
 
 if hasattr(_socket.socket, "recvmsg"):
index 03c54151a2218ff636114af46f2c14579a7b69d0..04dfb682ec683106f346225a58b6cd46e34204f1 100644 (file)
@@ -7366,6 +7366,30 @@ class SendRecvFdsTests(unittest.TestCase):
             data = os.read(rfd, 100)
             self.assertEqual(data,  str(index).encode())
 
+    def testSendAndRecvFdsByAddress(self):
+        rfd, wfd = os.pipe()
+        self.addCleanup(os.close, rfd)
+        self.addCleanup(os.close, wfd)
+
+        sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+        address = socket_helper.create_unix_domain_name()
+        self.addCleanup(os_helper.unlink, address)
+        socket_helper.bind_unix_socket(sock, address)
+
+        socket.send_fds(sock, [MSG], [rfd], 0, address)
+
+        # request more data and file descriptors than expected
+        msg, (rfd2,), flags, addr = socket.recv_fds(sock, len(MSG) * 2, 2)
+        self.addCleanup(os.close, rfd2)
+        self.assertEqual(msg, MSG)
+        self.assertEqual(flags, 0)
+        self.assertEqual(addr, address)
+
+        # test that the file descriptor is connected
+        os.write(wfd, b'data')
+        data = os.read(rfd2, 100)
+        self.assertEqual(data,  b'data')
+
 
 class FreeThreadingTests(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst b/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst
new file mode 100644 (file)
index 0000000..51eaaa9
--- /dev/null
@@ -0,0 +1 @@
+Fix :func:`socket.send_fds` ignoring flags and address parameters.