Skip to content

Commit 0608d2b

Browse files
committed
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
1 parent 48b2e54 commit 0608d2b

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,3 +3492,14 @@ def check_immutable_type(testcase, type):
34923492
else:
34933493
flags = type_getflags(type)
34943494
testcase.assertTrue(flags & Py_TPFLAGS_IMMUTABLETYPE)
3495+
3496+
3497+
def built_with_c_assertions():
3498+
# Check if Python was built in debug mode or using --with-assertions
3499+
if MS_WINDOWS:
3500+
Py_DEBUG = hasattr(sys, 'gettotalrefcount')
3501+
return Py_DEBUG
3502+
3503+
PY_CFLAGS = sysconfig.get_config_var('PY_CFLAGS')
3504+
NDEBUG = (PY_CFLAGS and '-DNDEBUG' in PY_CFLAGS)
3505+
return (not NDEBUG)

Lib/test/test_capi/test_misc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,9 @@ def test_ceval_decref(self):
30943094
self.assertEqual(lines.count("DESTROY list"), 2)
30953095

30963096

3097+
# _Py_CheckSingletons() is only built if the NDEBUG macro is not defined
3098+
@unittest.skipUnless(support.built_with_c_assertions(),
3099+
'Python built without C assertions')
30973100
class TestCheckSingleton(unittest.TestCase):
30983101
# Test _Py_CheckSingletons() which is called by gc.collect()
30993102
#

Objects/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ _Py_CheckSingletons(void)
36263626

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

36323632
// Empty Unicode string ('')

0 commit comments

Comments
 (0)