1
- import sys
2
1
import inspect
3
2
import warnings
4
- from .callers import _MultiCall , HookCallError , _raise_wrapfail
3
+ from .callers import _MultiCall , HookCallError , _raise_wrapfail , _Result
5
4
6
5
__version__ = '0.5.0'
7
6
8
7
__all__ = ["PluginManager" , "PluginValidationError" , "HookCallError" ,
9
8
"HookspecMarker" , "HookimplMarker" ]
10
9
11
- _py3 = sys .version_info > (3 , 0 )
12
-
13
10
14
11
class PluginValidationError (Exception ):
15
12
""" plugin failed validation. """
@@ -83,7 +80,7 @@ def __call__(self, function=None, hookwrapper=False, optionalhook=False,
83
80
If hookwrapper is True the hook implementations needs to execute exactly
84
81
one "yield". The code before the yield is run early before any non-hookwrapper
85
82
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
87
84
the exception or result outcome of the inner calls (including other hookwrapper
88
85
calls).
89
86
@@ -172,14 +169,14 @@ def get(self, name):
172
169
def _wrapped_call (wrap_controller , func ):
173
170
""" Wrap calling to a function with a generator which needs to yield
174
171
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
176
173
to finish (raise StopIteration) in order for the wrapped call to complete.
177
174
"""
178
175
try :
179
176
next (wrap_controller ) # first yield
180
177
except StopIteration :
181
178
_raise_wrapfail (wrap_controller , "did not yield" )
182
- call_outcome = _CallOutcome (func )
179
+ call_outcome = _Result . from_call (func )
183
180
try :
184
181
wrap_controller .send (call_outcome )
185
182
_raise_wrapfail (wrap_controller , "has second yield" )
@@ -188,39 +185,6 @@ def _wrapped_call(wrap_controller, func):
188
185
return call_outcome .get_result ()
189
186
190
187
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
-
224
188
class _TracedHookExecution (object ):
225
189
def __init__ (self , pluginmanager , before , after ):
226
190
self .pluginmanager = pluginmanager
@@ -232,7 +196,7 @@ def __init__(self, pluginmanager, before, after):
232
196
233
197
def __call__ (self , hook , hook_impls , kwargs ):
234
198
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 ))
236
200
self .after (outcome , hook .name , hook_impls , kwargs )
237
201
return outcome .get_result ()
238
202
@@ -481,7 +445,7 @@ def add_hookcall_monitoring(self, before, after):
481
445
of HookImpl instances and the keyword arguments for the hook call.
482
446
483
447
``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
485
449
which represents the result of the overall hook call.
486
450
"""
487
451
return _TracedHookExecution (self , before , after ).undo
0 commit comments