From 7ef9d40b788047feeedbaf41a89afce71212a416 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Jun 2024 13:25:58 +0200 Subject: [PATCH 1/2] gh-120417: Use import_helper() in test_regrtest Skip tests if imported module is missing. --- Lib/test/test_regrtest.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 97ce797f0f6acb..9870be72e66e0a 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -22,7 +22,7 @@ import textwrap import unittest from test import support -from test.support import os_helper, without_optimizer +from test.support import os_helper, import_helper from test.libregrtest import cmdline from test.libregrtest import main from test.libregrtest import setup @@ -1178,7 +1178,7 @@ def test_run(self): stats=TestStats(4, 1), forever=True) - @without_optimizer + @support.without_optimizer def check_leak(self, code, what, *, run_workers=False): test = self.create_test('huntrleaks', code=code) @@ -1746,10 +1746,9 @@ def test_other_bug(self): @support.cpython_only def test_uncollectable(self): - try: - import _testcapi - except ImportError: - raise unittest.SkipTest("requires _testcapi") + # Skip test if _testcapi is missing + import_helper.import_module('_testcapi') + code = textwrap.dedent(r""" import _testcapi import gc @@ -2132,10 +2131,10 @@ def test_unload_tests(self): def check_add_python_opts(self, option): # --fast-ci and --slow-ci add "-u -W default -bb -E" options to Python - try: - import _testinternalcapi - except ImportError: - raise unittest.SkipTest("requires _testinternalcapi") + + # Skip test if _testinternalcapi is missing + import_helper.import_module('_testinternalcapi') + code = textwrap.dedent(r""" import sys import unittest @@ -2198,10 +2197,8 @@ def test_add_python_opts(self): @unittest.skipIf(support.is_android, 'raising SIGSEGV on Android is unreliable') def test_worker_output_on_failure(self): - try: - from faulthandler import _sigsegv - except ImportError: - self.skipTest("need faulthandler._sigsegv") + # Skip test if faulthandler is missing + import_helper.import_module('faulthandler') code = textwrap.dedent(r""" import faulthandler From d9cf3a15bc757e66198ea4fe719e689153988bba Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Jun 2024 15:45:45 +0200 Subject: [PATCH 2/2] Update Lib/test/test_regrtest.py Co-authored-by: Serhiy Storchaka --- Lib/test/test_regrtest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 9870be72e66e0a..0a15170a99e757 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -22,7 +22,8 @@ import textwrap import unittest from test import support -from test.support import os_helper, import_helper +from test.support import import_helper +from test.support import os_helper from test.libregrtest import cmdline from test.libregrtest import main from test.libregrtest import setup