test_leak.py 669 B

12345678910111213141516171819202122232425
  1. from __future__ import annotations
  2. import gc
  3. from markupsafe import escape
  4. def test_markup_leaks() -> None:
  5. counts = set()
  6. # Try to start with a "clean" count. Works for PyPy but not 3.13 JIT.
  7. gc.collect()
  8. for _ in range(20):
  9. for _ in range(1000):
  10. escape("foo")
  11. escape("<foo>")
  12. escape("foo")
  13. escape("<foo>")
  14. counts.add(len(gc.get_objects()))
  15. # Some implementations, such as PyPy and Python 3.13 JIT, end up with 2
  16. # counts rather than one. Presumably this is internals stabilizing. A leak
  17. # would presumably have a different count every loop.
  18. assert len(counts) < 3