Skip to content

Commit

Permalink
Merge pull request #11794 from bluetech/fixturedef-ref-cycle
Browse files Browse the repository at this point in the history
A few cleanups
  • Loading branch information
RonnyPfannschmidt authored Jan 10, 2024
2 parents c4a356e + 35a3863 commit b1c4308
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,16 @@ def _importconftest(
if existing is not None:
return cast(types.ModuleType, existing)

# conftest.py files there are not in a Python package all have module
# name "conftest", and thus conflict with each other. Clear the existing
# before loading the new one, otherwise the existing one will be
# returned from the module cache.
pkgpath = resolve_package_path(conftestpath)
if pkgpath is None:
_ensure_removed_sysmodule(conftestpath.stem)
try:
del sys.modules[conftestpath.stem]
except KeyError:
pass

try:
mod = import_path(conftestpath, mode=importmode, root=rootpath)
Expand Down Expand Up @@ -818,13 +825,6 @@ def _get_plugin_specs_as_list(
)


def _ensure_removed_sysmodule(modname: str) -> None:
try:
del sys.modules[modname]
except KeyError:
pass


class Notset:
def __repr__(self):
return "<NOTSET>"
Expand Down
12 changes: 4 additions & 8 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def raiseerror(self, msg: Optional[str]) -> NoReturn:
:param msg:
An optional custom error message.
"""
raise self._fixturemanager.FixtureLookupError(None, self, msg)
raise FixtureLookupError(None, self, msg)

def getfixturevalue(self, argname: str) -> Any:
"""Dynamically run a named fixture function.
Expand Down Expand Up @@ -970,7 +970,7 @@ class FixtureDef(Generic[FixtureValue]):

def __init__(
self,
fixturemanager: "FixtureManager",
config: Config,
baseid: Optional[str],
argname: str,
func: "_FixtureFunc[FixtureValue]",
Expand All @@ -984,7 +984,6 @@ def __init__(
_ispytest: bool = False,
) -> None:
check_ispytest(_ispytest)
self._fixturemanager = fixturemanager
# The "base" node ID for the fixture.
#
# This is a node ID prefix. A fixture is only available to a node (e.g.
Expand All @@ -1010,7 +1009,7 @@ def __init__(
if scope is None:
scope = Scope.Function
elif callable(scope):
scope = _eval_scope_callable(scope, argname, fixturemanager.config)
scope = _eval_scope_callable(scope, argname, config)
if isinstance(scope, str):
scope = Scope.from_user(
scope, descr=f"Fixture '{func.__name__}'", where=baseid
Expand Down Expand Up @@ -1439,9 +1438,6 @@ class FixtureManager:
by a lookup of their FuncFixtureInfo.
"""

FixtureLookupError = FixtureLookupError
FixtureLookupErrorRepr = FixtureLookupErrorRepr

def __init__(self, session: "Session") -> None:
self.session = session
self.config: Config = session.config
Expand Down Expand Up @@ -1657,7 +1653,7 @@ def _register_fixture(
Set this if this is a unittest fixture.
"""
fixture_def = FixtureDef(
fixturemanager=self,
config=self.config,
baseid=nodeid,
argname=name,
func=func,
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ def parametrize(
fixturedef = name2pseudofixturedef[argname]
else:
fixturedef = FixtureDef(
fixturemanager=self.definition.session._fixturemanager,
config=self.config,
baseid="",
argname=argname,
func=get_direct_param_fixture_func,
Expand Down
15 changes: 9 additions & 6 deletions src/_pytest/setuponly.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,23 @@ def pytest_fixture_setup(
else:
param = request.param
fixturedef.cached_param = param # type: ignore[attr-defined]
_show_fixture_action(fixturedef, "SETUP")
_show_fixture_action(fixturedef, request.config, "SETUP")


def pytest_fixture_post_finalizer(fixturedef: FixtureDef[object]) -> None:
def pytest_fixture_post_finalizer(
fixturedef: FixtureDef[object], request: SubRequest
) -> None:
if fixturedef.cached_result is not None:
config = fixturedef._fixturemanager.config
config = request.config
if config.option.setupshow:
_show_fixture_action(fixturedef, "TEARDOWN")
_show_fixture_action(fixturedef, request.config, "TEARDOWN")
if hasattr(fixturedef, "cached_param"):
del fixturedef.cached_param # type: ignore[attr-defined]


def _show_fixture_action(fixturedef: FixtureDef[object], msg: str) -> None:
config = fixturedef._fixturemanager.config
def _show_fixture_action(
fixturedef: FixtureDef[object], config: Config, msg: str
) -> None:
capman = config.pluginmanager.getplugin("capturemanager")
if capman:
capman.suspend_global_capture()
Expand Down

0 comments on commit b1c4308

Please sign in to comment.