Skip to content

Commit dee4fd9

Browse files
author
Tyler Goodlet
committed
Move legacy multicall components to callers module
1 parent a62cff6 commit dee4fd9

File tree

2 files changed

+68
-68
lines changed

2 files changed

+68
-68
lines changed

pluggy/__init__.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22
import warnings
3-
from .callers import _MultiCall, HookCallError, _raise_wrapfail, _Result
3+
from .callers import _MultiCall, HookCallError, _Result, _LegacyMultiCall
44

55
__version__ = '0.5.3.dev'
66

@@ -166,25 +166,6 @@ def get(self, name):
166166
return self.__class__(self.root, self.tags + (name,))
167167

168168

169-
def _wrapped_call(wrap_controller, func):
170-
""" Wrap calling to a function with a generator which needs to yield
171-
exactly once. The yield point will trigger calling the wrapped function
172-
and return its ``_Result`` to the yield point. The generator then needs
173-
to finish (raise StopIteration) in order for the wrapped call to complete.
174-
"""
175-
try:
176-
next(wrap_controller) # first yield
177-
except StopIteration:
178-
_raise_wrapfail(wrap_controller, "did not yield")
179-
call_outcome = _Result.from_call(func)
180-
try:
181-
wrap_controller.send(call_outcome)
182-
_raise_wrapfail(wrap_controller, "has second yield")
183-
except StopIteration:
184-
pass
185-
return call_outcome.get_result()
186-
187-
188169
class _TracedHookExecution(object):
189170
def __init__(self, pluginmanager, before, after):
190171
self.pluginmanager = pluginmanager
@@ -485,54 +466,6 @@ def subset_hook_caller(self, name, remove_plugins):
485466
return orig
486467

487468

488-
class _LegacyMultiCall(object):
489-
""" execute a call into multiple python functions/methods. """
490-
491-
# XXX note that the __multicall__ argument is supported only
492-
# for pytest compatibility reasons. It was never officially
493-
# supported there and is explicitely deprecated since 2.8
494-
# so we can remove it soon, allowing to avoid the below recursion
495-
# in execute() and simplify/speed up the execute loop.
496-
497-
def __init__(self, hook_impls, kwargs, specopts={}, hook=None):
498-
self.hook = hook
499-
self.hook_impls = hook_impls
500-
self.caller_kwargs = kwargs # come from _HookCaller.__call__()
501-
self.caller_kwargs["__multicall__"] = self
502-
self.specopts = hook.spec_opts if hook else specopts
503-
504-
def execute(self):
505-
caller_kwargs = self.caller_kwargs
506-
self.results = results = []
507-
firstresult = self.specopts.get("firstresult")
508-
509-
while self.hook_impls:
510-
hook_impl = self.hook_impls.pop()
511-
try:
512-
args = [caller_kwargs[argname] for argname in hook_impl.argnames]
513-
except KeyError:
514-
for argname in hook_impl.argnames:
515-
if argname not in caller_kwargs:
516-
raise HookCallError(
517-
"hook call must provide argument %r" % (argname,))
518-
if hook_impl.hookwrapper:
519-
return _wrapped_call(hook_impl.function(*args), self.execute)
520-
res = hook_impl.function(*args)
521-
if res is not None:
522-
if firstresult:
523-
return res
524-
results.append(res)
525-
526-
if not firstresult:
527-
return results
528-
529-
def __repr__(self):
530-
status = "%d meths" % (len(self.hook_impls),)
531-
if hasattr(self, "results"):
532-
status = ("%d results, " % len(self.results)) + status
533-
return "<_MultiCall %s, kwargs=%r>" % (status, self.caller_kwargs)
534-
535-
536469
def varnames(func):
537470
"""Return tuple of positional and keywrord argument names for a function,
538471
method, class or callable.

pluggy/callers.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,73 @@ def get_result(self):
7777
_reraise(*ex) # noqa
7878

7979

80+
def _wrapped_call(wrap_controller, func):
81+
""" Wrap calling to a function with a generator which needs to yield
82+
exactly once. The yield point will trigger calling the wrapped function
83+
and return its ``_Result`` to the yield point. The generator then needs
84+
to finish (raise StopIteration) in order for the wrapped call to complete.
85+
"""
86+
try:
87+
next(wrap_controller) # first yield
88+
except StopIteration:
89+
_raise_wrapfail(wrap_controller, "did not yield")
90+
call_outcome = _Result.from_call(func)
91+
try:
92+
wrap_controller.send(call_outcome)
93+
_raise_wrapfail(wrap_controller, "has second yield")
94+
except StopIteration:
95+
pass
96+
return call_outcome.get_result()
97+
98+
99+
class _LegacyMultiCall(object):
100+
""" execute a call into multiple python functions/methods. """
101+
102+
# XXX note that the __multicall__ argument is supported only
103+
# for pytest compatibility reasons. It was never officially
104+
# supported there and is explicitely deprecated since 2.8
105+
# so we can remove it soon, allowing to avoid the below recursion
106+
# in execute() and simplify/speed up the execute loop.
107+
108+
def __init__(self, hook_impls, kwargs, specopts={}, hook=None):
109+
self.hook = hook
110+
self.hook_impls = hook_impls
111+
self.caller_kwargs = kwargs # come from _HookCaller.__call__()
112+
self.caller_kwargs["__multicall__"] = self
113+
self.specopts = hook.spec_opts if hook else specopts
114+
115+
def execute(self):
116+
caller_kwargs = self.caller_kwargs
117+
self.results = results = []
118+
firstresult = self.specopts.get("firstresult")
119+
120+
while self.hook_impls:
121+
hook_impl = self.hook_impls.pop()
122+
try:
123+
args = [caller_kwargs[argname] for argname in hook_impl.argnames]
124+
except KeyError:
125+
for argname in hook_impl.argnames:
126+
if argname not in caller_kwargs:
127+
raise HookCallError(
128+
"hook call must provide argument %r" % (argname,))
129+
if hook_impl.hookwrapper:
130+
return _wrapped_call(hook_impl.function(*args), self.execute)
131+
res = hook_impl.function(*args)
132+
if res is not None:
133+
if firstresult:
134+
return res
135+
results.append(res)
136+
137+
if not firstresult:
138+
return results
139+
140+
def __repr__(self):
141+
status = "%d meths" % (len(self.hook_impls),)
142+
if hasattr(self, "results"):
143+
status = ("%d results, " % len(self.results)) + status
144+
return "<_MultiCall %s, kwargs=%r>" % (status, self.caller_kwargs)
145+
146+
80147
class _MultiCall(object):
81148
"""Execute a call into multiple python functions/methods.
82149
"""

0 commit comments

Comments
 (0)