| 152 | |
| 153 | |
| 154 | class InteractiveColoredConsole(code.InteractiveConsole): |
| 155 | def __init__( |
| 156 | self, |
| 157 | locals: dict[str, object] | None = None, |
| 158 | filename: str = "<console>", |
| 159 | *, |
| 160 | local_exit: bool = False, |
| 161 | ) -> None: |
| 162 | super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg] |
| 163 | self.can_colorize = _colorize.can_colorize() |
| 164 | |
| 165 | def showsyntaxerror(self, filename=None, **kwargs): |
| 166 | super().showsyntaxerror(filename=filename, **kwargs) |
| 167 | |
| 168 | def _excepthook(self, typ, value, tb): |
| 169 | import traceback |
| 170 | lines = traceback.format_exception( |
| 171 | typ, value, tb, |
| 172 | colorize=self.can_colorize, |
| 173 | limit=traceback.BUILTIN_EXCEPTION_LIMIT) |
| 174 | self.write(''.join(lines)) |
| 175 | |
| 176 | def runsource(self, source, filename="<input>", symbol="single"): |
| 177 | try: |
| 178 | tree = self.compile.compiler( |
| 179 | source, |
| 180 | filename, |
| 181 | "exec", |
| 182 | ast.PyCF_ONLY_AST, |
| 183 | incomplete_input=False, |
| 184 | ) |
| 185 | except (SyntaxError, OverflowError, ValueError): |
| 186 | self.showsyntaxerror(filename, source=source) |
| 187 | return False |
| 188 | if tree.body: |
| 189 | *_, last_stmt = tree.body |
| 190 | for stmt in tree.body: |
| 191 | wrapper = ast.Interactive if stmt is last_stmt else ast.Module |
| 192 | the_symbol = symbol if stmt is last_stmt else "exec" |
| 193 | item = wrapper([stmt]) |
| 194 | try: |
| 195 | code = self.compile.compiler(item, filename, the_symbol) |
| 196 | except SyntaxError as e: |
| 197 | if e.args[0] == "'await' outside function": |
| 198 | python = os.path.basename(sys.executable) |
| 199 | e.add_note( |
| 200 | f"Try the asyncio REPL ({python} -m asyncio) to use" |
| 201 | f" top-level 'await' and run background asyncio tasks." |
| 202 | ) |
| 203 | self.showsyntaxerror(filename, source=source) |
| 204 | return False |
| 205 | except (OverflowError, ValueError): |
| 206 | self.showsyntaxerror(filename, source=source) |
| 207 | return False |
| 208 | |
| 209 | if code is None: |
| 210 | return True |
| 211 |
no outgoing calls
no test coverage detected