Skip to content

Commit 88f1b73

Browse files
author
Tyler Goodlet
committed
Remove _CallOutcome; embrace _Result
`_CallOutcome` limits usage due the constructor calling a function input. Instead add a `classmethod` constructor `_Result.from_call()` which can be used to get the same behaviour and avoids duplicate types.
1 parent 21fd6c4 commit 88f1b73

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

pluggy/__init__.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import sys
21
import inspect
32
import 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

1411
class 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):
172169
def _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-
224188
class _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

pluggy/callers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@ class HookCallError(Exception):
2424
""" Hook was called wrongly. """
2525

2626

27-
class Result(object):
27+
class _Result(object):
2828
def __init__(self, result, excinfo):
2929
self.result = result
3030
self.excinfo = excinfo
3131

32+
@classmethod
33+
def from_call(cls, func):
34+
result = excinfo = None
35+
try:
36+
result = func()
37+
except BaseException:
38+
excinfo = sys.exc_info()
39+
40+
return cls(result, excinfo)
41+
3242
def force_result(self, result):
3343
self.result = result
3444
self.excinfo = None
@@ -86,7 +96,7 @@ def execute(self):
8696
except BaseException:
8797
excinfo = sys.exc_info()
8898
finally:
89-
outcome = Result(results, excinfo)
99+
outcome = _Result(results, excinfo)
90100

91101
# run all wrapper post-yield blocks
92102
for gen in reversed(teardowns):

0 commit comments

Comments
 (0)