HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/pkgs/ujson-5.10.0-py312h5da7b33_0/info/test/tests/memory.py
import gc
import sys
import tracemalloc

# exec the first argument to get func() and n
exec_globals = {}
exec(sys.argv[1], exec_globals)
func = exec_globals["func"]
n = int(sys.argv[2]) if sys.argv[2:] else 1

# Pre-run once
try:
    func()
except Exception:
    pass

# Create filter to only report leaks on the 'tracemalloc: measure' line below
filters = []
with open(__file__) as fp:
    for i, line in enumerate(fp, start=1):
        if "tracemalloc: measure" in line:
            filters.append(tracemalloc.Filter(True, __file__, i))

# Clean up and take a snapshot
tracemalloc.start()
gc.collect()
before = tracemalloc.take_snapshot().filter_traces(filters)

# Run
for i in range(n):
    try:
        func()  # tracemalloc: measure
    except Exception:
        pass

# Clean up and second snapshot
gc.collect()
after = tracemalloc.take_snapshot().filter_traces(filters)

# Check that nothing got leaked
diff = after.compare_to(before, "lineno")
if diff:
    for stat in diff:
        print(stat)
    sys.exit(1)