Skip to content

Commit a538b61

Browse files
committed
fixup! Add object tracking feature for Python 3.13.3+
Add some documentation for the new marker. Signed-off-by: Matt Wozniski <godlygeek@gmail.com>
1 parent be1151e commit a538b61

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

docs/usage.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,37 @@ that can be used to enforce additional checks and validations on tests.
152152
Because of this, you will almost certainly need to allow some small amount of
153153
leaked memory per call stack, or use the ``filter_fn`` argument to filter out
154154
false-positive leak reports based on the call stack they're associated with.
155+
156+
157+
.. py:function:: pytest.mark.track_leaked_objects()
158+
159+
Fail the execution of the test if any Python objects created while the test body
160+
runs are still alive at the end of the test.
161+
162+
This is only possible for Python 3.13.3 and newer. Using this marker with older
163+
Python versions will cause an error during test collection. You can use
164+
``pytest.mark.skipif`` to prevent the test from running if the Python version is too
165+
old, or you can conditionally add the ``track_leaked_objects`` marker only if the
166+
Python version is new enough.
167+
168+
.. warning::
169+
It is **very** challenging to write tests that do not "leak" memory in some way,
170+
due to circumstances beyond your control.
171+
172+
There are many caches inside the Python interpreter itself. Just a few examples:
173+
174+
- The `re` module caches compiled regexes.
175+
- The `logging` module caches whether a given log level is active for
176+
a particular logger the first time you try to log something at that level.
177+
178+
There are many more such caches. Also, within pytest, any message that you log or
179+
print is captured, so that it can be included in the output if the test fails.
180+
181+
Memray sees these all as "leaks", because something was allocated while the test
182+
ran and it was not freed by the time the test body finished. We don't know that
183+
it's due to an implementation detail of the standard library or pytest that the
184+
memory wasn't freed. Morever, because these caches are implementation details,
185+
they can change from one Python version to another.
186+
187+
Because of this, you will need to very carefully design your test to avoid
188+
objects being cached.

0 commit comments

Comments
 (0)