Pops the topmost level for assignment tracking and updates the context variables if necessary.
(self, frame: Frame)
| 779 | self._assign_stack.append(set()) |
| 780 | |
| 781 | def pop_assign_tracking(self, frame: Frame) -> None: |
| 782 | """Pops the topmost level for assignment tracking and updates the |
| 783 | context variables if necessary. |
| 784 | """ |
| 785 | vars = self._assign_stack.pop() |
| 786 | if ( |
| 787 | not frame.block_frame |
| 788 | and not frame.loop_frame |
| 789 | and not frame.toplevel |
| 790 | or not vars |
| 791 | ): |
| 792 | return |
| 793 | public_names = [x for x in vars if x[:1] != "_"] |
| 794 | if len(vars) == 1: |
| 795 | name = next(iter(vars)) |
| 796 | ref = frame.symbols.ref(name) |
| 797 | if frame.loop_frame: |
| 798 | self.writeline(f"_loop_vars[{name!r}] = {ref}") |
| 799 | return |
| 800 | if frame.block_frame: |
| 801 | self.writeline(f"_block_vars[{name!r}] = {ref}") |
| 802 | return |
| 803 | self.writeline(f"context.vars[{name!r}] = {ref}") |
| 804 | else: |
| 805 | if frame.loop_frame: |
| 806 | self.writeline("_loop_vars.update({") |
| 807 | elif frame.block_frame: |
| 808 | self.writeline("_block_vars.update({") |
| 809 | else: |
| 810 | self.writeline("context.vars.update({") |
| 811 | for idx, name in enumerate(vars): |
| 812 | if idx: |
| 813 | self.write(", ") |
| 814 | ref = frame.symbols.ref(name) |
| 815 | self.write(f"{name!r}: {ref}") |
| 816 | self.write("})") |
| 817 | if not frame.block_frame and not frame.loop_frame and public_names: |
| 818 | if len(public_names) == 1: |
| 819 | self.writeline(f"context.exported_vars.add({public_names[0]!r})") |
| 820 | else: |
| 821 | names_str = ", ".join(map(repr, public_names)) |
| 822 | self.writeline(f"context.exported_vars.update(({names_str}))") |
| 823 | |
| 824 | # -- Statement Visitors |
| 825 |