Skip to content

Commit 614f539

Browse files
committed
Avoid @lru_cache on methods
The problem with `@lru_cache` on methods is that it also captures `self` and leaks it until the entry is evicted (if ever).
1 parent 7720154 commit 614f539

File tree

2 files changed

+2
-6
lines changed

2 files changed

+2
-6
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ def _fix(node, lineno, col_offset):
554554
return node
555555

556556

557+
@functools.lru_cache(maxsize=1)
557558
def _get_assertion_exprs(src: bytes) -> Dict[int, str]:
558559
"""Return a mapping from {lineno: "assertion test expression"}."""
559560
ret: Dict[int, str] = {}
@@ -675,10 +676,6 @@ def __init__(
675676
self.enable_assertion_pass_hook = False
676677
self.source = source
677678

678-
@functools.lru_cache(maxsize=1)
679-
def _assert_expr_to_lineno(self) -> Dict[int, str]:
680-
return _get_assertion_exprs(self.source)
681-
682679
def run(self, mod: ast.Module) -> None:
683680
"""Find all assert statements in *mod* and rewrite them."""
684681
if not mod.body:
@@ -906,7 +903,7 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]:
906903

907904
# Passed
908905
fmt_pass = self.helper("_format_explanation", msg)
909-
orig = self._assert_expr_to_lineno()[assert_.lineno]
906+
orig = _get_assertion_exprs(self.source)[assert_.lineno]
910907
hook_call_pass = ast.Expr(
911908
self.helper(
912909
"_call_assertion_pass",

src/_pytest/config/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ def _try_load_conftest(
522522
if x.is_dir():
523523
self._getconftestmodules(x, importmode, rootpath)
524524

525-
@lru_cache(maxsize=128)
526525
def _getconftestmodules(
527526
self, path: Path, importmode: Union[str, ImportMode], rootpath: Path
528527
) -> List[types.ModuleType]:

0 commit comments

Comments
 (0)