Commit e6855438 authored by Christian Flach's avatar Christian Flach Committed by LUCI CQ

Enable 'elaborate line editing' in `gclient_utils.AskForData`

The (raw_)input Python function used by `gclient_utils.AskForData`
supports additional 'elaborate line editing' [1] [2] features, such as
the ability to use backspace to fix a typo, if the readline module is
loaded. Make sure that readline is loaded by importing it locally in
`gclient_utils.AskForData`.
It does not appear to be possible to write a test that verifies this
behavior - assigning `io.StringIO` to `sys.stdin` appears to cause
(raw_)input to fall back to basic input processing.

[1] https://docs.python.org/2.7/library/functions.html#raw_input
[2] https://docs.python.org/3/library/functions.html#input

Change-Id: Ie1c25b3fa2f3a255b78426c20e47c968cd7198ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3306613Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Commit-Queue: Christian Flach <cmfcmf@chromium.org>
parent 5deb9c9a
......@@ -160,6 +160,15 @@ class PrintableObject(object):
def AskForData(message):
# Try to load the readline module, so that "elaborate line editing" features
# such as backspace work for `raw_input` / `input`.
try:
import readline
except ImportError:
# The readline module does not exist in all Python distributions, e.g. on
# Windows. Fall back to simple input handling.
pass
# Use this so that it can be mocked in tests on Python 2 and 3.
try:
if sys.version_info.major == 2:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment