Skip to content

Commit 1728e56

Browse files
ngoldbaummattip
authored andcommitted
Uniformly disable assertions on release builds and enable them for tests
1 parent a852067 commit 1728e56

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎.github/workflows/ci.yaml‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ jobs:
188188
cd .. && \
189189
rm -rf libffi-3.4.6
190190
CIBW_ENVIRONMENT_PASS_LINUX: CFLAGS # ensure that the build container can see our overridden build config
191+
CIBW_ENVIRONMENT: >-
192+
CPPFLAGS="$CPPFLAGS ${{ env.skip_artifact_upload == 'true' && '-UNDEBUG' || '-DNDEBUG' }}"
193+
CFFI_TEST_ASSERTIONS=${{ env.skip_artifact_upload == 'true' && '1' || '0' }}
191194
CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux_img || 'manylinux2014' }}
192195
CIBW_MANYLINUX_I686_IMAGE: ${{ matrix.manylinux_img || 'manylinux2014' }}
193196
CIBW_MANYLINUX_AARCH64_IMAGE: ${{ matrix.manylinux_img || 'manylinux2014' }}
@@ -287,6 +290,9 @@ jobs:
287290
CIBW_ENABLE: cpython-prerelease
288291
CIBW_TEST_REQUIRES: pytest setuptools meson-python ninja
289292
CIBW_TEST_COMMAND: pip install pip --upgrade; cd {project}; PYTHONUNBUFFERED=1 pytest
293+
CIBW_ENVIRONMENT: >-
294+
CPPFLAGS="$CPPFLAGS ${{ env.skip_artifact_upload == 'true' && '-UNDEBUG' || '-DNDEBUG' }}"
295+
CFFI_TEST_ASSERTIONS=${{ env.skip_artifact_upload == 'true' && '1' || '0' }}
290296
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.deployment_target || '11.0' }}
291297
SDKROOT: ${{ matrix.sdkroot || 'macosx' }}
292298
run: |
@@ -375,6 +381,9 @@ jobs:
375381
CIBW_ENABLE: cpython-prerelease
376382
CIBW_TEST_REQUIRES: pytest setuptools meson-python ninja
377383
CIBW_TEST_COMMAND: ${{ matrix.test_cmd || 'python -m pytest {package}/src/c' }}
384+
CIBW_ENVIRONMENT: >-
385+
_CL_="$_CL_ ${{ env.skip_artifact_upload == 'true' && '/UNDEBUG' || '/DNDEBUG' }}"
386+
CFFI_TEST_ASSERTIONS=${{ env.skip_artifact_upload == 'true' && '1' || '0' }}
378387
# FIXME: /testing takes ~45min on Windows and has some failures...
379388
# CIBW_TEST_COMMAND='python -m pytest {package}/src/c {package}/testing'
380389
run: |
@@ -479,9 +488,12 @@ jobs:
479488
CIBW_TEST_SOURCES: cffi
480489
# Running tests from `testing/` will not work since they try to compile C code on device
481490
CIBW_TEST_COMMAND: python -m pytest -sv cffi/src/c/
491+
# Xcode forwards TEST_RUNNER_ variables into the simulator test process.
492+
CIBW_TEST_ENVIRONMENT: TEST_RUNNER_CFFI_TEST_ASSERTIONS=${{ env.skip_artifact_upload == 'true' && '1' || '0' }}
482493
# Environment variables for the build
483494
CIBW_ENVIRONMENT: >
484495
CFLAGS="-I${LIBFFI_IOS_DIR}/include"
496+
CPPFLAGS="$CPPFLAGS ${{ env.skip_artifact_upload == 'true' && '-UNDEBUG' || '-DNDEBUG' }}"
485497
LDFLAGS="-L${LIBFFI_IOS_DIR}/lib"
486498
PYTHONUNBUFFERED=1
487499
# Pass through our custom env vars
@@ -535,6 +547,10 @@ jobs:
535547
536548
pytest-run-parallel:
537549
needs: make_run_parallel_matrix
550+
env:
551+
CPPFLAGS: -UNDEBUG
552+
_CL_: /UNDEBUG
553+
CFFI_TEST_ASSERTIONS: '1'
538554
strategy:
539555
fail-fast: false
540556
matrix: ${{ fromJSON(needs.make_run_parallel_matrix.outputs.matrix_json) }}
@@ -562,13 +578,17 @@ jobs:
562578
clang_TSAN:
563579
runs-on: ubuntu-24.04
564580
container: ghcr.io/nascheme/numpy-tsan:3.15t-dev
581+
env:
582+
CFLAGS: -g -O0 -fsanitize=thread
583+
CPPFLAGS: -UNDEBUG
584+
CFFI_TEST_ASSERTIONS: '1'
565585
steps:
566586
- uses: actions/checkout@v7
567587

568588
- name: build and install
569589
run: |
570590
python -m pip install setuptools meson-python ninja pytest pytest-run-parallel
571-
CFLAGS="-g -O3 -fsanitize=thread" python -m pip install -v .
591+
python -m pip install -v .
572592
573593
- name: run tests under pytest-run-parallel
574594
run: |

‎src/c/_cffi_backend.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7585,6 +7585,14 @@ static _cffi_double_complex_t _testfunc25(double a, double b)
75857585
}
75867586
#endif
75877587

7588+
static int _testfunc26(void)
7589+
{
7590+
/* Report whether assert expressions are evaluated in this build. */
7591+
int enabled = 0;
7592+
assert(++enabled);
7593+
return enabled;
7594+
}
7595+
75887596
static PyObject *b__testfunc(PyObject *self, PyObject *args)
75897597
{
75907598
/* for testing only */
@@ -7621,6 +7629,7 @@ static PyObject *b__testfunc(PyObject *self, PyObject *args)
76217629
case 24: f = &_testfunc24; break;
76227630
case 25: f = &_testfunc25; break;
76237631
#endif
7632+
case 26: f = &_testfunc26; break;
76247633
default:
76257634
PyErr_SetNone(PyExc_ValueError);
76267635
return NULL;

‎src/c/test_c.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import contextlib
4+
import os
45
import traceback
56
import unittest.mock
67

@@ -1222,6 +1223,16 @@ def test_call_function_23_bool_array():
12221223
assert res == 1000
12231224
pytest.raises(ValueError, f, b"\x02\x02")
12241225

1226+
def test_backend_assertions():
1227+
expected = os.environ.get('CFFI_TEST_ASSERTIONS')
1228+
if expected is None and not os.environ.get('GITHUB_ACTIONS'):
1229+
pytest.skip("CFFI_TEST_ASSERTIONS is not set")
1230+
assert expected in ('0', '1')
1231+
BInt = new_primitive_type("int")
1232+
BFunc = new_function_type((), BInt, False)
1233+
f = cast(BFunc, _testfunc(26))
1234+
assert f() == int(expected)
1235+
12251236
def test_cannot_pass_struct_with_array_of_length_0():
12261237
BInt = new_primitive_type("int")
12271238
BArray0 = new_array_type(new_pointer_type(BInt), 0)

0 commit comments

Comments
 (0)