8000 gh-127840: pass flags and address from send_fds by mbachry · Pull Request #127841 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127840: pass flags and address from send_fds #127841

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

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
socket: pass flags and address from send_fds
  • Loading branch information
mbachry committed Dec 11, 2024
commit 8cf4b5900536068637f879aa7bd2bab1e54329e8
2 changes: 1 addition & 1 deletion Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def send_fds(sock, buffers, fds, flags=0, address=None):
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"):
Expand Down
39 changes: 39 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7071,6 +7071,45 @@ def close_fds(fds):
data = os.read(rfd, 100)
self.assertEqual(data, str(index).encode())

def testSendAndRecvFdsByAddress(self):
def close_pipes(pipes):
for fd1, fd2 in pipes:
os.close(fd1)
os.close(fd2)

def close_fds(fds):
for fd in fds:
os.close(fd)

# send 10 file descriptors
pipes = [os.pipe() for _ in range(10)]
self.addCleanup(close_pipes, pipes)
fds = [rfd for rfd, wfd in pipes]

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], fds, 0, address)

# request more data and file descriptors than expected
msg, fds2, flags, addr = socket.recv_fds(sock, len(MSG) * 2, len(fds) * 2)
self.addCleanup(close_fds, fds2)

self.assertEqual(msg, MSG)
self.assertEqual(len(fds2), len(fds))
self.assertEqual(flags, 0)

# test that file descriptors are connected
for index, fds in enumerate(pipes):
rfd, wfd = fds
os.write(wfd, str(index).encode())

for index, rfd in enumerate(fds2):
data = os.read(rfd, 100)
self.assertEqual(data, str(index).encode())


def setUpModule():
thread_info = threading_helper.threading_setup()
Expand Down
Loading
0