title: Casting a bytearray instance in ctypes
body: <p>When developing in pycharm, the code below sometimes fails (the contents of the casted structure changes) when I run a coverage unit test, but not when running it as a unit test. The following is the relevant part of the code:</p>
  
  <pre><code>class SomeStruct(ctypes.Structure):
      _fields_ = [
          ('field', ctypes.c_char)
      ]
  
  res = bytearray(b'12345')
  ctypes.cast(bytes(res), ctypes.POINTER(SomeStruct)).contents
  </code></pre>
  
  <p>However the following always works:</p>
  
  <pre><code>class SomeStruct(ctypes.Structure):
      _fields_ = [
          ('field', ctypes.c_char)
      ]
  
  res = bytearray(b'12345')
  res_bytes = bytes(res)
  ctypes.cast(res_bytes, ctypes.POINTER(SomeStruct)).contents
  </code></pre>
  
  <p>This seems to me like some reference counting problem, however I'm not sure. Why does this problem occur? And what is the proper way to cast a bytearray instance in ctypes?</p>
  
  <p>The python version is 3.4.0, x64.</p>