diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 3eff9e753b6d84..7cc301827dbe1c 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -386,10 +386,11 @@ def get_build_info(): # --with-lto optimizations = [] - if '-flto=thin' in ldflags_nodist: - optimizations.append('ThinLTO') - elif '-flto' in ldflags_nodist: - optimizations.append('LTO') + if support.check_ldflags_lto(): + if '-flto=thin' in ldflags_nodist: + optimizations.append('ThinLTO') + else: + optimizations.append('LTO') if support.check_cflags_pgo(): # PGO (--enable-optimizations) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 37a69fda8ef74c..6b0673eff2d714 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -852,7 +852,7 @@ def python_is_optimized(): def check_cflags_pgo(): # Check if Python was built with ./configure --enable-optimizations: # with Profile Guided Optimization (PGO). - cflags_nodist = sysconfig.get_config_var('PY_CFLAGS_NODIST') or '' + cflags_nodist = (sysconfig.get_config_var('PY_CFLAGS_NODIST') or '') pgo_options = [ # GCC '-fprofile-use', @@ -867,13 +867,24 @@ def check_cflags_pgo(): return any(option in cflags_nodist for option in pgo_options) +def check_ldflags_lto(): + # Check if Python was built with ./configure --with-lto: + # with Link Time Optimization (PGO). + ldflags_nodist = (sysconfig.get_config_var('PY_LDFLAGS_NODIST') or '') + lto_options = { + '-flto', + '-flto=thin', + } + return any(option in ldflags_nodist for option in lto_options) + + def check_bolt_optimized(): # Always return false, if the platform is WASI, # because BOLT optimization does not support WASM binary. if is_wasi: return False - config_args = sysconfig.get_config_var('CONFIG_ARGS') or '' - return '--enable-bolt' in config_args + config_args = (sysconfig.get_config_var('CONFIG_ARGS') or '') + return ('--enable-bolt' in config_args) Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) diff --git a/Lib/test/test_gdb/__init__.py b/Lib/test/test_gdb/__init__.py index 0dd721780233e6..95c305eacb3181 100644 --- a/Lib/test/test_gdb/__init__.py +++ b/Lib/test/test_gdb/__init__.py @@ -24,6 +24,9 @@ if support.check_cflags_pgo(): raise unittest.SkipTest("test_gdb is not reliable on PGO builds") +if support.check_ldflags_lto(): + raise unittest.SkipTest("test_gdb is not reliable on LTO builds") + if support.check_bolt_optimized(): raise unittest.SkipTest("test_gdb is not reliable on BOLT optimized builds") diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index d900db546ada8d..d6d8f0485d805b 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -744,6 +744,11 @@ def test_get_signal_name(self): self.assertEqual(support.get_signal_name(exitcode), expected, exitcode) + def test_compiler_flags(self): + self.assertIsInstance(support.check_cflags_pgo(), bool) + self.assertIsInstance(support.check_ldflags_lto(), bool) + self.assertIsInstance(support.check_bolt_optimized(), bool) + # XXX -follows a list of untested API # make_legacy_pyc # is_resource_enabled diff --git a/Misc/NEWS.d/next/Tests/2025-03-12-12-10-18.gh-issue-117174.J0vKxH.rst b/Misc/NEWS.d/next/Tests/2025-03-12-12-10-18.gh-issue-117174.J0vKxH.rst new file mode 100644 index 00000000000000..ec0d48df4d651a --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2025-03-12-12-10-18.gh-issue-117174.J0vKxH.rst @@ -0,0 +1,2 @@ +Skip test_gdb if Python is built with Link Time Optimization (LTO). Patch by +Victor Stinner.