Call the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable has :func:`pass_context` or :func:`pass_environment`.
(
__self, __obj: t.Callable, *args: t.Any, **kwargs: t.Any # noqa: B902
)
| 259 | |
| 260 | @internalcode |
| 261 | def call( |
| 262 | __self, __obj: t.Callable, *args: t.Any, **kwargs: t.Any # noqa: B902 |
| 263 | ) -> t.Union[t.Any, "Undefined"]: |
| 264 | """Call the callable with the arguments and keyword arguments |
| 265 | provided but inject the active context or environment as first |
| 266 | argument if the callable has :func:`pass_context` or |
| 267 | :func:`pass_environment`. |
| 268 | """ |
| 269 | if __debug__: |
| 270 | __traceback_hide__ = True # noqa |
| 271 | |
| 272 | # Allow callable classes to take a context |
| 273 | if ( |
| 274 | hasattr(__obj, "__call__") # noqa: B004 |
| 275 | and _PassArg.from_obj(__obj.__call__) is not None # type: ignore |
| 276 | ): |
| 277 | __obj = __obj.__call__ # type: ignore |
| 278 | |
| 279 | pass_arg = _PassArg.from_obj(__obj) |
| 280 | |
| 281 | if pass_arg is _PassArg.context: |
| 282 | # the active context should have access to variables set in |
| 283 | # loops and blocks without mutating the context itself |
| 284 | if kwargs.get("_loop_vars"): |
| 285 | __self = __self.derived(kwargs["_loop_vars"]) |
| 286 | if kwargs.get("_block_vars"): |
| 287 | __self = __self.derived(kwargs["_block_vars"]) |
| 288 | args = (__self,) + args |
| 289 | elif pass_arg is _PassArg.eval_context: |
| 290 | args = (__self.eval_ctx,) + args |
| 291 | elif pass_arg is _PassArg.environment: |
| 292 | args = (__self.environment,) + args |
| 293 | |
| 294 | kwargs.pop("_block_vars", None) |
| 295 | kwargs.pop("_loop_vars", None) |
| 296 | |
| 297 | try: |
| 298 | return __obj(*args, **kwargs) |
| 299 | except StopIteration: |
| 300 | return __self.environment.undefined( |
| 301 | "value was undefined because a callable raised a" |
| 302 | " StopIteration exception" |
| 303 | ) |
| 304 | |
| 305 | def derived(self, locals: t.Optional[t.Dict[str, t.Any]] = None) -> "Context": |
| 306 | """Internal helper function to create a derived context. This is |