Skip to content

Commit 8cf4b59

Browse files
committed
socket: pass flags and address from send_fds
1 parent bc262de commit 8cf4b59

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Lib/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def send_fds(sock, buffers, fds, flags=0, address=None):
563563
import array
564564

565565
return sock.sendmsg(buffers, [(_socket.SOL_SOCKET,
566-
_socket.SCM_RIGHTS, array.array("i", fds))])
566+
_socket.SCM_RIGHTS, array.array("i", fds))], flags, address)
567567
__all__.append("send_fds")
568568

569569
if hasattr(_socket.socket, "recvmsg"):

Lib/test/test_socket.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7071,6 +7071,45 @@ def close_fds(fds):
70717071
data = os.read(rfd, 100)
70727072
self.assertEqual(data, str(index).encode())
70737073

7074+
def testSendAndRecvFdsByAddress(self):
7075+
def close_pipes(pipes):
7076+
for fd1, fd2 in pipes:
7077+
os.close(fd1)
7078+
os.close(fd2)
7079+
7080+
def close_fds(fds):
7081+
for fd in fds:
7082+
os.close(fd)
7083+
7084+
# send 10 file descriptors
7085+
pipes = [os.pipe() for _ in range(10)]
7086+
self.addCleanup(close_pipes, pipes)
7087+
fds = [rfd for rfd, wfd in pipes]
7088+
7089+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
7090+
address = socket_helper.create_unix_domain_name()
7091+
self.addCleanup(os_helper.unlink, address)
7092+
socket_helper.bind_unix_socket(sock, address)
7093+
7094+
socket.send_fds(sock, [MSG], fds, 0, address)
7095+
7096+
# request more data and file descriptors than expected
7097+
msg, fds2, flags, addr = socket.recv_fds(sock, len(MSG) * 2, len(fds) * 2)
7098+
self.addCleanup(close_fds, fds2)
7099+
7100+
self.assertEqual(msg, MSG)
7101+
self.assertEqual(len(fds2), len(fds))
7102+
self.assertEqual(flags, 0)
7103+
7104+
# test that file descriptors are connected
7105+
for index, fds in enumerate(pipes):
7106+
rfd, wfd = fds
7107+
os.write(wfd, str(index).encode())
7108+
7109+
for index, rfd in enumerate(fds2):
7110+
data = os.read(rfd, 100)
7111+
self.assertEqual(data, str(index).encode())
7112+
70747113

70757114
def setUpModule():
70767115
thread_info = threading_helper.threading_setup()

0 commit comments

Comments
 (0)