From 6a0d82f7584954fdd737fdfce47342f6fa429c61 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 18 Apr 2025 17:37:30 +0100 Subject: [PATCH 1/5] gh-126483: disable warnings filters mutation in concurrent test_check_hostname_idn use by test_ssl_in_multiple_threads --- Lib/test/test_ssl.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 1aff8e22683af0..81167d7ca188cc 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -1,5 +1,5 @@ # Test the support for SSL and sockets - +o import sys import unittest import unittest.mock @@ -31,6 +31,7 @@ import platform import sysconfig import functools +from contextlib import nullcontext try: import ctypes except ImportError: @@ -2843,6 +2844,7 @@ def test_ssl_in_multiple_threads(self): # See GH-124984: OpenSSL is not thread safe. threads = [] + warnings_filters = sys.flags.context_aware_warnings global USE_SAME_TEST_CONTEXT USE_SAME_TEST_CONTEXT = True try: @@ -2851,7 +2853,10 @@ def test_ssl_in_multiple_threads(self): self.test_alpn_protocols, self.test_getpeercert, self.test_crl_check, - self.test_check_hostname_idn, + partial( + self.test_check_hostname_idn, + warnings_filters=warnings_filters, + ), self.test_wrong_cert_tls12, self.test_wrong_cert_tls13, ): @@ -3097,7 +3102,7 @@ def test_dual_rsa_ecc(self): cipher = s.cipher()[0].split('-') self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) - def test_check_hostname_idn(self): + def test_check_hostname_idn(self, warnings_filters=True): if support.verbose: sys.stdout.write("\n") @@ -3152,16 +3157,26 @@ def test_check_hostname_idn(self): server_hostname="python.example.org") as s: with self.assertRaises(ssl.CertificateError): s.connect((HOST, server.port)) - with ThreadedEchoServer(context=server_context, chatty=True) as server: - with warnings_helper.check_no_resource_warning(self): - with self.assertRaises(UnicodeError): - context.wrap_socket(socket.socket(), - server_hostname='.pythontest.net') - with ThreadedEchoServer(context=server_context, chatty=True) as server: - with warnings_helper.check_no_resource_warning(self): - with self.assertRaises(UnicodeDecodeError): - context.wrap_socket(socket.socket(), - server_hostname=b'k\xf6nig.idn.pythontest.net') + with ( + ThreadedEchoServer(context=server_context, chatty=True) as server, + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext(), + self.assertRaises(UnicodeError), + ): + context.wrap_socket(socket.socket(), server_hostname='.pythontest.net') + + with ( + ThreadedEchoServer(context=server_context, chatty=True) as server, + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext(), + self.assertRaises(UnicodeDecodeError), + ): + context.wrap_socket( + socket.socket(), + server_hostname=b'k\xf6nig.idn.pythontest.net', + ) def test_wrong_cert_tls12(self): """Connecting when the server rejects the client's certificate From 5b109a62365fb018068cb0320b104b0d049a4617 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 18 Apr 2025 17:39:05 +0100 Subject: [PATCH 2/5] Update Lib/test/test_ssl.py --- Lib/test/test_ssl.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 81167d7ca188cc..2b69b9a29f4e0d 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -1,5 +1,4 @@ # Test the support for SSL and sockets -o import sys import unittest import unittest.mock From 535baad3ad31b4a78baeafd7bfd7c27eabbc643b Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 18 Apr 2025 17:45:05 +0100 Subject: [PATCH 3/5] Update Lib/test/test_ssl.py --- Lib/test/test_ssl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 2b69b9a29f4e0d..0d75433aecb89c 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -2852,7 +2852,7 @@ def test_ssl_in_multiple_threads(self): self.test_alpn_protocols, self.test_getpeercert, self.test_crl_check, - partial( + functools.partial( self.test_check_hostname_idn, warnings_filters=warnings_filters, ), From 148e41dce33ad8c8c7cdebb417db8eaff5df41a1 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 19 Apr 2025 08:05:42 +0100 Subject: [PATCH 4/5] Update Lib/test/test_ssl.py --- Lib/test/test_ssl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 0d75433aecb89c..9c9054ab066c09 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -1,4 +1,5 @@ # Test the support for SSL and sockets + import sys import unittest import unittest.mock From aa8c74a6eedbc695d481d1b0f4b496ddf4d96c43 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 30 Apr 2025 07:57:31 +0100 Subject: [PATCH 5/5] use parenthesis to highlight ternary --- Lib/test/test_ssl.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 779dcfed7293a4..169c976ca940dd 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -3159,18 +3159,22 @@ def test_check_hostname_idn(self, warnings_filters=True): s.connect((HOST, server.port)) with ( ThreadedEchoServer(context=server_context, chatty=True) as server, - warnings_helper.check_no_resource_warning(self) - if warnings_filters - else nullcontext(), + ( + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext() + ), self.assertRaises(UnicodeError), ): context.wrap_socket(socket.socket(), server_hostname='.pythontest.net') with ( ThreadedEchoServer(context=server_context, chatty=True) as server, - warnings_helper.check_no_resource_warning(self) - if warnings_filters - else nullcontext(), + ( + warnings_helper.check_no_resource_warning(self) + if warnings_filters + else nullcontext() + ), self.assertRaises(UnicodeDecodeError), ): context.wrap_socket(