From 393309ed43fbf852a698fe7dcd763d282a761590 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 4 Jun 2020 15:32:57 -0400 Subject: [PATCH 1/2] Move wrapper "result" types into new module --- src/pluggy/_result.py | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/pluggy/_result.py diff --git a/src/pluggy/_result.py b/src/pluggy/_result.py new file mode 100644 index 00000000..54538f35 --- /dev/null +++ b/src/pluggy/_result.py @@ -0,0 +1,81 @@ +""" +Hook wrapper "result" utilities. +""" +import sys +import warnings + +_py3 = sys.version_info > (3, 0) + + +if not _py3: + exec( + """ +def _reraise(cls, val, tb): + raise cls, val, tb +""" + ) + + +def _raise_wrapfail(wrap_controller, msg): + co = wrap_controller.gi_code + raise RuntimeError( + "wrap_controller at %r %s:%d %s" + % (co.co_name, co.co_filename, co.co_firstlineno, msg) + ) + + +class HookCallError(Exception): + """ Hook was called wrongly. """ + + +class _Result(object): + def __init__(self, result, excinfo): + self._result = result + self._excinfo = excinfo + + @property + def excinfo(self): + return self._excinfo + + @property + def result(self): + """Get the result(s) for this hook call (DEPRECATED in favor of ``get_result()``).""" + msg = "Use get_result() which forces correct exception handling" + warnings.warn(DeprecationWarning(msg), stacklevel=2) + return self._result + + @classmethod + def from_call(cls, func): + __tracebackhide__ = True + result = excinfo = None + try: + result = func() + except BaseException: + excinfo = sys.exc_info() + + return cls(result, excinfo) + + def force_result(self, result): + """Force the result(s) to ``result``. + + If the hook was marked as a ``firstresult`` a single value should + be set otherwise set a (modified) list of results. Any exceptions + found during invocation will be deleted. + """ + self._result = result + self._excinfo = None + + def get_result(self): + """Get the result(s) for this hook call. + + If the hook was marked as a ``firstresult`` only a single value + will be returned otherwise a list of results. + """ + __tracebackhide__ = True + if self._excinfo is None: + return self._result + else: + ex = self._excinfo + if _py3: + raise ex[1].with_traceback(ex[2]) + _reraise(*ex) # noqa From a8afb703465444139de68f568ffc603521c6d2d6 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 4 Jun 2020 15:44:51 -0400 Subject: [PATCH 2/2] Port caller.py to new _result.py module --- src/pluggy/callers.py | 77 +------------------------------------------ 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/src/pluggy/callers.py b/src/pluggy/callers.py index d2977a1b..f66cc946 100644 --- a/src/pluggy/callers.py +++ b/src/pluggy/callers.py @@ -2,83 +2,8 @@ Call loop machinery """ import sys -import warnings -_py3 = sys.version_info > (3, 0) - - -if not _py3: - exec( - """ -def _reraise(cls, val, tb): - raise cls, val, tb -""" - ) - - -def _raise_wrapfail(wrap_controller, msg): - co = wrap_controller.gi_code - raise RuntimeError( - "wrap_controller at %r %s:%d %s" - % (co.co_name, co.co_filename, co.co_firstlineno, msg) - ) - - -class HookCallError(Exception): - """ Hook was called wrongly. """ - - -class _Result(object): - def __init__(self, result, excinfo): - self._result = result - self._excinfo = excinfo - - @property - def excinfo(self): - return self._excinfo - - @property - def result(self): - """Get the result(s) for this hook call (DEPRECATED in favor of ``get_result()``).""" - msg = "Use get_result() which forces correct exception handling" - warnings.warn(DeprecationWarning(msg), stacklevel=2) - return self._result - - @classmethod - def from_call(cls, func): - __tracebackhide__ = True - result = excinfo = None - try: - result = func() - except BaseException: - excinfo = sys.exc_info() - - return cls(result, excinfo) - - def force_result(self, result): - """Force the result(s) to ``result``. - - If the hook was marked as a ``firstresult`` a single value should - be set otherwise set a (modified) list of results. Any exceptions - found during invocation will be deleted. - """ - self._result = result - self._excinfo = None - - def get_result(self): - """Get the result(s) for this hook call. - - If the hook was marked as a ``firstresult`` only a single value - will be returned otherwise a list of results. - """ - __tracebackhide__ = True - if self._excinfo is None: - return self._result - else: - ex = self._excinfo - if _py3: - raise ex[1].with_traceback(ex[2]) - _reraise(*ex) # noqa +from ._result import HookCallError, _Result, _raise_wrapfail def _multicall(hook_impls, caller_kwargs, firstresult=False):