Skip to content

Commit

Permalink
fixup! fixup! fixup! Add a new marker to check for memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
godlygeek committed Aug 18, 2023
1 parent b71d68d commit 05cf2cf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
5 changes: 3 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ pytest-memray API
Types
-----

.. autoclass:: StackElement
.. autoclass:: StackFrame()
:members:

.. autoclass:: Stack
.. autoclass:: Stack()
:members:

.. autoclass:: LeaksFilteringFunction
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_memray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from ._version import __version__ as __version__
from .marks import StackElement
from .marks import StackFrame
from .marks import Stack
from .marks import LeaksFilteringFunction

__all__ = [
"__version__",
"Stack",
"StackElement",
"StackFrame",
"LeaksFilteringFunction",
]
41 changes: 37 additions & 4 deletions src/pytest_memray/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
from typing import Tuple
from typing import cast
from typing import Callable
from typing import Optional
from typing import Collection
from typing import Sequence

from memray import AllocationRecord
from memray import FileReader
Expand All @@ -17,12 +18,34 @@
from .utils import value_or_ini

PytestSection = Tuple[str, str]
StackElement = Tuple[str, str, int]


@dataclass
class StackFrame:
"""One frame of a call stack.
Each frame has attributes to tell you what code was executing.
"""

function: str
"""The function being executed, or ``"???"`` if unknown."""

filename: str
"""The source file being executed, or ``"???"`` if unknown."""

lineno: int
"""The line number of the executing line, or ``0`` if unknown."""


@dataclass
class Stack:
frames: Collection[StackElement]
"""The call stack that led to some memory allocation.
You can inspect the frames which make up the call stack.
"""

frames: Sequence[StackFrame]
"""The frames that make up the call stack, most recent first."""


LeaksFilteringFunction = Callable[[Stack], bool]
Expand Down Expand Up @@ -112,6 +135,16 @@ def long_repr(self) -> str:
)


def passes_filter(
stack: Iterable[Tuple[str, str, int]], filter_fn: Optional[LeaksFilteringFunction]
) -> bool:
if filter_fn is None:
return True

stack_elements = [StackFrame(*frame) for frame in stack]
return filter_fn(Stack(stack_elements))


def limit_memory(
limit: str, *, _result_file: Path, _config: Config
) -> _MemoryInfo | None:
Expand Down Expand Up @@ -148,7 +181,7 @@ def limit_leaks(
for allocation in allocations
if (
allocation.size >= memory_limit
and (filter_fn is None or filter_fn(Stack(allocation.hybrid_stack_trace())))
and passes_filter(allocation.hybrid_stack_trace(), filter_fn)
)
)
if not leaked_allocations:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ def this_should_not_be_there():
# No free call here
def filtering_function(stack):
for fn, _, _ in stack.frames:
if fn == "this_should_not_be_there":
for frame in stack.frames:
if frame.function == "this_should_not_be_there":
return False
return True
Expand Down

0 comments on commit 05cf2cf

Please sign in to comment.