(
console: code.InteractiveConsole,
*,
future_flags: int = 0,
)
| 103 | |
| 104 | |
| 105 | def run_multiline_interactive_console( |
| 106 | console: code.InteractiveConsole, |
| 107 | *, |
| 108 | future_flags: int = 0, |
| 109 | ) -> None: |
| 110 | from .readline import _setup |
| 111 | _setup(console.locals) |
| 112 | if future_flags: |
| 113 | console.compile.compiler.flags |= future_flags |
| 114 | |
| 115 | more_lines = functools.partial(_more_lines, console) |
| 116 | input_n = 0 |
| 117 | |
| 118 | def maybe_run_command(statement: str) -> bool: |
| 119 | statement = statement.strip() |
| 120 | if statement in console.locals or statement not in REPL_COMMANDS: |
| 121 | return False |
| 122 | |
| 123 | reader = _get_reader() |
| 124 | reader.history.pop() # skip internal commands in history |
| 125 | command = REPL_COMMANDS[statement] |
| 126 | if callable(command): |
| 127 | # Make sure that history does not change because of commands |
| 128 | with reader.suspend_history(): |
| 129 | command() |
| 130 | return True |
| 131 | return False |
| 132 | |
| 133 | while 1: |
| 134 | try: |
| 135 | try: |
| 136 | sys.stdout.flush() |
| 137 | except Exception: |
| 138 | pass |
| 139 | |
| 140 | ps1 = getattr(sys, "ps1", ">>> ") |
| 141 | ps2 = getattr(sys, "ps2", "... ") |
| 142 | try: |
| 143 | statement = multiline_input(more_lines, ps1, ps2) |
| 144 | except EOFError: |
| 145 | break |
| 146 | |
| 147 | if maybe_run_command(statement): |
| 148 | continue |
| 149 | |
| 150 | input_name = f"<python-input-{input_n}>" |
| 151 | linecache._register_code(input_name, statement, "<stdin>") # type: ignore[attr-defined] |
| 152 | more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single") # type: ignore[call-arg] |
| 153 | assert not more |
| 154 | input_n += 1 |
| 155 | except KeyboardInterrupt: |
| 156 | r = _get_reader() |
| 157 | if r.input_trans is r.isearch_trans: |
| 158 | r.do_cmd(("isearch-end", [""])) |
| 159 | r.pos = len(r.get_unicode()) |
| 160 | r.dirty = True |
| 161 | r.refresh() |
| 162 | r.in_bracketed_paste = False |
no test coverage detected