title: How to mock numpy.fromfile?
body: <p>I'm trying to pass <code>numpy.fromfile</code> a mocked <code>file</code>, and I keep getting the following error:</p>
  <blockquote>
  <p>IOError: !first argument must be an open file</p>
  </blockquote>
  <p>Here's a basic example, using <code>mock_open</code> taken from the <a href="http://www.voidspace.org.uk/python/mock/examples.html#mocking-open" rel="nofollow noreferrer">Further Example</a> section of mock package:</p>
  <pre><code>import numpy as np
  from mock import MagicMock, patch
  
  file_spec = file
  
  def mock_open(mock=None, data=None):
      if mock is None:
          mock = MagicMock(spec=file_spec)
  
      handle = MagicMock(spec=file_spec)
      handle.write.return_value = None
      if data is None:
          handle.__enter__.return_value = handle
      else:
          handle.__enter__.return_value = data
      mock.return_value = handle
      return mock
  
  
  if __name__ == &quot;__main__&quot;:
      m = mock_open()
      with patch('__main__.open', m, create=True):
          with open('foo', 'rb') as h:
              print h.mock_calls
              try:
                  result = np.fromfile(h)
              except IOError:
                  print &quot;io error!&quot;
                  print h.mock_calls
  </code></pre>
  <p>resulting in:</p>
  <pre><code>% [call.__enter__()] 
  % io error!
  % [call.__enter__()]
  </code></pre>
  <p>I could just mock <code>numpy.fromfile</code>, and that's probably what I'll end up doing. I had trouble understanding the follow</p>
  <ol>
  <li>why the above example does not work</li>
  <li>why I have trouble debugging <code>fromfile</code> (by calling <code>pdb.set_trace()</code> just before <code>fromfile</code> and stepping into <code>fromfile</code>. The debugger seems to step over the call no matter what.</li>
  <li>why I can't find <code>fromfile</code> in the <code>numpy</code> source code. Such a function exists in <code>numpy/numarray/functions.py</code>, <code>numpy/core/records.py</code> and <code>numpy/ma/core.py</code>, but none of these raise an error as the one listed above.</li>
  </ol>
  <p>Any help clarifying the above issues is much appreciated. I'm using <code>numpy</code> version 1.6.1 by the way.</p>