Replies: 1 comment
-
I'm going to go with the following, it's ugly but seem to work. @pytest.fixture()
def tracemalloc_resource_warning(recwarn, N=10):
"""fixture to enable tracemalloc for a single test, and report the
location of the leaked resource
We cannot only enable tracemalloc, as otherwise it is stopped just after the
test, the frame cache is cleared by tracemalloc.stop() and thus the warning
printing code get None when doing
`tracemalloc.get_object_traceback(r.source)`.
So we need to both filter the warnings to enable ResourceWarning, and loop
through it print the stack before we stop tracemalloc and continue.
"""
tracemalloc.start(N)
with warnings.catch_warnings():
warnings.simplefilter("always", category=ResourceWarning)
yield None
try:
for r in recwarn:
if r.category is ResourceWarning and r.source is not None:
tb = tracemalloc.get_object_traceback(r.source)
if tb:
info = f"Leaking resource:{r}\n |" + "\n |".join(tb.format())
# technically an Error and not a failure as we fail in the fixture
# and not the test
pytest.fail(info)
finally:
tracemalloc.stop() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've tried to enable tracemalloc for a single set of tests, as there is a random ResourceWarning,
and runing globlally with TRACEMALLOC=20 is wayyyy to long.
Unfortunately setting a tracemalloc fixture with
tracemalloc.start(20)/.stop()
, meand that tracemalloc is disable beofre the warnings get printed;So I still get the
Do anyone has a tip on how I could have a proper tracemalloc and resource warnings for a single test ?
Beta Was this translation helpful? Give feedback.
All reactions