From 3f6bb27ce6dac85bdcb9cf584dcbfe4813b7a0f9 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Sat, 25 Jun 2011 21:53:30 +0200 Subject: [PATCH] [entropy.output] improve _my_raw_input(), use readline if available --- libraries/entropy/output.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/libraries/entropy/output.py b/libraries/entropy/output.py index 759b445ee..5697ab5a0 100644 --- a/libraries/entropy/output.py +++ b/libraries/entropy/output.py @@ -618,19 +618,25 @@ def readtext(request, password = False): return text def _my_raw_input(txt = ''): - if txt: + try: + import readline + except ImportError: + # not available? ignore + pass + + if not txt: + txt = "" + if sys.hexversion >= 0x3000000: try: - sys.stdout.write(darkgreen(txt)) + response = input(darkgreen(txt)) except UnicodeEncodeError: - sys.stdout.write(darkgreen(txt.encode('utf-8'))) + response = input(darkgreen(txt.encode('utf-8'))) + else: + try: + response = raw_input(darkgreen(txt)) + except UnicodeEncodeError: + response = raw_input(darkgreen(txt.encode('utf-8'))) _flush_stdouterr() - response = '' - while True: - y = sys.stdin.read(1) - if y in ('\n', '\r',): - break - response += y - _flush_stdouterr() # try to convert to unicode, because responses are stored that # way, fix bug #2006.