From 0d28dddbf82f5b4603cae7d6bdb426ff64aad8b4 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 28 Sep 2023 09:47:19 +0300 Subject: [PATCH 01/14] 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 02/14] 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 03/14] 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 04/14] 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 ec6a368c717fe9649fef0a8d4a0091c5eb5a6802 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 06:50:34 +0000 Subject: [PATCH 05/14] fix warnings in test_importlib.test_abc --- Lib/test/test_importlib/test_abc.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py index 00af2dd712425a..92a77e079e57e7 100644 --- a/Lib/test/test_importlib/test_abc.py +++ b/Lib/test/test_importlib/test_abc.py @@ -226,7 +226,15 @@ class ResourceLoaderDefaultsTests(ABCTestHarness): SPLIT = make_abc_subclasses(ResourceLoader) def test_get_data(self): - with self.assertRaises(IOError): + with ( + self.assertRaises(IOError), + self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.abc\.ResourceLoader is deprecated in favour of " + r"supporting resource loading through importlib\.resources" + r"\.abc\.TraversableResources.", + ), + ): self.ins.get_data('/some/path') @@ -927,9 +935,19 @@ def get_filename(self, fullname): def path_stats(self, path): return {'mtime': 1} - - loader = DummySourceLoader() - with self.assertWarns(DeprecationWarning): + with self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.abc\.ResourceLoader is deprecated in favour of " + r"supporting resource loading through importlib\.resources" + r"\.abc\.TraversableResources.", + ): + loader = DummySourceLoader() + + with self.assertWarnsRegex( + DeprecationWarning, + r"SourceLoader\.path_mtime is deprecated in favour of " + r"SourceLoader\.path_stats\(\)\." + ): loader.path_mtime('foo.py') From a5c2b7a9553596a50b8d2f2839242c7cac612a96 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 06:55:00 +0000 Subject: [PATCH 06/14] fix warnings found in test_importlib.test_windows --- Lib/test/test_importlib/test_windows.py | 31 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py index f32680bdbeb9e3..bef4fb46f859a4 100644 --- a/Lib/test/test_importlib/test_windows.py +++ b/Lib/test/test_importlib/test_windows.py @@ -91,23 +91,46 @@ class WindowsRegistryFinderTests: test_module = "spamham{}".format(os.getpid()) def test_find_spec_missing(self): - spec = self.machinery.WindowsRegistryFinder.find_spec('spam') + with self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.machinery\.WindowsRegistryFinder is deprecated; " + r"use site configuration instead\. Future versions of Python may " + r"not enable this finder by default\." + ): + spec = self.machinery.WindowsRegistryFinder.find_spec('spam') self.assertIsNone(spec) def test_module_found(self): with setup_module(self.machinery, self.test_module): - spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) + with self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.machinery\.WindowsRegistryFinder is deprecated; " + r"use site configuration instead\. Future versions of Python may " + r"not enable this finder by default\." + ): + spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) self.assertIsNotNone(spec) def test_module_not_found(self): with setup_module(self.machinery, self.test_module, path="."): - spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) + with self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.machinery\.WindowsRegistryFinder is deprecated; " + r"use site configuration instead\. Future versions of Python may " + r"not enable this finder by default\." + ): + spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module) self.assertIsNone(spec) def test_raises_deprecation_warning(self): # WindowsRegistryFinder is not meant to be instantiated, so the # deprecation warning is raised in the 'find_spec' method instead. - with self.assertWarns(DeprecationWarning): + with self.assertWarnsRegex( + DeprecationWarning, + r"importlib\.machinery\.WindowsRegistryFinder is deprecated; " + r"use site configuration instead\. Future versions of Python may " + r"not enable this finder by default\." + ): self.machinery.WindowsRegistryFinder.find_spec('spam') (Frozen_WindowsRegistryFinderTests, From 0f2afcdf4db078f97c2d1014890812df4b23f0f9 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 11 Jan 2025 09:48:56 +0000 Subject: [PATCH 07/14] set warnings as errors everywhere --- Lib/test/libregrtest/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index dcbcc6790c68d8..cd4d512771e05f 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -642,9 +642,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ): if not sys.stdout.write_through: python_opts.append('-u') - # Add warnings filter 'default' + # Add warnings filter 'error' if 'default' not in sys.warnoptions: - python_opts.extend(('-W', 'default')) + python_opts.extend(('-W', 'error')) # Error on bytes/str comparison if sys.flags.bytes_warning < 2: From f45b3b89fdfc1e7c8fd24261efb1568bbfa54d47 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 07:19:05 +0000 Subject: [PATCH 08/14] 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 From ca5eb671d8d5075c659033310b075063f0e51b30 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 07:31:02 +0000 Subject: [PATCH 09/14] fix ResourceWarning in test_pyrepl --- Lib/test/test_pyrepl/test_pyrepl.py | 31 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index f29a7ffbd7cafd..113ea1d2fe709f 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1324,22 +1324,21 @@ def test_readline_history_file(self): if readline.backend != "editline": self.skipTest("GNU readline is not affected by this issue") - hfile = tempfile.NamedTemporaryFile() - self.addCleanup(unlink, hfile.name) - env = os.environ.copy() - env["PYTHON_HISTORY"] = hfile.name - - env["PYTHON_BASIC_REPL"] = "1" - output, exit_code = self.run_repl("spam \nexit()\n", env=env) - self.assertEqual(exit_code, 0) - self.assertIn("spam ", output) - self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) - self.assertIn("spam\\040", pathlib.Path(hfile.name).read_text()) - - env.pop("PYTHON_BASIC_REPL", None) - output, exit_code = self.run_repl("exit\n", env=env) - self.assertEqual(exit_code, 0) - self.assertNotIn("\\040", pathlib.Path(hfile.name).read_text()) + with tempfile.NamedTemporaryFile() as hfile: + env = os.environ.copy() + env["PYTHON_HISTORY"] = hfile.name + + env["PYTHON_BASIC_REPL"] = "1" + output, exit_code = self.run_repl("spam \nexit()\n", env=env) + self.assertEqual(exit_code, 0) + self.assertIn("spam ", output) + self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) + self.assertIn("spam\\040", pathlib.Path(hfile.name).read_text()) + + env.pop("PYTHON_BASIC_REPL", None) + output, exit_code = self.run_repl("exit\n", env=env) + self.assertEqual(exit_code, 0) + self.assertNotIn("\\040", pathlib.Path(hfile.name).read_text()) def test_keyboard_interrupt_after_isearch(self): output, exit_code = self.run_repl(["\x12", "\x03", "exit"]) From 7b2b43717214f13a334bbc9d4108fb2791c5d2d7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 10:30:56 +0000 Subject: [PATCH 10/14] downgrade rather than ignore malformed socket warning --- Lib/test/test_socket.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index d1c1da3be5ca20..84728e8df48cd7 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -2,7 +2,6 @@ from test import support from test.support import ( is_apple, os_helper, refleak_helper, socket_helper, threading_helper, - warnings_helper, ) import _thread as thread import array @@ -200,12 +199,20 @@ def socket_setdefaulttimeout(timeout): @contextlib.contextmanager -def catch_malformed_data_warning(quiet=True): +def downgrade_malformed_data_warning(): # 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, - ): + if sys.platform not in {"win32", "darwin"}: + yield + return + + with warnings.catch_warnings(): + # TODO: gh-110012, we should investigate why this warning is happening + # and fix it properly. + warnings.simplefilter( + "always:" + r"received malformed or improperly-truncated ancillary data" + ":RuntimeWarning" + ) yield @@ -3957,7 +3964,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_malformed_data_warning(): + with downgrade_malformed_data_warning(): # TODO: gh-110012 msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), ancbuf) self.assertEqual(msg, MSG) @@ -4310,7 +4317,7 @@ def testSingleCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVHOPLIMIT, 1) self.misc_event.set() - with catch_malformed_data_warning(): + with downgrade_malformed_data_warning(): # TODO: gh-110012 msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) @@ -4415,7 +4422,7 @@ def testSecondCmsgTruncInData(self): self.serv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_RECVTCLASS, 1) self.misc_event.set() - with catch_malformed_data_warning(): + with downgrade_malformed_data_warning(): # TODO: gh-110012 msg, ancdata, flags, addr = self.doRecvmsg( self.serv_sock, len(MSG), socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) From 32cd095faf1847846fbaead53c3608765f9fffc9 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 12:41:44 +0000 Subject: [PATCH 11/14] Update Lib/test/test_socket.py --- Lib/test/test_socket.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 84728e8df48cd7..b4b9a420f9c335 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,4 +1,5 @@ import unittest +import warnings from test import support from test.support import ( is_apple, os_helper, refleak_helper, socket_helper, threading_helper, From db0cb51533f90468722155ffa9703b66153f16aa Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 14:57:15 +0000 Subject: [PATCH 12/14] Update Lib/test/test_socket.py --- Lib/test/test_socket.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index b4b9a420f9c335..aa7bef5426552c 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,5 +1,6 @@ import unittest import warnings +import warnings from test import support from test.support import ( is_apple, os_helper, refleak_helper, socket_helper, threading_helper, From 61eb61f419cb863c570e28d8963491a70fe6247b Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 14:58:56 +0000 Subject: [PATCH 13/14] Update Lib/test/test_socket.py --- Lib/test/test_socket.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index aa7bef5426552c..b4b9a420f9c335 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,6 +1,5 @@ import unittest import warnings -import warnings from test import support from test.support import ( is_apple, os_helper, refleak_helper, socket_helper, threading_helper, From 91ee5ee998d265c4dd0b6d230fbd0fab3c5e9a0b Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 16 Jan 2025 15:14:38 +0000 Subject: [PATCH 14/14] oops, it's not pytest --- 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 b4b9a420f9c335..7233847f37bf39 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -209,10 +209,10 @@ def downgrade_malformed_data_warning(): with warnings.catch_warnings(): # TODO: gh-110012, we should investigate why this warning is happening # and fix it properly. - warnings.simplefilter( - "always:" - r"received malformed or improperly-truncated ancillary data" - ":RuntimeWarning" + warnings.filterwarnings( + action="always", + message=r"received malformed or improperly-truncated ancillary data", + category=RuntimeWarning, ) yield