Skip to content

Commit c700121

Browse files
gh-155742: Add support.built_with_c_assertions() (#156776)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent d557d64 commit c700121

4 files changed

Lines changed: 23 additions & 27 deletions

File tree

Lib/test/libregrtest/utils.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,6 @@ def get_build_info():
330330
# Get most important configure and build options as a list of strings.
331331
# Example: ['debug', 'ASAN+MSAN'] or ['release', 'LTO+PGO'].
332332

333-
config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
334-
cflags = sysconfig.get_config_var('PY_CFLAGS') or ''
335-
cflags += ' ' + (sysconfig.get_config_var('PY_CFLAGS_NODIST') or '')
336333
ldflags_nodist = sysconfig.get_config_var('PY_LDFLAGS_NODIST') or ''
337334

338335
build = []
@@ -351,18 +348,16 @@ def get_build_info():
351348
free_threading = f"{free_threading} GIL={int(PYTHON_GIL)}"
352349
build.append(free_threading)
353350

354-
if hasattr(sys, 'gettotalrefcount'):
351+
if support.Py_DEBUG:
355352
# --with-pydebug
356353
build.append('debug')
357354

358-
if '-DNDEBUG' in cflags:
355+
if not support.built_with_c_assertions():
359356
build.append('without_assert')
360357
else:
361358
build.append('release')
362359

363-
if '--with-assertions' in config_args:
364-
build.append('with_assert')
365-
elif '-DNDEBUG' not in cflags:
360+
if support.built_with_c_assertions():
366361
build.append('with_assert')
367362

368363
# --enable-experimental-jit

Lib/test/pythoninfo.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,6 @@ def collect_sysconfig(info_add):
611611
value = normalize_text(value)
612612
info_add('sysconfig[%s]' % name, value)
613613

614-
PY_CFLAGS = sysconfig.get_config_var('PY_CFLAGS')
615-
NDEBUG = (PY_CFLAGS and '-DNDEBUG' in PY_CFLAGS)
616-
if NDEBUG:
617-
text = 'ignore assertions (macro defined)'
618-
else:
619-
text= 'build assertions (macro not defined)'
620-
info_add('build.NDEBUG',text)
621-
622614
for name in (
623615
'WITH_DOC_STRINGS',
624616
'WITH_DTRACE',
@@ -844,6 +836,8 @@ def collect_support(info_add):
844836
support.check_sanitizer(memory=True))
845837
info_add('support.check_sanitizer(ub=True)',
846838
support.check_sanitizer(ub=True))
839+
info_add('support.built_with_c_assertions',
840+
support.built_with_c_assertions())
847841

848842

849843
def collect_support_os_helper(info_add):

Lib/test/support/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
7575
"reset_code", "on_github_actions",
7676
"requires_root_user", "requires_non_root_user",
77-
"skip_if_double_rounding",
77+
"skip_if_double_rounding", "built_with_c_assertions",
7878
]
7979

8080

@@ -3526,3 +3526,18 @@ def check_immutable_type(testcase, type):
35263526
else:
35273527
flags = type_getflags(type)
35283528
testcase.assertTrue(flags & Py_TPFLAGS_IMMUTABLETYPE)
3529+
3530+
3531+
def built_with_c_assertions():
3532+
"""Check if Python was built with C assertions (assert())."""
3533+
3534+
if MS_WINDOWS:
3535+
# On Windows, rely on the Py_DEBUG macro to check for assertions
3536+
return Py_DEBUG
3537+
3538+
# Check if the NDEBUG macro is defined in C compiler flags
3539+
PY_CFLAGS = (sysconfig.get_config_var('PY_CFLAGS') or '')
3540+
if '-DNDEBUG' in PY_CFLAGS:
3541+
return False
3542+
3543+
return True

Lib/test/test_gc.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import gc
1414
import sys
15-
import sysconfig
1615
import textwrap
1716
import threading
1817
import time
@@ -79,13 +78,6 @@ def __init__(self, partner=None):
7978
def __tp_del__(self):
8079
pass
8180

82-
if sysconfig.get_config_vars().get('PY_CFLAGS', ''):
83-
BUILD_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
84-
else:
85-
# Usually, sys.gettotalrefcount() is only present if Python has been
86-
# compiled in debug mode. If it's missing, expect that Python has
87-
# been released in release mode: with NDEBUG defined.
88-
BUILD_WITH_NDEBUG = (not hasattr(sys, 'gettotalrefcount'))
8981

9082
### Tests
9183
###############################################################################
@@ -1422,8 +1414,8 @@ def test_collect_garbage(self):
14221414

14231415

14241416
@requires_subprocess()
1425-
@unittest.skipIf(BUILD_WITH_NDEBUG,
1426-
'built with -NDEBUG')
1417+
@unittest.skipIf(not support.built_with_c_assertions(),
1418+
'built without C assertions')
14271419
def test_refcount_errors(self):
14281420
self.preclean()
14291421
# Verify the "handling" of objects with broken refcounts

0 commit comments

Comments
 (0)