Added try/except block for user_input in ipautil

Added error handling for function user_input in order to
handle EOFError in ipautil.py

https://fedorahosted.org/freeipa/ticket/3406

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
Abhijeet Kasurde
2015-08-19 17:13:43 +05:30
committed by Martin Basti
parent 0abaf195dc
commit 7c48621bb8

View File

@@ -747,30 +747,40 @@ def ipa_generate_password(characters=None,pwd_len=None):
def user_input(prompt, default = None, allow_empty = True):
if default == None:
while True:
ret = raw_input("%s: " % prompt)
if allow_empty or ret.strip():
return ret
try:
ret = raw_input("%s: " % prompt)
if allow_empty or ret.strip():
return ret
except EOFError:
if allow_empty:
return ''
raise RuntimeError("Failed to get user input")
if isinstance(default, basestring):
while True:
ret = raw_input("%s [%s]: " % (prompt, default))
if not ret and (allow_empty or default):
try:
ret = raw_input("%s [%s]: " % (prompt, default))
if not ret and (allow_empty or default):
return default
elif ret.strip():
return ret
except EOFError:
return default
elif ret.strip():
return ret
if isinstance(default, bool):
if default:
choice = "yes"
else:
choice = "no"
choice = "yes" if default else "no"
while True:
ret = raw_input("%s [%s]: " % (prompt, choice))
if not ret:
try:
ret = raw_input("%s [%s]: " % (prompt, choice))
if not ret:
return default
elif ret.lower()[0] == "y":
return True
elif ret.lower()[0] == "n":
return False
except EOFError:
return default
elif ret.lower()[0] == "y":
return True
elif ret.lower()[0] == "n":
return False
if isinstance(default, int):
while True:
try:
@@ -780,6 +790,8 @@ def user_input(prompt, default = None, allow_empty = True):
ret = int(ret)
except ValueError:
pass
except EOFError:
return default
else:
return ret