def pseudo_raw_input(self, prompt):
"""
began life as a copy of cmd's cmdloop; like raw_input but
- accounts for changed stdin, stdout
- if input is a pipe (instead of a tty), look at self.echo
to decide whether to print the prompt and the input
"""
# Deal with the vagaries of readline and ANSI escape codes
safe_prompt = self._surround_ansi_escapes(prompt)
if self.use_rawinput:
try:
if sys.stdin.isatty():
line = sm.input(safe_prompt)
else:
line = sm.input()
if self.echo:
sys.stdout.write('{}{}\n'.format(safe_prompt, line))
except EOFError:
line = 'eof'
else:
if self.stdin.isatty():
# on a tty, print the prompt first, then read the line
self.poutput(safe_prompt, end='')
self.stdout.flush()
line = self.stdin.readline()
if len(line) == 0:
line = 'eof'
else:
# we are reading from a pipe, read the line to see if there is
# anything there, if so, then decide whether to print the
# prompt or not
line = self.stdin.readline()
if len(line):
# we read something, output the prompt and the something
if self.echo:
self.poutput('{}{}'.format(safe_prompt, line))
else:
line = 'eof'
return line.strip()
评论列表
文章目录