From fbc79e2622ff3af5e618cf63c5f6731f10f7be88 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 18 Apr 2020 13:36:41 +0300 Subject: [PATCH] Remove useless try in _multicall In Python 3, every raisable thing inherits from BaseException, so the `except BaseException` in the code already handles every possible exit. So we can reduce the indentation and the slight overhead of `try`. Diff better viewed with `git diff -w`. --- src/pluggy/callers.py | 83 ++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/src/pluggy/callers.py b/src/pluggy/callers.py index f66cc946..1fff95ec 100644 --- a/src/pluggy/callers.py +++ b/src/pluggy/callers.py @@ -15,46 +15,47 @@ def _multicall(hook_impls, caller_kwargs, firstresult=False): __tracebackhide__ = True results = [] excinfo = None - try: # run impl and wrapper setup functions in a loop - teardowns = [] - try: - for hook_impl in reversed(hook_impls): - try: - args = [caller_kwargs[argname] for argname in hook_impl.argnames] - except KeyError: - for argname in hook_impl.argnames: - if argname not in caller_kwargs: - raise HookCallError( - "hook call must provide argument %r" % (argname,) - ) - - if hook_impl.hookwrapper: - try: - gen = hook_impl.function(*args) - next(gen) # first yield - teardowns.append(gen) - except StopIteration: - _raise_wrapfail(gen, "did not yield") - else: - res = hook_impl.function(*args) - if res is not None: - results.append(res) - if firstresult: # halt further impl calls - break - except BaseException: - excinfo = sys.exc_info() - finally: - if firstresult: # first result hooks return a single value - outcome = _Result(results[0] if results else None, excinfo) - else: - outcome = _Result(results, excinfo) - - # run all wrapper post-yield blocks - for gen in reversed(teardowns): + + # run impl and wrapper setup functions in a loop + teardowns = [] + try: + for hook_impl in reversed(hook_impls): try: - gen.send(outcome) - _raise_wrapfail(gen, "has second yield") - except StopIteration: - pass + args = [caller_kwargs[argname] for argname in hook_impl.argnames] + except KeyError: + for argname in hook_impl.argnames: + if argname not in caller_kwargs: + raise HookCallError( + "hook call must provide argument %r" % (argname,) + ) + + if hook_impl.hookwrapper: + try: + gen = hook_impl.function(*args) + next(gen) # first yield + teardowns.append(gen) + except StopIteration: + _raise_wrapfail(gen, "did not yield") + else: + res = hook_impl.function(*args) + if res is not None: + results.append(res) + if firstresult: # halt further impl calls + break + except BaseException: + excinfo = sys.exc_info() + + if firstresult: # first result hooks return a single value + outcome = _Result(results[0] if results else None, excinfo) + else: + outcome = _Result(results, excinfo) + + # run all wrapper post-yield blocks + for gen in reversed(teardowns): + try: + gen.send(outcome) + _raise_wrapfail(gen, "has second yield") + except StopIteration: + pass - return outcome.get_result() + return outcome.get_result()