1"""

2 Hook wrapper "result" utilities.

3"""

4 import sys

5

6

7 def _raise_wrapfail(wrap_controller, msg):

8 co = wrap_controller.gi_code

9 raise RuntimeError(

10 "wrap_controller at %r %s:%d %s"

11 % (co.co_name, co.co_filename, co.co_firstlineno, msg)

12 )

13

14

15 class HookCallError(Exception):

16 """Hook was called wrongly."""

17

18

19 class _Result:

20 def __init__(self, result, excinfo):

21 self._result = result

22 self._excinfo = excinfo

23

24 @property

25 def excinfo(self):

26 return self._excinfo

27

28 @classmethod

29 def from_call(cls, func):

30 __tracebackhide__ = True

31 result = excinfo = None

32 try:

33 result = func()

34 except BaseException:

35 excinfo = sys.exc_info()

36

37 return cls(result, excinfo)

38

39 def force_result(self, result):

40 """Force the result(s) to ``result``.

41

42 If the hook was marked as a ``firstresult`` a single value should

43 be set otherwise set a (modified) list of results. Any exceptions

44 found during invocation will be deleted.

45 """

46 self._result = result

47 self._excinfo = None

48

49 def get_result(self):

50 """Get the result(s) for this hook call.

51

52 If the hook was marked as a ``firstresult`` only a single value

53 will be returned otherwise a list of results.

54 """

55 __tracebackhide__ = True

56 if self._excinfo is None:

57 return self._result

58 else:

59 ex = self._excinfo

60 raise ex[1].with_traceback(ex[2])