(
console: code.InteractiveConsole,
*,
future_flags: int = 0,
)
| 98 | |
| 99 | |
| 100 | def run_multiline_interactive_console( |
| 101 | console: code.InteractiveConsole, |
| 102 | *, |
| 103 | future_flags: int = 0, |
| 104 | ) -> None: |
| 105 | from .readline import _setup |
| 106 | _setup(console.locals) |
| 107 | if future_flags: |
| 108 | console.compile.compiler.flags |= future_flags |
| 109 | |
| 110 | more_lines = functools.partial(_more_lines, console) |
| 111 | input_n = 0 |
| 112 | |
| 113 | _is_x_showrefcount_set = sys._xoptions.get("showrefcount") |
| 114 | _is_pydebug_build = hasattr(sys, "gettotalrefcount") |
| 115 | show_ref_count = _is_x_showrefcount_set and _is_pydebug_build |
| 116 | |
| 117 | def maybe_run_command(statement: str) -> bool: |
| 118 | statement = statement.strip() |
| 119 | if statement in console.locals or statement not in REPL_COMMANDS: |
| 120 | return False |
| 121 | |
| 122 | reader = _get_reader() |
| 123 | reader.history.pop() # skip internal commands in history |
| 124 | command = REPL_COMMANDS[statement] |
| 125 | if callable(command): |
| 126 | # Make sure that history does not change because of commands |
| 127 | with reader.suspend_history(), reader.suspend_colorization(): |
| 128 | command() |
| 129 | return True |
| 130 | return False |
| 131 | |
| 132 | while True: |
| 133 | try: |
| 134 | try: |
| 135 | sys.stdout.flush() |
| 136 | except Exception: |
| 137 | pass |
| 138 | |
| 139 | ps1 = getattr(sys, "ps1", ">>> ") |
| 140 | ps2 = getattr(sys, "ps2", "... ") |
| 141 | try: |
| 142 | statement = multiline_input(more_lines, ps1, ps2) |
| 143 | except EOFError: |
| 144 | break |
| 145 | |
| 146 | if maybe_run_command(statement): |
| 147 | continue |
| 148 | |
| 149 | input_name = f"<python-input-{input_n}>" |
| 150 | more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single") # type: ignore[call-arg] |
| 151 | assert not more |
| 152 | try: |
| 153 | append_history_file() |
| 154 | except (FileNotFoundError, PermissionError, OSError) as e: |
| 155 | warnings.warn(f"failed to open the history file for writing: {e}") |
| 156 | |
| 157 | input_n += 1 |
no test coverage detected
searching dependent graphs…