Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support.built_with_c_assertions()
Fix also a compiler warning on Windows:

    Objects\object.c(3629,39): warning C4244: 'function': conversion
    from 'long' to 'un signed char', possible loss of data
  • Loading branch information
vstinner committed Aug 14, 2026
commit 0608d2bbef09dd4be8b0354e246d215ebbadc3c0
10 changes: 2 additions & 8 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3492,3 +3492,14 @@ 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 in debug mode or using --with-assertions
if MS_WINDOWS:
Py_DEBUG = hasattr(sys, 'gettotalrefcount')
return Py_DEBUG

PY_CFLAGS = sysconfig.get_config_var('PY_CFLAGS')
NDEBUG = (PY_CFLAGS and '-DNDEBUG' in PY_CFLAGS)
return (not NDEBUG)
3 changes: 3 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,9 @@ def test_ceval_decref(self):
self.assertEqual(lines.count("DESTROY list"), 2)


# _Py_CheckSingletons() is only built if the NDEBUG macro is not defined
@unittest.skipUnless(support.built_with_c_assertions(),
'Python built without C assertions')
class TestCheckSingleton(unittest.TestCase):
# Test _Py_CheckSingletons() which is called by gc.collect()
#
Expand Down
2 changes: 1 addition & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3626,7 +3626,7 @@ _Py_CheckSingletons(void)

for (ival=0; ival <= 255; ival++) {
obj = (PyObject*)&_Py_SINGLETON(bytes_characters)[ival];
check_singleton_bytes(obj, 1, ival);
check_singleton_bytes(obj, 1, (unsigned char)ival);
}

// Empty Unicode string ('')
Expand Down
Loading