fixtures: change `FixtureDef.cached_result[2]` from exception triplet to exception

Fix #11208.
This commit is contained in:
Ran Benita 2023-07-14 10:35:18 +03:00
parent 01f38aca44
commit 9d0ddb4625
2 changed files with 13 additions and 16 deletions

View File

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

View File

@ -2,13 +2,11 @@ import dataclasses
import functools import functools
import inspect import inspect
import os import os
import sys
import warnings import warnings
from collections import defaultdict from collections import defaultdict
from collections import deque from collections import deque
from contextlib import suppress from contextlib import suppress
from pathlib import Path from pathlib import Path
from types import TracebackType
from typing import Any from typing import Any
from typing import Callable from typing import Callable
from typing import cast from typing import cast
@ -27,7 +25,6 @@ from typing import overload
from typing import Sequence from typing import Sequence
from typing import Set from typing import Set
from typing import Tuple from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import TypeVar from typing import TypeVar
from typing import Union from typing import Union
@ -99,8 +96,8 @@ _FixtureCachedResult = Union[
None, None,
# Cache key. # Cache key.
object, object,
# Exc info if raised. # Exception if raised.
Tuple[Type[BaseException], BaseException, TracebackType], BaseException,
], ],
] ]
@ -1085,13 +1082,13 @@ class FixtureDef(Generic[FixtureValue]):
my_cache_key = self.cache_key(request) my_cache_key = self.cache_key(request)
if self.cached_result is not None: if self.cached_result is not None:
cache_key = self.cached_result[1]
# note: comparison with `==` can fail (or be expensive) for e.g. # note: comparison with `==` can fail (or be expensive) for e.g.
# numpy arrays (#6497). # numpy arrays (#6497).
cache_key = self.cached_result[1]
if my_cache_key is cache_key: if my_cache_key is cache_key:
if self.cached_result[2] is not None: if self.cached_result[2] is not None:
_, val, tb = self.cached_result[2] exc = self.cached_result[2]
raise val.with_traceback(tb) raise exc
else: else:
result = self.cached_result[0] result = self.cached_result[0]
return result return result
@ -1156,14 +1153,12 @@ def pytest_fixture_setup(
my_cache_key = fixturedef.cache_key(request) my_cache_key = fixturedef.cache_key(request)
try: try:
result = call_fixture_func(fixturefunc, request, kwargs) result = call_fixture_func(fixturefunc, request, kwargs)
except TEST_OUTCOME: except TEST_OUTCOME as e:
exc_info = sys.exc_info() if isinstance(e, skip.Exception) and not fixturefunc.__name__.startswith(
assert exc_info[0] is not None "xunit_setup"
if isinstance( ):
exc_info[1], skip.Exception e._use_item_location = True
) and not fixturefunc.__name__.startswith("xunit_setup"): fixturedef.cached_result = (None, my_cache_key, e)
exc_info[1]._use_item_location = True # type: ignore[attr-defined]
fixturedef.cached_result = (None, my_cache_key, exc_info)
raise raise
fixturedef.cached_result = (result, my_cache_key, None) fixturedef.cached_result = (result, my_cache_key, None)
return result return result