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.
This commit is contained in:
Allan Feldman 2018-02-21 19:51:33 -08:00
parent dae74b674e
commit 75f11f0b65
1 changed files with 6 additions and 5 deletions

View File

@ -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