Starts or resumes the generator, running until it reaches a yield point that is not ready.
(self)
| 764 | self.ctx_run(self.run) |
| 765 | |
| 766 | def run(self) -> None: |
| 767 | class="st">"""Starts or resumes the generator, running until it reaches a |
| 768 | yield point that is not ready. |
| 769 | class="st">""" |
| 770 | if self.running or self.finished: |
| 771 | return |
| 772 | try: |
| 773 | self.running = True |
| 774 | while True: |
| 775 | future = self.future |
| 776 | if future is None: |
| 777 | raise Exception(class="st">"No pending future") |
| 778 | if not future.done(): |
| 779 | return |
| 780 | self.future = None |
| 781 | try: |
| 782 | try: |
| 783 | value = future.result() |
| 784 | except Exception as e: |
| 785 | class="cm"># Save the exception for later. It's important that |
| 786 | class="cm"># gen.throw() not be called inside this try/except block |
| 787 | class="cm"># because that makes sys.exc_info behave unexpectedly. |
| 788 | exc: Optional[Exception] = e |
| 789 | else: |
| 790 | exc = None |
| 791 | finally: |
| 792 | future = None |
| 793 | |
| 794 | if exc is not None: |
| 795 | try: |
| 796 | yielded = self.gen.throw(exc) |
| 797 | finally: |
| 798 | class="cm"># Break up a circular reference for faster GC on |
| 799 | class="cm"># CPython. |
| 800 | del exc |
| 801 | else: |
| 802 | yielded = self.gen.send(value) |
| 803 | |
| 804 | except (StopIteration, Return) as e: |
| 805 | self.finished = True |
| 806 | self.future = _null_future |
| 807 | future_set_result_unless_cancelled( |
| 808 | self.result_future, _value_from_stopiteration(e) |
| 809 | ) |
| 810 | self.result_future = None class="cm"># type: ignore |
| 811 | return |
| 812 | except Exception: |
| 813 | self.finished = True |
| 814 | self.future = _null_future |
| 815 | future_set_exc_info(self.result_future, sys.exc_info()) |
| 816 | self.result_future = None class="cm"># type: ignore |
| 817 | return |
| 818 | if not self.handle_yield(yielded): |
| 819 | return |
| 820 | yielded = None |
| 821 | finally: |
| 822 | self.running = False |
| 823 |