title: How to patch built-in/extension types in python using mockito?
body: <p>While using <a href="http://mockito-python.readthedocs.io/en/latest/" rel="nofollow noreferrer">mockito</a> for unit testing in python, I came across an issue that I cannot find a solution for. I am trying to patch the usage of <code>io.BytesIO</code> in a given class method. The following code shows a simplified version where the issue occurs:</p>
  
  <pre><code>from mockito import mock, patch, when
  from io import BytesIO
  
  class Foo:
      def bar(self):
          buffer = io.BytesIO()
          # ...
          return buffer.getvalue()
  
  def test_foo():
      bytesIO_mock = mock(strict=True)
      when(bytesIO_mock).getvalue().thenReturn('data')
  
      patch(BytesIO.__new__, lambda: bytesIO_mock)
      result = Foo().bar()
      assert result == 'data'
  </code></pre>
  
  <p>I am getting the following error when I execute the test:</p>
  
  <pre><code>/venv/lib/python3.6/site-packages/mockito/mockito.py:270: in patch
  when2(fn, Ellipsis).thenAnswer(replacement)
  /venv/lib/python3.6/site-packages/mockito/mockito.py:245: in when2
  return invocation.StubbedInvocation(theMock, name)(*args, **kwargs)
  /venv/lib/python3.6/site-packages/mockito/invocation.py:284: in __call__
  self.mock.stub(self.method_name)
  /venv/lib/python3.6/site-packages/mockito/mocking.py:117: in stub
  self.replace_method(method_name, original_method)
  /venv/lib/python3.6/site-packages/mockito/mocking.py:108: in replace_method
  self.set_method(method_name, new_mocked_method)
  
  self = &lt;mockito.mocking.Mock object at 0x10d50cb38&gt;, method_name = '__new__'
  new_method = &lt;function Mock.replace_method.&lt;locals&gt;.new_mocked_method at 0x10d753e18&gt;
  
      def set_method(self, method_name, new_method):
  &gt;       setattr(self.mocked_obj, method_name, new_method)
  E       TypeError: can't set attributes of built-in/extension type '_io.BytesIO'
  
  /venv/lib/python3.6/site-packages/mockito/mocking.py:74: TypeError
  </code></pre>
  
  <p>Is there any solution to this issue, or is it just the case it is not possible to mock certain objects in python?</p>