(self, code)
| 28 | self.context = contextvars.copy_context() |
| 29 | |
| 30 | def runcode(self, code): |
| 31 | global return_code |
| 32 | future = concurrent.futures.Future() |
| 33 | |
| 34 | def callback(): |
| 35 | global return_code |
| 36 | global repl_future |
| 37 | global keyboard_interrupted |
| 38 | |
| 39 | repl_future = None |
| 40 | keyboard_interrupted = False |
| 41 | |
| 42 | func = types.FunctionType(code, self.locals) |
| 43 | try: |
| 44 | coro = func() |
| 45 | except SystemExit as se: |
| 46 | return_code = se.code |
| 47 | self.loop.stop() |
| 48 | return |
| 49 | except KeyboardInterrupt as ex: |
| 50 | keyboard_interrupted = True |
| 51 | future.set_exception(ex) |
| 52 | return |
| 53 | except BaseException as ex: |
| 54 | future.set_exception(ex) |
| 55 | return |
| 56 | |
| 57 | if not inspect.iscoroutine(coro): |
| 58 | future.set_result(coro) |
| 59 | return |
| 60 | |
| 61 | try: |
| 62 | repl_future = self.loop.create_task(coro, context=self.context) |
| 63 | futures._chain_future(repl_future, future) |
| 64 | except BaseException as exc: |
| 65 | future.set_exception(exc) |
| 66 | |
| 67 | self.loop.call_soon_threadsafe(callback, context=self.context) |
| 68 | |
| 69 | try: |
| 70 | return future.result() |
| 71 | except SystemExit as se: |
| 72 | return_code = se.code |
| 73 | self.loop.stop() |
| 74 | return |
| 75 | except BaseException: |
| 76 | if keyboard_interrupted: |
| 77 | if not CAN_USE_PYREPL: |
| 78 | self.write("\nKeyboardInterrupt\n") |
| 79 | else: |
| 80 | self.showtraceback() |
| 81 | return self.STATEMENT_FAILED |
| 82 | |
| 83 | class REPLThread(threading.Thread): |
| 84 |
no test coverage detected