Skip to content

Commit 9d0ddb4

Browse files
committed
fixtures: change FixtureDef.cached_result[2] from exception triplet to exception
Fix #11208.
1 parent 01f38ac commit 9d0ddb4

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

changelog/11208.trivial.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The (internal) ``FixtureDef.cached_result`` type has changed.
2+
Now the third item ``cached_result[2]``, when set, is an exception instance instead of an exception triplet.

src/_pytest/fixtures.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import functools
33
import inspect
44
import os
5-
import sys
65
import warnings
76
from collections import defaultdict
87
from collections import deque
98
from contextlib import suppress
109
from pathlib import Path
11-
from types import TracebackType
1210
from typing import Any
1311
from typing import Callable
1412
from typing import cast
@@ -27,7 +25,6 @@
2725
from typing import Sequence
2826
from typing import Set
2927
from typing import Tuple
30-
from typing import Type
3128
from typing import TYPE_CHECKING
3229
from typing import TypeVar
3330
from typing import Union
@@ -99,8 +96,8 @@
9996
None,
10097
# Cache key.
10198
object,
102-
# Exc info if raised.
103-
Tuple[Type[BaseException], BaseException, TracebackType],
99+
# Exception if raised.
100+
BaseException,
104101
],
105102
]
106103

@@ -1085,13 +1082,13 @@ def execute(self, request: SubRequest) -> FixtureValue:
10851082

10861083
my_cache_key = self.cache_key(request)
10871084
if self.cached_result is not None:
1085+
cache_key = self.cached_result[1]
10881086
# note: comparison with `==` can fail (or be expensive) for e.g.
10891087
# numpy arrays (#6497).
1090-
cache_key = self.cached_result[1]
10911088
if my_cache_key is cache_key:
10921089
if self.cached_result[2] is not None:
1093-
_, val, tb = self.cached_result[2]
1094-
raise val.with_traceback(tb)
1090+
exc = self.cached_result[2]
1091+
raise exc
10951092
else:
10961093
result = self.cached_result[0]
10971094
return result
@@ -1156,14 +1153,12 @@ def pytest_fixture_setup(
11561153
my_cache_key = fixturedef.cache_key(request)
11571154
try:
11581155
result = call_fixture_func(fixturefunc, request, kwargs)
1159-
except TEST_OUTCOME:
1160-
exc_info = sys.exc_info()
1161-
assert exc_info[0] is not None
1162-
if isinstance(
1163-
exc_info[1], skip.Exception
1164-
) and not fixturefunc.__name__.startswith("xunit_setup"):
1165-
exc_info[1]._use_item_location = True # type: ignore[attr-defined]
1166-
fixturedef.cached_result = (None, my_cache_key, exc_info)
1156+
except TEST_OUTCOME as e:
1157+
if isinstance(e, skip.Exception) and not fixturefunc.__name__.startswith(
1158+
"xunit_setup"
1159+
):
1160+
e._use_item_location = True
1161+
fixturedef.cached_result = (None, my_cache_key, e)
11671162
raise
11681163
fixturedef.cached_result = (result, my_cache_key, None)
11691164
return result

0 commit comments

Comments
 (0)