]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-135734: correctly handle `--enable-optimizations --disable-test-modules...
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Thu, 21 Aug 2025 18:28:03 +0000 (20:28 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Aug 2025 18:28:03 +0000 (20:28 +0200)
Lib/test/libregrtest/main.py
Lib/test/support/__init__.py
Misc/NEWS.d/next/Build/2025-08-20-16-45-34.gh-issue-135734.2hvJCe.rst [new file with mode: 0644]

index 2894da7509110cc0883980d082912b113299cb1e..9e04138890c25752eef024c39547101081d0f193 100644 (file)
@@ -9,6 +9,7 @@ import trace
 from typing import NoReturn
 
 from test.support import (os_helper, MS_WINDOWS, flush_std_streams,
+                          can_use_suppress_immortalization,
                           suppress_immortalization)
 
 from .cmdline import _parse_args, Namespace
@@ -536,8 +537,16 @@ class Regrtest:
             if self.num_workers:
                 self._run_tests_mp(runtests, self.num_workers)
             else:
+                # gh-135734: suppress_immortalization() raises SkipTest
+                # if _testinternalcapi is missing and the -R option is set.
+                if not can_use_suppress_immortalization(runtests.hunt_refleak):
+                    print("Module '_testinternalcapi' is missing. "
+                          "Did you disable it with --disable-test-modules?",
+                          file=sys.stderr)
+                    raise SystemExit(1)
+
                 # gh-117783: don't immortalize deferred objects when tracking
-                # refleaks. Only releveant for the free-threaded build.
+                # refleaks. Only relevant for the free-threaded build.
                 with suppress_immortalization(runtests.hunt_refleak):
                     self.run_tests_sequentially(runtests)
 
index c60fc03064f3c252d1c90522d16e40f022148e24..279a9ca23ef4dfb3b36bdd6c7fb90bad8a19b1fa 100644 (file)
@@ -519,25 +519,43 @@ def requires_debug_ranges(reason='requires co_positions / debug_ranges'):
         reason = e.args[0] if e.args else reason
     return unittest.skipIf(skip, reason)
 
-@contextlib.contextmanager
-def suppress_immortalization(suppress=True):
-    """Suppress immortalization of deferred objects."""
+
+def can_use_suppress_immortalization(suppress=True):
+    """Check if suppress_immortalization(suppress) can be used.
+
+    Use this helper in code where SkipTest must be eagerly handled.
+    """
+    if not suppress:
+        return True
     try:
         import _testinternalcapi
     except ImportError:
-        yield
-        return
+        return False
+    return True
 
+
+@contextlib.contextmanager
+def suppress_immortalization(suppress=True):
+    """Suppress immortalization of deferred objects.
+
+    If _testinternalcapi is not available, the decorated test or class
+    is skipped. Use can_use_suppress_immortalization() outside test cases
+    to check if this decorator can be used.
+    """
     if not suppress:
-        yield
+        yield  # no-op
         return
 
+    from .import_helper import import_module
+
+    _testinternalcapi = import_module("_testinternalcapi")
     _testinternalcapi.suppress_immortalization(True)
     try:
         yield
     finally:
         _testinternalcapi.suppress_immortalization(False)
 
+
 def skip_if_suppress_immortalization():
     try:
         import _testinternalcapi
diff --git a/Misc/NEWS.d/next/Build/2025-08-20-16-45-34.gh-issue-135734.2hvJCe.rst b/Misc/NEWS.d/next/Build/2025-08-20-16-45-34.gh-issue-135734.2hvJCe.rst
new file mode 100644 (file)
index 0000000..9a83580
--- /dev/null
@@ -0,0 +1,4 @@
+Python can correctly be configured and built with
+``./configure --enable-optimizations --disable-test-modules``.
+Previously, the profile data generation step failed due to PGO tests where
+immortalization couldn't be properly suppressed. Patch by Bénédikt Tran.