title: Why is fileinput.input object not lost when going out-of-scope?
body: <p>In the following code I would expect than python is freeing <code>fileinput.input</code> when I'm <code>return</code>ing in the middle my loop as it is going out-of-scope. However, when calling again my function fileinput tells me</p>
  
  <pre><code>raise RuntimeError, "input() already active"
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>def func(inplace):
      for line in fileinput.input(sys.argv[1], inplace=inplace):
          [..]
          if condition:
               return True
          [..]
  
  if func(False):
      func(True)
  </code></pre>
  
  <p>I would expect this behavior when using <code>yield</code> but not when using return.</p>
  
  <p>I'm using Python 2.7.3.</p>
  
  <p>Is there any way to force a reset of fileinput?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>When calling <code>fileinput.close()</code> before returning it works. Why is it not done implicitly?</p>
  
  <p><strong>EDIT 2</strong>: Thanks to @MatsLindh</p>
  
  <p>Replacing </p>
  
  <pre><code>for line in fileinput.input(sys.argv[1], inplace=inplace):
  </code></pre>
  
  <p>with</p>
  
  <pre><code>for line in fileinput.FileInput(sys.argv[1], inplace=inplace):
  </code></pre>
  
  <p>does what I want, because it returns an object which goes out-of-scope in a defined manner. I assumed that <code>fileinput.input()</code> does that, but no. It is using a global instance.</p>