_hooks.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. """
  2. Internal hook annotation, representation and calling machinery.
  3. """
  4. from __future__ import annotations
  5. import inspect
  6. import sys
  7. import warnings
  8. from types import ModuleType
  9. from typing import AbstractSet
  10. from typing import Any
  11. from typing import Callable
  12. from typing import Generator
  13. from typing import List
  14. from typing import Mapping
  15. from typing import Optional
  16. from typing import overload
  17. from typing import Sequence
  18. from typing import Tuple
  19. from typing import TYPE_CHECKING
  20. from typing import TypeVar
  21. from typing import Union
  22. from ._result import _Result
  23. if TYPE_CHECKING:
  24. from typing_extensions import TypedDict
  25. from typing_extensions import Final
  26. _T = TypeVar("_T")
  27. _F = TypeVar("_F", bound=Callable[..., object])
  28. _Namespace = Union[ModuleType, type]
  29. _Plugin = object
  30. _HookExec = Callable[
  31. [str, Sequence["HookImpl"], Mapping[str, object], bool],
  32. Union[object, List[object]],
  33. ]
  34. _HookImplFunction = Callable[..., Union[_T, Generator[None, _Result[_T], None]]]
  35. if TYPE_CHECKING:
  36. class _HookSpecOpts(TypedDict):
  37. firstresult: bool
  38. historic: bool
  39. warn_on_impl: Warning | None
  40. class _HookImplOpts(TypedDict):
  41. wrapper: bool
  42. hookwrapper: bool
  43. optionalhook: bool
  44. tryfirst: bool
  45. trylast: bool
  46. specname: str | None
  47. class HookspecMarker:
  48. """Decorator for marking functions as hook specifications.
  49. Instantiate it with a project_name to get a decorator.
  50. Calling :meth:`PluginManager.add_hookspecs` later will discover all marked
  51. functions if the :class:`PluginManager` uses the same project_name.
  52. """
  53. __slots__ = ("project_name",)
  54. def __init__(self, project_name: str) -> None:
  55. self.project_name: Final = project_name
  56. @overload
  57. def __call__(
  58. self,
  59. function: _F,
  60. firstresult: bool = False,
  61. historic: bool = False,
  62. warn_on_impl: Warning | None = None,
  63. ) -> _F:
  64. ...
  65. @overload # noqa: F811
  66. def __call__( # noqa: F811
  67. self,
  68. function: None = ...,
  69. firstresult: bool = ...,
  70. historic: bool = ...,
  71. warn_on_impl: Warning | None = ...,
  72. ) -> Callable[[_F], _F]:
  73. ...
  74. def __call__( # noqa: F811
  75. self,
  76. function: _F | None = None,
  77. firstresult: bool = False,
  78. historic: bool = False,
  79. warn_on_impl: Warning | None = None,
  80. ) -> _F | Callable[[_F], _F]:
  81. """If passed a function, directly sets attributes on the function
  82. which will make it discoverable to :meth:`PluginManager.add_hookspecs`.
  83. If passed no function, returns a decorator which can be applied to a
  84. function later using the attributes supplied.
  85. If ``firstresult`` is ``True``, the 1:N hook call (N being the number of
  86. registered hook implementation functions) will stop at I<=N when the
  87. I'th function returns a non-``None`` result.
  88. If ``historic`` is ``True``, every call to the hook will be memorized
  89. and replayed on plugins registered after the call was made.
  90. """
  91. def setattr_hookspec_opts(func: _F) -> _F:
  92. if historic and firstresult:
  93. raise ValueError("cannot have a historic firstresult hook")
  94. opts: _HookSpecOpts = {
  95. "firstresult": firstresult,
  96. "historic": historic,
  97. "warn_on_impl": warn_on_impl,
  98. }
  99. setattr(func, self.project_name + "_spec", opts)
  100. return func
  101. if function is not None:
  102. return setattr_hookspec_opts(function)
  103. else:
  104. return setattr_hookspec_opts
  105. class HookimplMarker:
  106. """Decorator for marking functions as hook implementations.
  107. Instantiate it with a ``project_name`` to get a decorator.
  108. Calling :meth:`PluginManager.register` later will discover all marked
  109. functions if the :class:`PluginManager` uses the same project_name.
  110. """
  111. __slots__ = ("project_name",)
  112. def __init__(self, project_name: str) -> None:
  113. self.project_name: Final = project_name
  114. @overload
  115. def __call__(
  116. self,
  117. function: _F,
  118. hookwrapper: bool = ...,
  119. optionalhook: bool = ...,
  120. tryfirst: bool = ...,
  121. trylast: bool = ...,
  122. specname: str | None = ...,
  123. wrapper: bool = ...,
  124. ) -> _F:
  125. ...
  126. @overload # noqa: F811
  127. def __call__( # noqa: F811
  128. self,
  129. function: None = ...,
  130. hookwrapper: bool = ...,
  131. optionalhook: bool = ...,
  132. tryfirst: bool = ...,
  133. trylast: bool = ...,
  134. specname: str | None = ...,
  135. wrapper: bool = ...,
  136. ) -> Callable[[_F], _F]:
  137. ...
  138. def __call__( # noqa: F811
  139. self,
  140. function: _F | None = None,
  141. hookwrapper: bool = False,
  142. optionalhook: bool = False,
  143. tryfirst: bool = False,
  144. trylast: bool = False,
  145. specname: str | None = None,
  146. wrapper: bool = False,
  147. ) -> _F | Callable[[_F], _F]:
  148. """If passed a function, directly sets attributes on the function
  149. which will make it discoverable to :meth:`PluginManager.register`.
  150. If passed no function, returns a decorator which can be applied to a
  151. function later using the attributes supplied.
  152. If ``optionalhook`` is ``True``, a missing matching hook specification
  153. will not result in an error (by default it is an error if no matching
  154. spec is found).
  155. If ``tryfirst`` is ``True``, this hook implementation will run as early
  156. as possible in the chain of N hook implementations for a specification.
  157. If ``trylast`` is ``True``, this hook implementation will run as late as
  158. possible in the chain of N hook implementations.
  159. If ``wrapper`` is ``True``("new-style hook wrapper"), the hook
  160. implementation needs to execute exactly one ``yield``. The code before
  161. the ``yield`` is run early before any non-hook-wrapper function is run.
  162. The code after the ``yield`` is run after all non-hook-wrapper functions
  163. have run. The ``yield`` receives the result value of the inner calls, or
  164. raises the exception of inner calls (including earlier hook wrapper
  165. calls). The return value of the function becomes the return value of the
  166. hook, and a raised exception becomes the exception of the hook.
  167. If ``hookwrapper`` is ``True`` ("old-style hook wrapper"), the hook
  168. implementation needs to execute exactly one ``yield``. The code before
  169. the ``yield`` is run early before any non-hook-wrapper function is run.
  170. The code after the ``yield`` is run after all non-hook-wrapper function
  171. have run The ``yield`` receives a :class:`_Result` object representing
  172. the exception or result outcome of the inner calls (including earlier
  173. hook wrapper calls). This option is mutually exclusive with ``wrapper``.
  174. If ``specname`` is provided, it will be used instead of the function
  175. name when matching this hook implementation to a hook specification
  176. during registration.
  177. .. versionadded:: 1.2.0
  178. The ``wrapper`` parameter.
  179. """
  180. def setattr_hookimpl_opts(func: _F) -> _F:
  181. opts: _HookImplOpts = {
  182. "wrapper": wrapper,
  183. "hookwrapper": hookwrapper,
  184. "optionalhook": optionalhook,
  185. "tryfirst": tryfirst,
  186. "trylast": trylast,
  187. "specname": specname,
  188. }
  189. setattr(func, self.project_name + "_impl", opts)
  190. return func
  191. if function is None:
  192. return setattr_hookimpl_opts
  193. else:
  194. return setattr_hookimpl_opts(function)
  195. def normalize_hookimpl_opts(opts: _HookImplOpts) -> None:
  196. opts.setdefault("tryfirst", False)
  197. opts.setdefault("trylast", False)
  198. opts.setdefault("wrapper", False)
  199. opts.setdefault("hookwrapper", False)
  200. opts.setdefault("optionalhook", False)
  201. opts.setdefault("specname", None)
  202. _PYPY = hasattr(sys, "pypy_version_info")
  203. def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
  204. """Return tuple of positional and keywrord argument names for a function,
  205. method, class or callable.
  206. In case of a class, its ``__init__`` method is considered.
  207. For methods the ``self`` parameter is not included.
  208. """
  209. if inspect.isclass(func):
  210. try:
  211. func = func.__init__
  212. except AttributeError:
  213. return (), ()
  214. elif not inspect.isroutine(func): # callable object?
  215. try:
  216. func = getattr(func, "__call__", func)
  217. except Exception:
  218. return (), ()
  219. try:
  220. # func MUST be a function or method here or we won't parse any args.
  221. sig = inspect.signature(
  222. func.__func__ if inspect.ismethod(func) else func # type:ignore[arg-type]
  223. )
  224. except TypeError:
  225. return (), ()
  226. _valid_param_kinds = (
  227. inspect.Parameter.POSITIONAL_ONLY,
  228. inspect.Parameter.POSITIONAL_OR_KEYWORD,
  229. )
  230. _valid_params = {
  231. name: param
  232. for name, param in sig.parameters.items()
  233. if param.kind in _valid_param_kinds
  234. }
  235. args = tuple(_valid_params)
  236. defaults = (
  237. tuple(
  238. param.default
  239. for param in _valid_params.values()
  240. if param.default is not param.empty
  241. )
  242. or None
  243. )
  244. if defaults:
  245. index = -len(defaults)
  246. args, kwargs = args[:index], tuple(args[index:])
  247. else:
  248. kwargs = ()
  249. # strip any implicit instance arg
  250. # pypy3 uses "obj" instead of "self" for default dunder methods
  251. if not _PYPY:
  252. implicit_names: tuple[str, ...] = ("self",)
  253. else:
  254. implicit_names = ("self", "obj")
  255. if args:
  256. qualname: str = getattr(func, "__qualname__", "")
  257. if inspect.ismethod(func) or ("." in qualname and args[0] in implicit_names):
  258. args = args[1:]
  259. return args, kwargs
  260. class _HookRelay:
  261. """Hook holder object for performing 1:N hook calls where N is the number
  262. of registered plugins."""
  263. __slots__ = ("__dict__",)
  264. if TYPE_CHECKING:
  265. def __getattr__(self, name: str) -> _HookCaller:
  266. ...
  267. _CallHistory = List[Tuple[Mapping[str, object], Optional[Callable[[Any], None]]]]
  268. class _HookCaller:
  269. __slots__ = (
  270. "name",
  271. "spec",
  272. "_hookexec",
  273. "_hookimpls",
  274. "_call_history",
  275. )
  276. def __init__(
  277. self,
  278. name: str,
  279. hook_execute: _HookExec,
  280. specmodule_or_class: _Namespace | None = None,
  281. spec_opts: _HookSpecOpts | None = None,
  282. ) -> None:
  283. self.name: Final = name
  284. self._hookexec: Final = hook_execute
  285. self._hookimpls: Final[list[HookImpl]] = []
  286. self._call_history: _CallHistory | None = None
  287. self.spec: HookSpec | None = None
  288. if specmodule_or_class is not None:
  289. assert spec_opts is not None
  290. self.set_specification(specmodule_or_class, spec_opts)
  291. def has_spec(self) -> bool:
  292. return self.spec is not None
  293. def set_specification(
  294. self,
  295. specmodule_or_class: _Namespace,
  296. spec_opts: _HookSpecOpts,
  297. ) -> None:
  298. if self.spec is not None:
  299. raise ValueError(
  300. f"Hook {self.spec.name!r} is already registered "
  301. f"within namespace {self.spec.namespace}"
  302. )
  303. self.spec = HookSpec(specmodule_or_class, self.name, spec_opts)
  304. if spec_opts.get("historic"):
  305. self._call_history = []
  306. def is_historic(self) -> bool:
  307. return self._call_history is not None
  308. def _remove_plugin(self, plugin: _Plugin) -> None:
  309. for i, method in enumerate(self._hookimpls):
  310. if method.plugin == plugin:
  311. del self._hookimpls[i]
  312. return
  313. raise ValueError(f"plugin {plugin!r} not found")
  314. def get_hookimpls(self) -> list[HookImpl]:
  315. return self._hookimpls.copy()
  316. def _add_hookimpl(self, hookimpl: HookImpl) -> None:
  317. """Add an implementation to the callback chain."""
  318. for i, method in enumerate(self._hookimpls):
  319. if method.hookwrapper or method.wrapper:
  320. splitpoint = i
  321. break
  322. else:
  323. splitpoint = len(self._hookimpls)
  324. if hookimpl.hookwrapper or hookimpl.wrapper:
  325. start, end = splitpoint, len(self._hookimpls)
  326. else:
  327. start, end = 0, splitpoint
  328. if hookimpl.trylast:
  329. self._hookimpls.insert(start, hookimpl)
  330. elif hookimpl.tryfirst:
  331. self._hookimpls.insert(end, hookimpl)
  332. else:
  333. # find last non-tryfirst method
  334. i = end - 1
  335. while i >= start and self._hookimpls[i].tryfirst:
  336. i -= 1
  337. self._hookimpls.insert(i + 1, hookimpl)
  338. def __repr__(self) -> str:
  339. return f"<_HookCaller {self.name!r}>"
  340. def _verify_all_args_are_provided(self, kwargs: Mapping[str, object]) -> None:
  341. # This is written to avoid expensive operations when not needed.
  342. if self.spec:
  343. for argname in self.spec.argnames:
  344. if argname not in kwargs:
  345. notincall = ", ".join(
  346. repr(argname)
  347. for argname in self.spec.argnames
  348. # Avoid self.spec.argnames - kwargs.keys() - doesn't preserve order.
  349. if argname not in kwargs.keys()
  350. )
  351. warnings.warn(
  352. "Argument(s) {} which are declared in the hookspec "
  353. "cannot be found in this hook call".format(notincall),
  354. stacklevel=2,
  355. )
  356. break
  357. def __call__(self, **kwargs: object) -> Any:
  358. assert (
  359. not self.is_historic()
  360. ), "Cannot directly call a historic hook - use call_historic instead."
  361. self._verify_all_args_are_provided(kwargs)
  362. firstresult = self.spec.opts.get("firstresult", False) if self.spec else False
  363. return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
  364. def call_historic(
  365. self,
  366. result_callback: Callable[[Any], None] | None = None,
  367. kwargs: Mapping[str, object] | None = None,
  368. ) -> None:
  369. """Call the hook with given ``kwargs`` for all registered plugins and
  370. for all plugins which will be registered afterwards.
  371. If ``result_callback`` is provided, it will be called for each
  372. non-``None`` result obtained from a hook implementation.
  373. """
  374. assert self._call_history is not None
  375. kwargs = kwargs or {}
  376. self._verify_all_args_are_provided(kwargs)
  377. self._call_history.append((kwargs, result_callback))
  378. # Historizing hooks don't return results.
  379. # Remember firstresult isn't compatible with historic.
  380. res = self._hookexec(self.name, self._hookimpls, kwargs, False)
  381. if result_callback is None:
  382. return
  383. if isinstance(res, list):
  384. for x in res:
  385. result_callback(x)
  386. def call_extra(
  387. self, methods: Sequence[Callable[..., object]], kwargs: Mapping[str, object]
  388. ) -> Any:
  389. """Call the hook with some additional temporarily participating
  390. methods using the specified ``kwargs`` as call parameters."""
  391. assert (
  392. not self.is_historic()
  393. ), "Cannot directly call a historic hook - use call_historic instead."
  394. self._verify_all_args_are_provided(kwargs)
  395. opts: _HookImplOpts = {
  396. "wrapper": False,
  397. "hookwrapper": False,
  398. "optionalhook": False,
  399. "trylast": False,
  400. "tryfirst": False,
  401. "specname": None,
  402. }
  403. hookimpls = self._hookimpls.copy()
  404. for method in methods:
  405. hookimpl = HookImpl(None, "<temp>", method, opts)
  406. # Find last non-tryfirst nonwrapper method.
  407. i = len(hookimpls) - 1
  408. while (
  409. i >= 0
  410. and hookimpls[i].tryfirst
  411. and not (hookimpls[i].hookwrapper or hookimpls[i].wrapper)
  412. ):
  413. i -= 1
  414. hookimpls.insert(i + 1, hookimpl)
  415. firstresult = self.spec.opts.get("firstresult", False) if self.spec else False
  416. return self._hookexec(self.name, hookimpls, kwargs, firstresult)
  417. def _maybe_apply_history(self, method: HookImpl) -> None:
  418. """Apply call history to a new hookimpl if it is marked as historic."""
  419. if self.is_historic():
  420. assert self._call_history is not None
  421. for kwargs, result_callback in self._call_history:
  422. res = self._hookexec(self.name, [method], kwargs, False)
  423. if res and result_callback is not None:
  424. # XXX: remember firstresult isn't compat with historic
  425. assert isinstance(res, list)
  426. result_callback(res[0])
  427. class _SubsetHookCaller(_HookCaller):
  428. """A proxy to another HookCaller which manages calls to all registered
  429. plugins except the ones from remove_plugins."""
  430. # This class is unusual: in inhertits from `_HookCaller` so all of
  431. # the *code* runs in the class, but it delegates all underlying *data*
  432. # to the original HookCaller.
  433. # `subset_hook_caller` used to be implemented by creating a full-fledged
  434. # HookCaller, copying all hookimpls from the original. This had problems
  435. # with memory leaks (#346) and historic calls (#347), which make a proxy
  436. # approach better.
  437. # An alternative implementation is to use a `_getattr__`/`__getattribute__`
  438. # proxy, however that adds more overhead and is more tricky to implement.
  439. __slots__ = (
  440. "_orig",
  441. "_remove_plugins",
  442. )
  443. def __init__(self, orig: _HookCaller, remove_plugins: AbstractSet[_Plugin]) -> None:
  444. self._orig = orig
  445. self._remove_plugins = remove_plugins
  446. self.name = orig.name # type: ignore[misc]
  447. self._hookexec = orig._hookexec # type: ignore[misc]
  448. @property # type: ignore[misc]
  449. def _hookimpls(self) -> list[HookImpl]:
  450. return [
  451. impl
  452. for impl in self._orig._hookimpls
  453. if impl.plugin not in self._remove_plugins
  454. ]
  455. @property
  456. def spec(self) -> HookSpec | None: # type: ignore[override]
  457. return self._orig.spec
  458. @property
  459. def _call_history(self) -> _CallHistory | None: # type: ignore[override]
  460. return self._orig._call_history
  461. def __repr__(self) -> str:
  462. return f"<_SubsetHookCaller {self.name!r}>"
  463. class HookImpl:
  464. __slots__ = (
  465. "function",
  466. "argnames",
  467. "kwargnames",
  468. "plugin",
  469. "opts",
  470. "plugin_name",
  471. "wrapper",
  472. "hookwrapper",
  473. "optionalhook",
  474. "tryfirst",
  475. "trylast",
  476. )
  477. def __init__(
  478. self,
  479. plugin: _Plugin,
  480. plugin_name: str,
  481. function: _HookImplFunction[object],
  482. hook_impl_opts: _HookImplOpts,
  483. ) -> None:
  484. self.function: Final = function
  485. self.argnames, self.kwargnames = varnames(self.function)
  486. self.plugin = plugin
  487. self.opts = hook_impl_opts
  488. self.plugin_name = plugin_name
  489. self.wrapper = hook_impl_opts["wrapper"]
  490. self.hookwrapper = hook_impl_opts["hookwrapper"]
  491. self.optionalhook = hook_impl_opts["optionalhook"]
  492. self.tryfirst = hook_impl_opts["tryfirst"]
  493. self.trylast = hook_impl_opts["trylast"]
  494. def __repr__(self) -> str:
  495. return f"<HookImpl plugin_name={self.plugin_name!r}, plugin={self.plugin!r}>"
  496. class HookSpec:
  497. __slots__ = (
  498. "namespace",
  499. "function",
  500. "name",
  501. "argnames",
  502. "kwargnames",
  503. "opts",
  504. "warn_on_impl",
  505. )
  506. def __init__(self, namespace: _Namespace, name: str, opts: _HookSpecOpts) -> None:
  507. self.namespace = namespace
  508. self.function: Callable[..., object] = getattr(namespace, name)
  509. self.name = name
  510. self.argnames, self.kwargnames = varnames(self.function)
  511. self.opts = opts
  512. self.warn_on_impl = opts.get("warn_on_impl")