Debian Codegraph @

tests/test_leak.py

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