1- import sys
21import inspect
32import warnings
4- from .callers import _MultiCall , HookCallError , _raise_wrapfail
3+ from .callers import _MultiCall , HookCallError , _raise_wrapfail , _Result
54
65__version__ = '0.5.0'
76
87__all__ = ["PluginManager" , "PluginValidationError" , "HookCallError" ,
98 "HookspecMarker" , "HookimplMarker" ]
109
11- _py3 = sys .version_info > (3 , 0 )
12-
1310
1411class PluginValidationError (Exception ):
1512 """ plugin failed validation. """
@@ -83,7 +80,7 @@ def __call__(self, function=None, hookwrapper=False, optionalhook=False,
8380 If hookwrapper is True the hook implementations needs to execute exactly
8481 one "yield". The code before the yield is run early before any non-hookwrapper
8582 function is run. The code after the yield is run after all non-hookwrapper
86- function have run. The yield receives an ``_CallOutcome `` object representing
83+ function have run. The yield receives a ``_Result `` object representing
8784 the exception or result outcome of the inner calls (including other hookwrapper
8885 calls).
8986
@@ -172,14 +169,14 @@ def get(self, name):
172169def _wrapped_call (wrap_controller , func ):
173170 """ Wrap calling to a function with a generator which needs to yield
174171 exactly once. The yield point will trigger calling the wrapped function
175- and return its _CallOutcome to the yield point. The generator then needs
172+ and return its ``_Result`` to the yield point. The generator then needs
176173 to finish (raise StopIteration) in order for the wrapped call to complete.
177174 """
178175 try :
179176 next (wrap_controller ) # first yield
180177 except StopIteration :
181178 _raise_wrapfail (wrap_controller , "did not yield" )
182- call_outcome = _CallOutcome (func )
179+ call_outcome = _Result . from_call (func )
183180 try :
184181 wrap_controller .send (call_outcome )
185182 _raise_wrapfail (wrap_controller , "has second yield" )
@@ -188,39 +185,6 @@ def _wrapped_call(wrap_controller, func):
188185 return call_outcome .get_result ()
189186
190187
191- class _CallOutcome (object ):
192- """ Outcome of a function call, either an exception or a proper result.
193- Calling the ``get_result`` method will return the result or reraise
194- the exception raised when the function was called. """
195- excinfo = None
196-
197- def __init__ (self , func ):
198- try :
199- self .result = func ()
200- except BaseException :
201- self .excinfo = sys .exc_info ()
202-
203- def force_result (self , result ):
204- self .result = result
205- self .excinfo = None
206-
207- def get_result (self ):
208- if self .excinfo is None :
209- return self .result
210- else :
211- ex = self .excinfo
212- if _py3 :
213- raise ex [1 ].with_traceback (ex [2 ])
214- _reraise (* ex ) # noqa
215-
216-
217- if not _py3 :
218- exec ("""
219- def _reraise(cls, val, tb):
220- raise cls, val, tb
221- """ )
222-
223-
224188class _TracedHookExecution (object ):
225189 def __init__ (self , pluginmanager , before , after ):
226190 self .pluginmanager = pluginmanager
@@ -232,7 +196,7 @@ def __init__(self, pluginmanager, before, after):
232196
233197 def __call__ (self , hook , hook_impls , kwargs ):
234198 self .before (hook .name , hook_impls , kwargs )
235- outcome = _CallOutcome (lambda : self .oldcall (hook , hook_impls , kwargs ))
199+ outcome = _Result . from_call (lambda : self .oldcall (hook , hook_impls , kwargs ))
236200 self .after (outcome , hook .name , hook_impls , kwargs )
237201 return outcome .get_result ()
238202
@@ -481,7 +445,7 @@ def add_hookcall_monitoring(self, before, after):
481445 of HookImpl instances and the keyword arguments for the hook call.
482446
483447 ``after(outcome, hook_name, hook_impls, kwargs)`` receives the
484- same arguments as ``before`` but also a :py:class:`_CallOutcome `` object
448+ same arguments as ``before`` but also a :py:class:`_Result `` object
485449 which represents the result of the overall hook call.
486450 """
487451 return _TracedHookExecution (self , before , after ).undo
0 commit comments