[entropy.output] improve _my_raw_input(), use readline if available

This commit is contained in:
Fabio Erculiani
2011-06-25 21:53:30 +02:00
parent cfa7476f9d
commit 3f6bb27ce6
+16 -10
View File
@@ -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.