https://github.com/python/cpython/commit/c700121b15c9a055ec4488d22726b09f3f184d34
commit: c700121b15c9a055ec4488d22726b09f3f184d34
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-02T11:27:22Z
summary:

gh-155742: Add support.built_with_c_assertions() (#156776)

Co-authored-by: Stan Ulbrych <[email protected]>

files:
M Lib/test/libregrtest/utils.py
M Lib/test/pythoninfo.py
M Lib/test/support/__init__.py
M Lib/test/test_gc.py

diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py
index dfec7a59b02583b..6de2d07bcbb3b38 100644
--- a/Lib/test/libregrtest/utils.py
+++ b/Lib/test/libregrtest/utils.py
@@ -330,9 +330,6 @@ def get_build_info():
     # Get most important configure and build options as a list of strings.
     # Example: ['debug', 'ASAN+MSAN'] or ['release', 'LTO+PGO'].
 
-    config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
-    cflags = sysconfig.get_config_var('PY_CFLAGS') or ''
-    cflags += ' ' + (sysconfig.get_config_var('PY_CFLAGS_NODIST') or '')
     ldflags_nodist = sysconfig.get_config_var('PY_LDFLAGS_NODIST') or ''
 
     build = []
@@ -351,18 +348,16 @@ def get_build_info():
             free_threading = f"{free_threading} GIL={int(PYTHON_GIL)}"
         build.append(free_threading)
 
-    if hasattr(sys, 'gettotalrefcount'):
+    if support.Py_DEBUG:
         # --with-pydebug
         build.append('debug')
 
-        if '-DNDEBUG' in cflags:
+        if not support.built_with_c_assertions():
             build.append('without_assert')
     else:
         build.append('release')
 
-        if '--with-assertions' in config_args:
-            build.append('with_assert')
-        elif '-DNDEBUG' not in cflags:
+        if support.built_with_c_assertions():
             build.append('with_assert')
 
     # --enable-experimental-jit
diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py
index ea7edb798051567..90b32aaaaf9a40a 100644
--- a/Lib/test/pythoninfo.py
+++ b/Lib/test/pythoninfo.py
@@ -611,14 +611,6 @@ def collect_sysconfig(info_add):
         value = normalize_text(value)
         info_add('sysconfig[%s]' % name, value)
 
-    PY_CFLAGS = sysconfig.get_config_var('PY_CFLAGS')
-    NDEBUG = (PY_CFLAGS and '-DNDEBUG' in PY_CFLAGS)
-    if NDEBUG:
-        text = 'ignore assertions (macro defined)'
-    else:
-        text= 'build assertions (macro not defined)'
-    info_add('build.NDEBUG',text)
-
     for name in (
         'WITH_DOC_STRINGS',
         'WITH_DTRACE',
@@ -844,6 +836,8 @@ def collect_support(info_add):
              support.check_sanitizer(memory=True))
     info_add('support.check_sanitizer(ub=True)',
              support.check_sanitizer(ub=True))
+    info_add('support.built_with_c_assertions',
+             support.built_with_c_assertions())
 
 
 def collect_support_os_helper(info_add):
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 1c28af08988d9f0..28a0ba6c666629b 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -74,7 +74,7 @@
     "run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
     "reset_code", "on_github_actions",
     "requires_root_user", "requires_non_root_user",
-    "skip_if_double_rounding",
+    "skip_if_double_rounding", "built_with_c_assertions",
     ]
 
 
@@ -3526,3 +3526,18 @@ def check_immutable_type(testcase, type):
     else:
         flags = type_getflags(type)
         testcase.assertTrue(flags & Py_TPFLAGS_IMMUTABLETYPE)
+
+
+def built_with_c_assertions():
+    """Check if Python was built with C assertions (assert())."""
+
+    if MS_WINDOWS:
+        # On Windows, rely on the Py_DEBUG macro to check for assertions
+        return Py_DEBUG
+
+    # Check if the NDEBUG macro is defined in C compiler flags
+    PY_CFLAGS = (sysconfig.get_config_var('PY_CFLAGS') or '')
+    if '-DNDEBUG' in PY_CFLAGS:
+        return False
+
+    return True
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 4c721c01a34f070..10f2a5dfb505e31 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -12,7 +12,6 @@
 
 import gc
 import sys
-import sysconfig
 import textwrap
 import threading
 import time
@@ -79,13 +78,6 @@ def __init__(self, partner=None):
     def __tp_del__(self):
         pass
 
-if sysconfig.get_config_vars().get('PY_CFLAGS', ''):
-    BUILD_WITH_NDEBUG = ('-DNDEBUG' in 
sysconfig.get_config_vars()['PY_CFLAGS'])
-else:
-    # Usually, sys.gettotalrefcount() is only present if Python has been
-    # compiled in debug mode. If it's missing, expect that Python has
-    # been released in release mode: with NDEBUG defined.
-    BUILD_WITH_NDEBUG = (not hasattr(sys, 'gettotalrefcount'))
 
 ### Tests
 ###############################################################################
@@ -1422,8 +1414,8 @@ def test_collect_garbage(self):
 
 
     @requires_subprocess()
-    @unittest.skipIf(BUILD_WITH_NDEBUG,
-                     'built with -NDEBUG')
+    @unittest.skipIf(not support.built_with_c_assertions(),
+                     'built without C assertions')
     def test_refcount_errors(self):
         self.preclean()
         # Verify the "handling" of objects with broken refcounts

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to