|
| 1 | +# Script that tries to prove that the quickjs wrapper does not leak memory. |
| 2 | +# |
| 3 | +# It finds the leak if a Py_DECREF is commented out in module.c. |
| 4 | + |
| 5 | +import gc |
| 6 | +import tracemalloc |
| 7 | +import unittest |
| 8 | + |
| 9 | +import quickjs |
| 10 | +import test_quickjs |
| 11 | + |
| 12 | +def run(): |
| 13 | + loader = unittest.TestLoader() |
| 14 | + suite = loader.discover(".") |
| 15 | + runner = unittest.TextTestRunner() |
| 16 | + runner.run(suite) |
| 17 | + |
| 18 | +def main(): |
| 19 | + print("Warming up (to discount regex cache etc.)") |
| 20 | + run() |
| 21 | + |
| 22 | + tracemalloc.start(25) |
| 23 | + gc.collect() |
| 24 | + snapshot1 = tracemalloc.take_snapshot() |
| 25 | + run() |
| 26 | + gc.collect() |
| 27 | + snapshot2 = tracemalloc.take_snapshot() |
| 28 | + |
| 29 | + top_stats = snapshot2.compare_to(snapshot1, 'traceback') |
| 30 | + |
| 31 | + print("Objects not released") |
| 32 | + print("====================") |
| 33 | + for stat in top_stats: |
| 34 | + if "tracemalloc.py" in str(stat) or stat.size_diff == 0: |
| 35 | + continue |
| 36 | + print(stat) |
| 37 | + for line in stat.traceback.format(): |
| 38 | + print(" ", line) |
| 39 | + |
| 40 | + print("\nquickjs should not show up above.") |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + main() |
0 commit comments