| 501 | @internalcode |
| 502 | @evalcontextfunction |
| 503 | def __call__(self, *args, **kwargs): |
| 504 | # This requires a bit of explanation, In the past we used to |
| 505 | # decide largely based on compile-time information if a macro is |
| 506 | # safe or unsafe. While there was a volatile mode it was largely |
| 507 | # unused for deciding on escaping. This turns out to be |
| 508 | # problemtic for macros because if a macro is safe or not not so |
| 509 | # much depends on the escape mode when it was defined but when it |
| 510 | # was used. |
| 511 | # |
| 512 | # Because however we export macros from the module system and |
| 513 | # there are historic callers that do not pass an eval context (and |
| 514 | # will continue to not pass one), we need to perform an instance |
| 515 | # check here. |
| 516 | # |
| 517 | # This is considered safe because an eval context is not a valid |
| 518 | # argument to callables otherwise anwyays. Worst case here is |
| 519 | # that if no eval context is passed we fall back to the compile |
| 520 | # time autoescape flag. |
| 521 | if args and isinstance(args[0], EvalContext): |
| 522 | autoescape = args[0].autoescape |
| 523 | args = args[1:] |
| 524 | else: |
| 525 | autoescape = self._default_autoescape |
| 526 | |
| 527 | # try to consume the positional arguments |
| 528 | arguments = list(args[:self._argument_count]) |
| 529 | off = len(arguments) |
| 530 | |
| 531 | # For information why this is necessary refer to the handling |
| 532 | # of caller in the `macro_body` handler in the compiler. |
| 533 | found_caller = False |
| 534 | |
| 535 | # if the number of arguments consumed is not the number of |
| 536 | # arguments expected we start filling in keyword arguments |
| 537 | # and defaults. |
| 538 | if off != self._argument_count: |
| 539 | for idx, name in enumerate(self.arguments[len(arguments):]): |
| 540 | try: |
| 541 | value = kwargs.pop(name) |
| 542 | except KeyError: |
| 543 | value = missing |
| 544 | if name == 'caller': |
| 545 | found_caller = True |
| 546 | arguments.append(value) |
| 547 | else: |
| 548 | found_caller = self.explicit_caller |
| 549 | |
| 550 | # it's important that the order of these arguments does not change |
| 551 | # if not also changed in the compiler's `function_scoping` method. |
| 552 | # the order is caller, keyword arguments, positional arguments! |
| 553 | if self.caller and not found_caller: |
| 554 | caller = kwargs.pop('caller', None) |
| 555 | if caller is None: |
| 556 | caller = self._environment.undefined('No caller defined', |
| 557 | name='caller') |
| 558 | arguments.append(caller) |
| 559 | |
| 560 | if self.catch_kwargs: |