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:
Bruno Oliveira 2018-07-22 09:27:34 -03:00
parent 0bb29d5649
commit 5167933395
1 changed files with 15 additions and 12 deletions

View File

@ -789,23 +789,26 @@ def call_fixture_func(fixturefunc, request, kwargs):
if yieldctx:
it = fixturefunc(**kwargs)
res = next(it)
def teardown():
try:
next(it)
except StopIteration:
pass
else:
fail_fixturefunc(
fixturefunc, "yield_fixture function has more than one 'yield'"
)
request.addfinalizer(teardown)
finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, it)
request.addfinalizer(finalizer)
else:
res = fixturefunc(**kwargs)
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):
""" A container for a factory definition. """