summaryrefslogtreecommitdiffstats
path: root/ipapython
diff options
context:
space:
mode:
authorAbhijeet Kasurde <akasurde@redhat.com>2015-08-19 17:13:43 +0530
committerMartin Basti <mbasti@redhat.com>2015-08-19 15:41:57 +0200
commit7c48621bb8e9efc47c68bb7b4af936da93325050 (patch)
treef71b063be3f651d294e2d2afe0a1ba1e59fc2ebb /ipapython
parent0abaf195dc3b0920d2439dd4ec6df61e0aadc4f9 (diff)
downloadfreeipa-7c48621bb8e9efc47c68bb7b4af936da93325050.tar.gz
freeipa-7c48621bb8e9efc47c68bb7b4af936da93325050.tar.xz
freeipa-7c48621bb8e9efc47c68bb7b4af936da93325050.zip
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>
Diffstat (limited to 'ipapython')
-rw-r--r--ipapython/ipautil.py46
1 files changed, 29 insertions, 17 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index ba8312e79..c3ffb1d5c 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -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