Skip to content
Myr

Merge pull request #1108 from matejcik/6.x-maintenance

Before adoption: this commit came in with the history the project adopted, so no landing records it.

make `getchar` work on Windows.

authored by Stephen Rosen · committed by GitHub · parents 035bbfa, da4a182 · Browse files · 3 files+46 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+46 added−9 removed

  • other:+46 added−9 removed

3 files+46 added−9 removed

modified:click/_termui_impl.py+41 added-9 removed
527 unmodified lines
528529def _translate_ch_to_exc(ch):530 if ch == '\x03':531 raise KeyboardInterrupt()532 if ch == '\x04':532 if ch == '\x04' and not WIN: # Unix-like, Ctrl+D533 raise EOFError()534 if ch == '\x1a' and WIN: # Windows, Ctrl+Z535 raise EOFError()536537538if WIN:539 import msvcrt
2 unmodified lines
542 def raw_terminal():543 yield544545 def getchar(echo):544 rv = msvcrt.getch()546 # The function `getch` will return a bytes object corresponding to547 # the pressed character. Since Windows 10 build 1803, it will also548 # return \x00 when called a second time after pressing a regular key.549 #550 # `getwch` does not share this probably-bugged behavior. Moreover, it551 # returns a Unicode object by default, which is what we want.552 #553 # Either of these functions will return \x00 or \xe0 to indicate554 # a special key, and you need to call the same function again to get555 # the "rest" of the code. The fun part is that \u00e0 is556 # "latin small letter a with grave", so if you type that on a French557 # keyboard, you _also_ get a \xe0.558 # E.g., consider the Up arrow. This returns \xe0 and then \x48. The559 # resulting Unicode string reads as "a with grave" + "capital H".560 # This is indistinguishable from when the user actually types561 # "a with grave" and then "capital H".562 #563 # When \xe0 is returned, we assume it's part of a special-key sequence564 # and call `getwch` again, but that means that when the user types565 # the \u00e0 character, `getchar` doesn't return until a second566 # character is typed.567 # The alternative is returning immediately, but that would mess up568 # cross-platform handling of arrow keys and others that start with569 # \xe0. Another option is using `getch`, but then we can't reliably570 # read non-ASCII characters, because return values of `getch` are571 # limited to the current 8-bit codepage.572 #573 # Anyway, Click doesn't claim to do this Right(tm), and using `getwch`574 # is doing the right thing in more situations than with `getch`.575 if echo:546 msvcrt.putchar(rv)576 func = msvcrt.getwche577 else:578 func = msvcrt.getwch579580 rv = func()581 if rv in (u'\x00', u'\xe0'):582 # \x00 and \xe0 are control characters that indicate special key,583 # see above.584 rv += func()585 _translate_ch_to_exc(rv)548 if PY2:549 enc = getattr(sys.stdin, 'encoding', None)550 if enc is not None:551 rv = rv.decode(enc, 'replace')552 else:553 rv = rv.decode('cp1252', 'replace')586 return rv587else:588 import tty589 import termios
29 unmodified lines
modified:click/termui.py+4 added
555 unmodified lines
556557 Note that this will always read from the terminal, even if something558 is piped into the standard input.559560 Note for Windows: in rare cases when typing non-ASCII characters, this561 function might wait for a second character and then return both at once.562 This is because certain Unicode characters look like special-key markers.563564 .. versionadded:: 2.0565566 :param echo: if set to `True`, the character read will also show up on567 the terminal. The default is to not show it.
39 unmodified lines
modified:CHANGES.rst+1 added
5 unmodified lines
6-----------78Unreleased910- ``click.getchar()`` now returns unicode in Python 3 on Windows, same as other platforms. (`#1108`_, `#1088`_, `#822`_, `#821`_, `#537`_)11- Drop support for Python 2.6 and 3.3. (`#976`_)12- Wrap ``click.Choice``'s missing message. (`#202`_, `#1000`_)13- Add native ZSH autocompletion support. (`#323`_, `#865`_)14- Document that ANSI color info isn't parsed from bytearrays in Python 2. (`#334`_)
542 unmodified lines