Fix AskUser to not die on extra input
authorIustin Pop <iustin@google.com>
Wed, 10 Oct 2007 14:04:51 +0000 (14:04 +0000)
committerIustin Pop <iustin@google.com>
Wed, 10 Oct 2007 14:04:51 +0000 (14:04 +0000)
Currently, AskUser dies with -ESPIPE if the user gives more than one
character plus newline. This is because the python library, while
returning only two chars from the readline(2) call, will cache the rest
of the input, and when we do a write, it will try to seek back to just
after the last returned char. This fails on /dev/tty, so an exception is
raised. However, then opening the file descriptor in O_APPEND mode,
python will not issue such seeks, thereby fixing this problem.

The other alternative, opening in unbuffered mode, is not as good, since
then python will issue one-byte reads at a time, but the tty will read
the entire line, so at the next readline call, whatever remained in the
descriptor buffer is returned from the kernel to python, and the user
doesn't get a chance to enter something at all.

Reviewed-by: imsnah

lib/cli.py

index 9a6784d..85606d4 100644 (file)
@@ -311,7 +311,7 @@ def AskUser(text, choices=None):
     new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
   text = "\n".join(new_text)
   try:
-    f = file("/dev/tty", "r+")
+    f = file("/dev/tty", "a+")
   except IOError:
     return answer
   try: