From 0d28dddbf82f5b4603cae7d6bdb426ff64aad8b4 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 28 Sep 2023 09:47:19 +0300 Subject: [PATCH 1/5] gh-110012: Fix `RuntimeWarning` in `test_socket` --- Lib/test/test_socket.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 99c4c5cbc4902d..b9cf1e49a5cf89 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3,6 +3,7 @@ from test.support import os_helper from test.support import socket_helper from test.support import threading_helper +from test.support import warnings_helper import _thread as thread import array @@ -165,6 +166,15 @@ def socket_setdefaulttimeout(timeout): socket.setdefaulttimeout(old_timeout) +@contextlib.contextmanager +def catch_mailformed_data_warning(quite=False): + with warnings_helper.check_warnings( + ("received malformed or improperly-truncated ancillary data", RuntimeWarning), + quite=quite, + ): + yield + + HAVE_SOCKET_CAN = _have_socket_can() HAVE_SOCKET_CAN_ISOTP = _have_socket_can_isotp() @@ -3874,8 +3884,9 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): # mindata and maxdata bytes when received with buffer size # ancbuf, and that any complete file descriptor numbers are # valid. - msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, - len(MSG), ancbuf) + with catch_mailformed_data_warning(): + msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, + len(MSG), ancbuf) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) @@ -4217,8 +4228,9 @@ def testSingleCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() - msg, ancdata, flags, addr = self.doRecvmsg( - self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) + with catch_mailformed_data_warning(): + msg, ancdata, flags, addr = self.doRecvmsg( + self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) @@ -4321,9 +4333,10 @@ def testSecondCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() - msg, ancdata, flags, addr = self.doRecvmsg( - self.serv_sock, len(MSG), - socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) + with catch_mailformed_data_warning(): + msg, ancdata, flags, addr = self.doRecvmsg( + self.serv_sock, len(MSG), + socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) From da358cc5d2a78715bc21563ef4487fc336ad8ae9 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 28 Sep 2023 09:53:21 +0300 Subject: [PATCH 2/5] Typos! --- Lib/test/test_socket.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index b9cf1e49a5cf89..713d723ad9df0c 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -167,7 +167,7 @@ def socket_setdefaulttimeout(timeout): @contextlib.contextmanager -def catch_mailformed_data_warning(quite=False): +def catch_malformed_data_warning(quite=False): with warnings_helper.check_warnings( ("received malformed or improperly-truncated ancillary data", RuntimeWarning), quite=quite, @@ -3884,7 +3884,7 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): # mindata and maxdata bytes when received with buffer size # ancbuf, and that any complete file descriptor numbers are # valid. - with catch_mailformed_data_warning(): + with catch_malformed_data_warning(): msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbuf) self.assertEqual(msg, MSG) @@ -4228,7 +4228,7 @@ def testSingleCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() - with catch_mailformed_data_warning(): + with catch_malformed_data_warning(): msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) @@ -4333,7 +4333,7 @@ def testSecondCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() - with catch_mailformed_data_warning(): + with catch_malformed_data_warning(): msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) From cf0cfd052691703a2311d6242b6bbdea0f156935 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 28 Sep 2023 09:54:37 +0300 Subject: [PATCH 3/5] Typos! --- Lib/test/test_socket.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 713d723ad9df0c..0258b9dc726cbf 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -167,10 +167,10 @@ def socket_setdefaulttimeout(timeout): @contextlib.contextmanager -def catch_malformed_data_warning(quite=False): +def catch_malformed_data_warning(quiet=False): with warnings_helper.check_warnings( ("received malformed or improperly-truncated ancillary data", RuntimeWarning), - quite=quite, + quiet=quiet, ): yield From dfda2548a56ed73cb78ac076fcdf52f28e456afd Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 28 Sep 2023 10:20:20 +0300 Subject: [PATCH 4/5] Use `quiet=True` by default --- Lib/test/test_socket.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 0258b9dc726cbf..b7f05ec8793921 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -167,7 +167,8 @@ def socket_setdefaulttimeout(timeout): @contextlib.contextmanager -def catch_malformed_data_warning(quiet=False): +def catch_malformed_data_warning(quiet=True): + # This warning happens on macos and win, but does not always happen on linux. with warnings_helper.check_warnings( ("received malformed or improperly-truncated ancillary data", RuntimeWarning), quiet=quiet, From f45b3b89fdfc1e7c8fd24261efb1568bbfa54d47 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 07:19:05 +0000 Subject: [PATCH 5/5] Update test_socket.py --- Lib/test/test_socket.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 3b9cf28d71374d..d1c1da3be5ca20 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,7 +1,8 @@ import unittest from test import support from test.support import ( - is_apple, os_helper, refleak_helper, socket_helper, threading_helper + is_apple, os_helper, refleak_helper, socket_helper, threading_helper, + warnings_helper, ) import _thread as thread import array