title: Getting data from ctypes array into numpy
body: <p>I am using a Python (via <code>ctypes</code>) wrapped C library to run a series of computation.  At different stages of the running, I want to get data into Python, and specifically <code>numpy</code> arrays.</p>
  
  <p>The wrapping I am using does two different types of return for array data (which is of particular interest to me):</p>
  
  <ul>
  <li><p><strong><code>ctypes</code> Array</strong>:  When I do <code>type(x)</code> (where x is the <code>ctypes</code> array, I get a <code>&lt;class 'module_name.wrapper_class_name.c_double_Array_12000'&gt;</code> in return.  I know that this data is a copy of the internal data from the documentation and I am able to get it into a <code>numpy</code> array easily:</p>
  
  <pre><code>&gt;&gt;&gt; np.ctypeslib.as_array(x)
  </code></pre></li>
  </ul>
  
  <p>This returns a 1D <code>numpy</code> array of the data.</p>
  
  <ul>
  <li><p><strong><code>ctype</code> pointer to data</strong>:  In this case from the library's documentation, I understand that I am getting a pointer to the data stored and used directly to the library.  Whey I do <code>type(y)</code> (where y is the pointer) I get <code>&lt;class 'module_name.wrapper_class_name.LP_c_double'&gt;</code>.  With this case I am still able to index through the data like <code>y[0][2]</code>, but I was only able to get it into numpy via a super awkward:</p>
  
  <pre><code>&gt;&gt;&gt; np.frombuffer(np.core.multiarray.int_asbuffer(
      ctypes.addressof(y.contents), array_length*np.dtype(float).itemsize))
  </code></pre></li>
  </ul>
  
  <p>I found this in an old <code>numpy</code> mailing list <a href="http://mail.scipy.org/pipermail/numpy-discussion/2008-September/037131.html" rel="noreferrer">thread from Travis Oliphant</a>, but not in the <code>numpy</code> documentation.  If instead of this approach I try as above I get the following:</p>
  
  <pre><code>&gt;&gt;&gt; np.ctypeslib.as_array(y)
  ...
  ...  BUNCH OF STACK INFORMATION
  ...
  AttributeError: 'LP_c_double' object has no attribute '__array_interface__'
  </code></pre>
  
  <p><strong>Is this <code>np.frombuffer</code> approach the best or only way to do this? I am open to other suggestions but must would still like to use <code>numpy</code> as I have a lot of other post-processing code that relies on <code>numpy</code> functionality that I want to use with this data</strong>.</p>