title: Python: equivalent of input using sys.stdin
body: <p>I want to test some (python 3) code that directly uses the <code>print</code> and <code>input</code> functions.  As I understand it, the easiest way to do this is by dependency injection: modifying the code so that it takes input and output streams as arguments, using <code>sys.stdin</code> and <code>sys.stdout</code> by default and passing in mock objects during testing.  It's obvious what to do with <code>print</code> calls:</p>
  
  <pre><code>print(text)
  #replaced with...
  print(text, file=output_stream)
  </code></pre>
  
  <p>However, <code>input</code> doesn't have arguments for input and output streams.  Does the following code correctly reproduce its behaviour?</p>
  
  <pre><code>text = input(prompt)
  #replaced with...
  print(prompt, file=output_stream, end='')
  text = input_stream.readline()[:-1]
  </code></pre>
  
  <p>I had a look at the implementation of <code>input</code>, and it does quite a lot of magic, calling <code>sys.stdin.fileno</code> and examining <code>sys.stdin.encoding</code> and <code>sys.stdin.errors</code> rather than calling any of the <code>read*</code> methods - I wouldn't know where to start with mocking those.</p>