From 75f11f0b6576a7bd60927e3425a679c20c40f261 Mon Sep 17 00:00:00 2001 From: Allan Feldman Date: Wed, 21 Feb 2018 19:51:33 -0800 Subject: [PATCH] Fix reference cycle caused by PseudoFixtureDef. Python types have reference cycles to themselves when they are created. This is partially caused by descriptors which get / set values from the __dict__ attribute for getattr / setattr on classes. This is not normally an issue since types tend to remain referenced for the lifetime of the Python process (and thus never become garbage). However, in the case of PseudoFixtureDef, the class is generated in _get_active_fixturedef and later discarded when pytest_fixture_setup returns. As a result, the generated PseudoFixtureDef type becomes garbage. This is not really a performance issue but it can lead to some problems when making tests and assertions about garbage when using pytest. This garbage creation problem can be rectified by returning a namedtuple instance which is functionally the same. In the modified code, the namedtuple is allocated / deallocated using reference counting rather than having to use the garbage collector. --- _pytest/fixtures.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index a8445767c..21fc32846 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -4,7 +4,7 @@ import functools import inspect import sys import warnings -from collections import OrderedDict, deque, defaultdict +from collections import OrderedDict, deque, defaultdict, namedtuple import attr import py @@ -23,6 +23,8 @@ from _pytest.compat import ( ) from _pytest.outcomes import fail, TEST_OUTCOME +PseudoFixtureDef = namedtuple('PseudoFixtureDef', ('cached_result', 'scope')) + def pytest_sessionstart(session): import _pytest.python @@ -440,10 +442,9 @@ class FixtureRequest(FuncargnamesCompatAttr): fixturedef = self._getnextfixturedef(argname) except FixtureLookupError: if argname == "request": - class PseudoFixtureDef(object): - cached_result = (self, [0], None) - scope = "function" - return PseudoFixtureDef + cached_result = (self, [0], None) + scope = "function" + return PseudoFixtureDef(cached_result, scope) raise # remove indent to prevent the python3 exception # from leaking into the call