Skip to content

CLI: an abort while reading input does not end the session - #2122

Closed
tombouctou wants to merge 3 commits into
masterfrom
abort-at-idle-prompt
Closed

tombouctou wants to merge 3 commits into
masterfrom
abort-at-idle-prompt

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

Found while testing Ctrl-C for #2121 by driving the CLI through a pseudo-terminal.

The interrupt menu's "abort" raises AbortInterrupt from the SIGINT handler. When the handler runs while the CLI is reading input, there's no evaluation to catch it, so it escapes interactive_eval_loop and ends the session with a traceback (exit code 1):

interrupt> a
aborting
Traceback (most recent call last):
  ...
  File "mathics/repl.py", line 363, in interactive_eval_loop
    query, source_code = evaluation.parse_feeder_returning_code(shell)
  ...
  File "mathics/repl.py", line 294, in rl_read_line
    return input(prompt)
  File "mathics/interrupt.py", line 182, in Mathics3_basic_signal_handler
  ...
mathics.core.interrupt.AbortInterrupt

Now the loop drops the input being typed and shows a new prompt.

How hard is it to hit? Hard, in an ordinary terminal. With readline, Ctrl-C at the prompt never reaches Mathics' handler: readline deals with it. It does reach the handler when a line was typed before readline set up the terminal, which is what a script driving the CLI does. I reproduced it that way every time. A fast paste, or another front end, might hit it too. The fix is a single except clause in the read-eval loop.

Test: test/test_repl_abort_at_prompt.py runs interactive_eval_loop with a shell whose read_line() raises AbortInterrupt (as the handler does inside input()), then reads 6*7. The session must go on and print 42. The test fails without the fix.

🤖 Generated with Claude Code

The interrupt menu raises AbortInterrupt from the SIGINT handler. When the
handler runs while the CLI reads input, no evaluation is there to catch
it: it left interactive_eval_loop and ended the session with a traceback.
Now the loop drops the input being typed and shows a new prompt.

Reaching it takes a SIGINT that Python handles inside input(). With
readline, Ctrl-C at the prompt does not get there: readline handles it.
It does when the line was typed before readline set up the terminal, as a
script driving the CLI in a pseudo-terminal does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tombouctou and others added 2 commits November 7, 2026 01:51
… leaves installed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rocky

rocky commented Nov 7, 2026

Copy link
Copy Markdown
Member

LGTM.

With respect to the "black" problems on the PRs, if you install Python's pre-commit:

$ pip show pre-commit
Name: pre_commit
Version: 4.7.0
Summary: A framework for managing and maintaining multi-language pre-commit hooks.
Home-page: https://github.com/pre-commit/pre-commit

It will run black, isort, and mypy as the first part of git commit.

@rocky rocky closed this Nov 10, 2026
@rocky
rocky deleted the abort-at-idle-prompt branch November 10, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants