Compile the decorated function. Called once for a given decorated function -- collects the code from all argmap decorators in the stack, and compiles the decorated function. Much of the work done here uses the `assemble` method to allow recursive treatment of multip
(self, f)
| 711 | return f"argmap_{fname}_{cls._count()}" |
| 712 | |
| 713 | def compile(self, f): |
| 714 | """Compile the decorated function. |
| 715 | |
| 716 | Called once for a given decorated function -- collects the code from all |
| 717 | argmap decorators in the stack, and compiles the decorated function. |
| 718 | |
| 719 | Much of the work done here uses the `assemble` method to allow recursive |
| 720 | treatment of multiple argmap decorators on a single decorated function. |
| 721 | That flattens the argmap decorators, collects the source code to construct |
| 722 | a single decorated function, then compiles/executes/returns that function. |
| 723 | |
| 724 | The source code for the decorated function is stored as an attribute |
| 725 | `_code` on the function object itself. |
| 726 | |
| 727 | Note that Python's `compile` function requires a filename, but this |
| 728 | code is constructed without a file, so a fictitious filename is used |
| 729 | to describe where the function comes from. The name is something like: |
| 730 | "argmap compilation 4". |
| 731 | |
| 732 | Parameters |
| 733 | ---------- |
| 734 | f : callable |
| 735 | The function to be decorated |
| 736 | |
| 737 | Returns |
| 738 | ------- |
| 739 | func : callable |
| 740 | The decorated file |
| 741 | |
| 742 | """ |
| 743 | sig, wrapped_name, functions, mapblock, finallys, mutable_args = self.assemble( |
| 744 | f |
| 745 | ) |
| 746 | |
| 747 | call = f"{sig.call_sig.format(wrapped_name)}#" |
| 748 | mut_args = f"{sig.args} = list({sig.args})" if mutable_args else "" |
| 749 | body = argmap._indent(sig.def_sig, mut_args, mapblock, call, finallys) |
| 750 | code = "\n".join(body) |
| 751 | |
| 752 | locl = {} |
| 753 | globl = dict(functions.values()) |
| 754 | filename = f"{self.__class__} compilation {self._count()}" |
| 755 | compiled = compile(code, filename, "exec") |
| 756 | exec(compiled, globl, locl) |
| 757 | func = locl[sig.name] |
| 758 | func._code = code |
| 759 | return func |
| 760 | |
| 761 | def assemble(self, f): |
| 762 | """Collects components of the source for the decorated function wrapping f. |
no test coverage detected