Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pluggy/_result.py: 54%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Hook wrapper "result" utilities.
3"""
5from __future__ import annotations
7from types import TracebackType
8from typing import Callable
9from typing import cast
10from typing import final
11from typing import Generic
12from typing import Optional
13from typing import Tuple
14from typing import Type
15from typing import TypeVar
18_ExcInfo = Tuple[Type[BaseException], BaseException, Optional[TracebackType]]
19ResultType = TypeVar("ResultType")
22class HookCallError(Exception):
23 """Hook was called incorrectly."""
26@final
27class Result(Generic[ResultType]):
28 """An object used to inspect and set the result in a :ref:`hook wrapper
29 <hookwrappers>`."""
31 __slots__ = ("_result", "_exception")
33 def __init__(
34 self,
35 result: ResultType | None,
36 exception: BaseException | None,
37 ) -> None:
38 """:meta private:"""
39 self._result = result
40 self._exception = exception
42 @property
43 def excinfo(self) -> _ExcInfo | None:
44 """:meta private:"""
45 exc = self._exception
46 if exc is None:
47 return None
48 else:
49 return (type(exc), exc, exc.__traceback__)
51 @property
52 def exception(self) -> BaseException | None:
53 """:meta private:"""
54 return self._exception
56 @classmethod
57 def from_call(cls, func: Callable[[], ResultType]) -> Result[ResultType]:
58 """:meta private:"""
59 __tracebackhide__ = True
60 result = exception = None
61 try:
62 result = func()
63 except BaseException as exc:
64 exception = exc
65 return cls(result, exception)
67 def force_result(self, result: ResultType) -> None:
68 """Force the result(s) to ``result``.
70 If the hook was marked as a ``firstresult`` a single value should
71 be set, otherwise set a (modified) list of results. Any exceptions
72 found during invocation will be deleted.
74 This overrides any previous result or exception.
75 """
76 self._result = result
77 self._exception = None
79 def force_exception(self, exception: BaseException) -> None:
80 """Force the result to fail with ``exception``.
82 This overrides any previous result or exception.
84 .. versionadded:: 1.1.0
85 """
86 self._result = None
87 self._exception = exception
89 def get_result(self) -> ResultType:
90 """Get the result(s) for this hook call.
92 If the hook was marked as a ``firstresult`` only a single value
93 will be returned, otherwise a list of results.
94 """
95 __tracebackhide__ = True
96 exc = self._exception
97 if exc is None:
98 return cast(ResultType, self._result)
99 else:
100 raise exc.with_traceback(exc.__traceback__)
103# Historical name (pluggy<=1.2), kept for backward compatibility.
104_Result = Result