Python has valgrind support with --with-valgrind, please add a use flag for it (and depend on valgrind if it is active).
What does this option do?
https://github.com/python/cpython/blob/master/Misc/README.valgrind The valgrind option explicitly redirects memory allocations to valgrind: https://github.com/python/cpython/blob/5d4b09c106e2e3b7d17dd1b904568eb2ff7353d3/Objects/obmalloc.c#L1233 Which is very handy to debug issues that come up with manual python api work in c extensions or cython.
I've updated the pull request for the current python 3.9 ebuild. If it looks alright, I can add it for all currently active versions.
I'm not sure how to deal with this best. I don't think USE=valgrind is correct here, as it would wrongly suggest that Python actually uses valgrind. Maybe we could add USE=debug instead that unconditionally disables pymalloc. I think that would be cleaner that supporting exactly one memory debugger. WDYT?
Having a USE=valgrind flag would make sense to me - it enables valgrind support in Python (--with-valgrind as implemented 2009 in CPython: https://github.com/python/cpython/commit/05159c4a7b37403613772a0883bd927bb57e8060), so Python can detect if it's running under valgrind, and then memory allocation behaves differently. And it does use valgrind.h to invoke the test if we're running under Valgrind. My usecase would be to use normal Python in nearly every execution, but once I want to trace down some corruption (mainly due to Cython) Python's valgrind support would activate. Unconditionally disabling pymalloc with USE=debug would not be helpful.
The bug has been closed via the following commit(s): https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=3b7ae26f65794fed2d6b90b85d6a81a96b326702 commit 3b7ae26f65794fed2d6b90b85d6a81a96b326702 Author: Jonas Jelten <jj@sft.lol> AuthorDate: 2022-11-22 19:11:50 +0000 Commit: Michał Górny <mgorny@gentoo.org> CommitDate: 2022-11-23 06:53:42 +0000 dev-lang/python: add valgrind support When the valgrind useflag is enabled, Python performs a runtime check if it is run in the Valgrind VM, and adjusts its memory allocator to not use pymalloc. Valgrind then does a much better job in tracking the memory reachability. Closes: https://bugs.gentoo.org/610816 Signed-off-by: Jonas Jelten <jj@sft.lol> Closes: https://github.com/gentoo/gentoo/pull/11012 Signed-off-by: Michał Górny <mgorny@gentoo.org> dev-lang/python/metadata.xml | 5 +++++ dev-lang/python/python-2.7.18_p16.ebuild | 6 +++++- dev-lang/python/python-3.10.8_p3.ebuild | 4 +++- dev-lang/python/python-3.11.0_p2.ebuild | 4 +++- dev-lang/python/python-3.12.0_alpha2.ebuild | 4 +++- dev-lang/python/python-3.8.15_p3.ebuild | 5 ++++- dev-lang/python/python-3.9.15_p3.ebuild | 4 +++- 7 files changed, 26 insertions(+), 6 deletions(-)