Skip to content

Print \x04 and \x1a input bytes as ^D and ^Z in CliRunner - #3878

Open
kdeldycke wants to merge 1 commit into
pallets:mainfrom
kdeldycke:echo-stdin-class
Open

kdeldycke wants to merge 1 commit into
pallets:mainfrom
kdeldycke:echo-stdin-class

Conversation

@kdeldycke

@kdeldycke kdeldycke commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

This PR change CliRunner to translate the control bytes and render them differently on output:

<stdin> byte Echoed to <stdout> as Pressed keys
\x04 ^D\n Ctrl+D on Unix
\x1a ^Z\n Ctrl+Z on Windows

It also no longer passes these bytes to the command.

As this introduce behavioral changes, it targets 8.6.0.

This will remove an hard-coded patch in pallets-sphinx-themes and a follow-up PR is maintained at pallets/pallets-sphinx-themes#133 so you can see the benefits of this change.

@kdeldycke
kdeldycke changed the base branch from main to stable September 24, 2026 06:46
@kdeldycke kdeldycke added the test runner CliRunner and Click's test utilities feature label Sep 24, 2026
@kdeldycke kdeldycke added this to the 8.5.1 milestone Sep 24, 2026
kdeldycke added a commit to kdeldycke/pallets-sphinx-themes that referenced this pull request Sep 24, 2026
@davidism

Copy link
Copy Markdown
Member

I'd prefer not to add more xyz_class style customization points, unless absolutely necessary. It makes typing more complex for anything that wants to override methods on such a class (that wouldn't really be the case here, but still). I'm actively removing them from Flask and Werkzeug.

I'm not particularly bothered by the override in the theme. I don't think enough things would want to override this to justify the new attribute.

Perhaps we should consider why the default echo doesn't show control input? Perhaps we can add it here and remove the override that way?

@kdeldycke

Copy link
Copy Markdown
Collaborator Author

why the default echo doesn't show control input? Perhaps we can add it here and remove the override that way?

Yes, I'm OK to explore that direction instead. I don't have a real use-case, just stumble upon it while cleaning my own re-implementation of the {click:*} directives for Sphinx.

Question is, should we translate all control bytes to caret-based notation or should we just do \x04, and maybe \x1a as _translate_ch_to_exc does?

def _translate_ch_to_exc(ch: str) -> None:
if ch == "\x03":
raise KeyboardInterrupt()
if ch == "\x04" and not WIN: # Unix-like, Ctrl+D
raise EOFError()
if ch == "\x1a" and WIN: # Windows, Ctrl+Z
raise EOFError()

@Rowlando13

Copy link
Copy Markdown
Member

@kdeldycke I also think not more customization here would be good.

@kdeldycke

Copy link
Copy Markdown
Collaborator Author

Just reworked that PR and pushed it a bit: it now integrates directly the modifications from pallets-sphinx-themes, and perform the translation of bytes in CliRunner:

<stdin> byte Echoed to <stdout> as Pressed keys
\x04 ^D\n Ctrl+D on Unix
\x1a ^Z\n Ctrl+Z on Windows

This does not propose a documented point of customization, but change the behavior of the CliRunner.

@kdeldycke
kdeldycke changed the base branch from stable to main October 5, 2026 11:38
@kdeldycke kdeldycke modified the milestones: 8.5.1, 8.6.0 Oct 5, 2026
@kdeldycke kdeldycke changed the title Add CliRunner.echo_stdin_class to override the <stdin> echo wrapper Print \x04 and \x1a input bytes as ^D and ^Z in CliRunner Oct 5, 2026
@kdeldycke

Copy link
Copy Markdown
Collaborator Author

Just retargeted to 8.6.0 . This PR is ready for a review.

kdeldycke added a commit to kdeldycke/pallets-sphinx-themes that referenced this pull request Oct 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test runner CliRunner and Click's test utilities feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants