| 160 | |
| 161 | |
| 162 | def _raw_input(prompt="", stream=None, input=None, echo_char=None): |
| 163 | # This doesn't save the string in the GNU readline history. |
| 164 | if not stream: |
| 165 | stream = sys.stderr |
| 166 | if not input: |
| 167 | input = sys.stdin |
| 168 | prompt = str(prompt) |
| 169 | if prompt: |
| 170 | try: |
| 171 | stream.write(prompt) |
| 172 | except UnicodeEncodeError: |
| 173 | # Use replace error handler to get as much as possible printed. |
| 174 | prompt = prompt.encode(stream.encoding, 'replace') |
| 175 | prompt = prompt.decode(stream.encoding) |
| 176 | stream.write(prompt) |
| 177 | stream.flush() |
| 178 | # NOTE: The Python C API calls flockfile() (and unlock) during readline. |
| 179 | if echo_char: |
| 180 | return _readline_with_echo_char(stream, input, echo_char) |
| 181 | line = input.readline() |
| 182 | if not line: |
| 183 | raise EOFError |
| 184 | if line[-1] == '\n': |
| 185 | line = line[:-1] |
| 186 | return line |
| 187 | |
| 188 | |
| 189 | def _readline_with_echo_char(stream, input, echo_char): |