Skip to content

Commit f9d3554

Browse files
bstaleticYannickJadoul
authored andcommitted
Add a memcheck cmake target
- Add a memcheck cmake target - Reformat cmake - Appease the formatting overlords - they are angry - Format CMake valgrind target decently
1 parent c183120 commit f9d3554

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

tests/CMakeLists.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,30 @@ endif()
370370
string(REPLACE "test_" "${CMAKE_CURRENT_SOURCE_DIR}/test_" PYBIND11_ABS_PYTEST_FILES
371371
"${PYBIND11_PYTEST_FILES}")
372372

373+
if(NOT PYBIND11_MEMCHECK)
374+
set(PYBIND11_TEST_PREFIX_COMMAND "")
375+
else()
376+
# cmake-format: off
377+
set(PYBIND11_TEST_PREFIX_COMMAND
378+
PYTHONMALLOC=malloc
379+
valgrind
380+
--leak-check=full
381+
--show-leak-kinds=definite,indirect
382+
--errors-for-leak-kinds=definite,indirect
383+
--error-exitcode=1
384+
--read-var-info=yes
385+
--track-origins=yes
386+
--suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-python.supp"
387+
--suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-numpy-scipy.supp"
388+
--gen-suppressions=all)
389+
# cmake-format: on
390+
endif()
391+
373392
# A single command to compile and run the tests
374393
add_custom_target(
375394
pytest
376-
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_ABS_PYTEST_FILES}
395+
COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest
396+
${PYBIND11_ABS_PYTEST_FILES}
377397
DEPENDS ${test_targets}
378398
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
379399
USES_TERMINAL)
@@ -386,6 +406,11 @@ if(PYBIND11_TEST_OVERRIDE)
386406
"Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
387407
endif()
388408

409+
add_custom_target(
410+
memcheck
411+
DEPENDS pytest
412+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
413+
389414
# Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
390415
add_custom_target(check DEPENDS pytest)
391416

tests/valgrind-python.supp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,21 @@
115115
fun:_PyImport_FindSharedFuncptr
116116
fun:_PyImport_LoadDynamicModuleWithSpec
117117
}
118+
119+
# Not really CPython-specific, see link
120+
{
121+
dlopen leak (https://stackoverflow.com/questions/1542457/memory-leak-reported-by-valgrind-in-dlopen)
122+
Memcheck:Leak
123+
fun:malloc
124+
...
125+
fun:dl_open_worker
126+
fun:_dl_catch_exception
127+
fun:_dl_open
128+
fun:dlopen_doit
129+
fun:_dl_catch_exception
130+
fun:_dl_catch_error
131+
fun:_dlerror_run
132+
fun:dlopen@@GLIBC_2.2.5
133+
fun:_PyImport_FindSharedFuncptr
134+
fun:_PyImport_LoadDynamicModuleWithSpec
135+
}

0 commit comments

Comments
 (0)