Skip to content

Commit 7368656

Browse files
committed
extract the logic into _recv_one_fd method
1 parent 02b0ddd commit 7368656

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

Lib/test/test_socket.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7339,6 +7339,16 @@ def _test_pipe(self, rfd, wfd, msg):
73397339
data = os.read(rfd, 512)
73407340
self.assertEqual(data, msg)
73417341

7342+
@staticmethod
7343+
def _recv_one_fd(sock, bufsize, flags=0):
7344+
if sys.platform.startswith("freebsd"):
7345+
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
7346+
# otherwise the access control message is truncated.
7347+
max_fds = 2
7348+
else:
7349+
max_fds = 1
7350+
return socket.recv_fds(sock, bufsize, max_fds, flags)
7351+
73427352
def testSendAndRecvFds(self):
73437353
# send 10 file descriptors
73447354
pipes = [os.pipe() for _ in range(10)]
@@ -7377,13 +7387,7 @@ def test_send_recv_fds_with_addrs(self):
73777387
sock2.setblocking(False)
73787388

73797389
socket.send_fds(sock1, [MSG], [rfd], address=sock2_addr)
7380-
if sys.platform.startswith("freebsd"):
7381-
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
7382-
# otherwise the access control message is truncated.
7383-
recv_fds_len = 2
7384-
else:
7385-
recv_fds_len = 1
7386-
msg, fds, flags, addr = socket.recv_fds(sock2, len(MSG), recv_fds_len)
7390+
msg, fds, flags, addr = self._recv_one_fd(sock2, len(MSG))
73877391
self._cleanup_fds(fds)
73887392

73897393
self.assertEqual(msg, MSG)
@@ -7448,13 +7452,7 @@ def test_send_fds_dontwait(self):
74487452
for _ in range(64 * 1024):
74497453
socket.send_fds(sock1, [MSG], [rfd], socket.MSG_DONTWAIT)
74507454

7451-
if sys.platform.startswith("freebsd"):
7452-
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
7453-
# otherwise the access control message is truncated.
7454-
recv_fds_len = 2
7455-
else:
7456-
recv_fds_len = 1
7457-
msg, fds, flags, addr = socket.recv_fds(sock2, len(MSG), recv_fds_len)
7455+
msg, fds, flags, addr = self._recv_one_fd(sock2, len(MSG))
74587456
self._cleanup_fds(fds)
74597457

74607458
self.assertEqual(msg, MSG)

0 commit comments

Comments
 (0)