Skip to content

Improve Python console: Fix compound mode, Ctrl+C handling and auto-indent - #678

Merged
joern274 merged 1 commit into
emsec:masterfrom
JohanHaas:fix/python-console-improvements
Nov 1, 2026
Merged

joern274 merged 1 commit into
emsec:masterfrom
JohanHaas:fix/python-console-improvements

Conversation

@JohanHaas

Copy link
Copy Markdown
Contributor

Problem

  • The only way exiting compound mode is restarting HAL
  • Compound statements were never executed
  • No auto indents
  • Tabs instead of spaces

Changes

Behavior is modeled after the standard Python shell

  • Ctrl + C now handles 4 cases:
    • Exit Compound Mode
    • Abort thread
    • Reset prompt
    • Copy
  • Fix compound mode so statements are actually executed
  • Auto indentations after :
  • Block unindent: On 4 aligned whitespaces delete them in batches
  • Use codeop.compile_command instead of removed PyParser/Py_CompileStringExFlags API. This can distinguish between incomplete, complete and syntax error

@joern274 joern274 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for providing this PR to improve the Python console. I haven't done a thorough review yet, but I can already see some valuable contributions, like using codeop to check whether a statement is complete instead of calling the parser directly.

Unfortunately, the code you worked on has changed in the meantime to fix other problems (3418bd2), so your PR no longer merges without conflicts. Please rebase it onto the current master.

You might also want to test whether some of the problems you were targeting are already gone. For example, I added the [Esc] key to leave compound mode, since users complained about getting trapped in it, and I removed some causes of getting trapped by accident, such as pressing [Enter] twice.

Also note that PR #672 changes interpretCommand() and checkCompleteStatement() as well, so whichever of the two is merged later will need another small rebase.

I look forward to reviewing and merging your PR once it is rebased.

@JohanHaas
JohanHaas force-pushed the fix/python-console-improvements branch from 8163aa7 to cad7426 Compare October 28, 2026 18:48
@JohanHaas

Copy link
Copy Markdown
Contributor Author

I did the rebase. I kept my approach to the interpretCommand restructure, because it is more readable and makes auto indent possible, and adopted two improvements from HEAD. I kept the codeop base implementation.

@JohanHaas
JohanHaas requested a review from joern274 October 28, 2026 19:31
@JohanHaas

Copy link
Copy Markdown
Contributor Author

Note that this PR partially addresses #672: PyErr_Clear is resolved by the rewrite using codeop.

@joern274 joern274 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will merge useful PR now and fix minor weaknesses afterwards, namely:

  • By now we have already a hotkey to escape compound mode (Esc), we don't need two
  • Converting tabs to 4 spaces must be consistent including history entries and paste
  • The history index is reset when input is discarded.

@joern274
joern274 merged commit 3c2e8eb into emsec:master Nov 1, 2026
5 checks passed
joern274 added a commit that referenced this pull request Nov 1, 2026
JohanHaas pushed a commit to JohanHaas/hal that referenced this pull request Nov 2, 2026
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