Move teardown code of yield fixtures to a partial to avoid leaks
As it were before, it was keeping a reference to fixturefunc and it alive when an error occurred
This commit is contained in:
parent
0bb29d5649
commit
5167933395
|
@ -789,23 +789,26 @@ def call_fixture_func(fixturefunc, request, kwargs):
|
||||||
if yieldctx:
|
if yieldctx:
|
||||||
it = fixturefunc(**kwargs)
|
it = fixturefunc(**kwargs)
|
||||||
res = next(it)
|
res = next(it)
|
||||||
|
finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, it)
|
||||||
def teardown():
|
request.addfinalizer(finalizer)
|
||||||
try:
|
|
||||||
next(it)
|
|
||||||
except StopIteration:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
fail_fixturefunc(
|
|
||||||
fixturefunc, "yield_fixture function has more than one 'yield'"
|
|
||||||
)
|
|
||||||
|
|
||||||
request.addfinalizer(teardown)
|
|
||||||
else:
|
else:
|
||||||
res = fixturefunc(**kwargs)
|
res = fixturefunc(**kwargs)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def _teardown_yield_fixture(fixturefunc, it):
|
||||||
|
"""Executes the teardown of a fixture function by advancing the iterator after the
|
||||||
|
yield and ensure the iteration ends (if not it means there is more than one yield in the function"""
|
||||||
|
try:
|
||||||
|
next(it)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
fail_fixturefunc(
|
||||||
|
fixturefunc, "yield_fixture function has more than one 'yield'"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FixtureDef(object):
|
class FixtureDef(object):
|
||||||
""" A container for a factory definition. """
|
""" A container for a factory definition. """
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue