BUG: Fix some bugs found via valgrind (#30680) by charris · Pull Request #30709 · numpy/numpy

#!/usr/bin/env python

"""
NOTE: To open the files with actual failures, try something like:

find . -type f ! -name "*valgrind-out.txt" | xargs -I {} sh -c "grep -q -E '(FAILURES|ERRORS)' {} && gnome-text-editor {} &"
"""

from concurrent.futures import ProcessPoolExecutor
import os
import subprocess

os.environ["PYTHONMALLOC"] = "malloc"  # for the spawned processes

test_files = []

DIR = os.getcwd()
FOLDER = "/home/sebastianb/forks/numpy/"

for root, dirs, files in os.walk(FOLDER + "numpy/"):
    files = [f for f in files if f.startswith("test_") and f.endswith(".py")]
    root = root.removeprefix(FOLDER + "numpy/")
    test_files.extend([root + "/" + f for f in files])

for f in test_files[:]:
    # These are particularly slow, so start early.
    if "test_multiarray" in f or "ma/test_core" in f:
        test_files.remove(f)
        test_files.insert(0, f)


setup = """
export PYTHONPATH="/home/sebastianb/forks/numpy/build-install/usr/lib/python3.14/site-packages"
/home/sebastianb/miniforge3/envs/numpy-dev/bin/python3.14 -P -c 'import numpy'
"""

cmd = "PYTHONMALLOC=malloc valgrind --errors-for-leak-kinds=definite --show-leak-kinds=definite --log-file={log_file} "
cmd += '''python -P -mpytest -vv {test_file} -m "not slow"'''
cmd += " --valgrind --valgrind-log={log_file}"


def process(file):
    # Yeah, should use proper pathlib paths :)
    os.chdir(DIR)
    output = os.path.abspath("output/" + file.replace("/", "_").replace(".py", ".txt"))
    log = output.replace(".txt", "-valgrind-out.txt")

    with open(output, "w") as out_file:
        os.chdir("/home/sebastianb/forks/numpy/build-install/usr/lib/python3.14/site-packages")
        actual_command = cmd.format(log_file=log, test_file="numpy/" + file, FOLDER=FOLDER)
        print(actual_command)
        subprocess.run(setup, shell=True)
        subprocess.run(actual_command, shell=True, stdout=out_file, stderr=out_file)
        print("    done file:", file)


def run():
    with ProcessPoolExecutor(16) as executor:
        result = executor.map(process, test_files)

    print(list(result))

if __name__ == "__main__":
    run()