python / python/cpython

make pyrepl more robust against crashes

Offen
#122,468 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic-repl type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

This issue is motivated by a discussion with @serhiy-storchaka on #122456. Pyrepl/code.InteractiveInterpreter does not handle exceptions at various points, see the linked commend for an example.

Directly after writing this, I also got the following traceback that I now can't reproduce, by pressing ctrl-r then searching for a string, and then pressing ctrl-c at somehow the wrong point:

>>> Traceback (most recent call last):
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/simple_interact.py", line 147, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/readline.py", line 385, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 773, in readline
    self.handle1()
    ~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 730, in handle1
    event = self.console.get_event(block)
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/unix_console.py", line 391, in get_event
    self.push_char(self.__read(1))
                   ~~~~~~~~~~~^^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/unix_console.py", line 203, in __read
    self.input_buffer = os.read(self.input_fd, 10000)
                        ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/simple_interact.py", line 164, in run_multiline_interactive_console
    r.pop_input_trans()
    ~~~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 546, in pop_input_trans
    self.input_trans = self.input_trans_stack.pop()
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
IndexError: pop from empty list

This crashed pyrepl. I think it would be better if we printed the exception, but then actually stayed in pyrepl, to make sure we don't lose the repl state which might have been annoying to get to.

Another way to crash pyrepl, but not the classic repl is to close sys.stderr and then raising an exception.

We could make pyrepl less crash-prone that by adding an except BaseException around the pyrepl loop in run_multiline_interactive_console.

Any opinions, @pablogsal, @ambv?

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Linux

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in Lib/_pyrepl/simple_interact.py bei run_multiline_interactive_console und verfolge dann die in der Traceback angezeigten Ausnahme-Pfade durch reader.py und unix_console.py. Reproduziere den Ctrl-C-/Suchfehler sowie den Fall mit geschlossenem sys.stderr. Als erledigt gilt die Aufgabe, wenn pyrepl die Ausnahme meldet und weiterhin verwendbar bleibt, ohne den REPL-Zustand zu verlieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.