| 154 | |
| 155 | |
| 156 | class InteractiveColoredConsole(code.InteractiveConsole): |
| 157 | STATEMENT_FAILED = object() |
| 158 | |
| 159 | def __init__( |
| 160 | self, |
| 161 | locals: dict[str, object] | None = None, |
| 162 | filename: str = "<console>", |
| 163 | *, |
| 164 | local_exit: bool = False, |
| 165 | ) -> None: |
| 166 | super().__init__(locals=locals, filename=filename, local_exit=local_exit) |
| 167 | self.can_colorize = _colorize.can_colorize() |
| 168 | |
| 169 | def showsyntaxerror(self, filename=None, **kwargs): |
| 170 | super().showsyntaxerror(filename=filename, **kwargs) |
| 171 | |
| 172 | def _excepthook(self, typ, value, tb): |
| 173 | import traceback |
| 174 | lines = traceback.format_exception( |
| 175 | typ, value, tb, |
| 176 | colorize=self.can_colorize, |
| 177 | limit=traceback.BUILTIN_EXCEPTION_LIMIT) |
| 178 | self.write(''.join(lines)) |
| 179 | |
| 180 | def runcode(self, code): |
| 181 | try: |
| 182 | exec(code, self.locals) |
| 183 | except SystemExit: |
| 184 | raise |
| 185 | except BaseException: |
| 186 | self.showtraceback() |
| 187 | return self.STATEMENT_FAILED |
| 188 | return None |
| 189 | |
| 190 | def runsource(self, source, filename="<input>", symbol="single"): |
| 191 | try: |
| 192 | tree = self.compile.compiler( |
| 193 | source, |
| 194 | filename, |
| 195 | "exec", |
| 196 | ast.PyCF_ONLY_AST, |
| 197 | incomplete_input=False, |
| 198 | ) |
| 199 | except SyntaxError as e: |
| 200 | # If it looks like pip install was entered (a common beginner |
| 201 | # mistake), provide a hint to use the system command prompt. |
| 202 | if re.match(r"^\s*(pip3?|py(thon3?)? -m pip) install.*", source): |
| 203 | e.add_note( |
| 204 | "The Python package manager (pip) can only be used" |
| 205 | " outside of the Python REPL.\n" |
| 206 | "Try the 'pip' command in a separate terminal or" |
| 207 | " command prompt." |
| 208 | ) |
| 209 | self.showsyntaxerror(filename, source=source) |
| 210 | return False |
| 211 | except (OverflowError, ValueError): |
| 212 | self.showsyntaxerror(filename, source=source) |
| 213 | return False |
no outgoing calls
searching dependent graphs…