title: Pickling a ctypes.Structure with ct.Pointer
body: <p>I have the following classes of a <code>ctypes</code> Structure.</p>
  
  <pre><code>import ctypes as ct
  
  class my_array(ct.Structure):
      _fields_ = [("_data", ct.POINTER(ct.c_uint32)),
                  ("_size", ct.c_size_t)]
  
      def __init__(self, data):
          self._data = (ct.c_uint32 * len(data))()
          for i in range(0, len(data)):
              self._data[i] = data[i]
              self._size = len(data)
  
      def __reduce__(self):
          data = [None]*self._size
          for j in xrange(0, self._size):
              data[j] = self._data[j]
          return (my_array, (data,))
  
  
  class my_struct(ct.Structure):
      _fields_ = [("numbs", my_array)]
  </code></pre>
  
  <p>However, when I pickle the second object</p>
  
  <pre><code>import cPickle
  a = my_array([1, 2, 10])
  b = my_struct(a)
  
  with open('myfile', 'wb') as f:
      cPickle.dump(a, f)  # This succeeds
      cPickle.dump(b, f)  # This fails
  </code></pre>
  
  <p>I get the exception</p>
  
  <pre><code>Traceback (most recent call last):
    File "tmp.py", line 30, in &lt;module&gt;
      cPickle.dump(a, f)  # This fails
  ValueError: ctypes objects containing pointers cannot be pickled
  </code></pre>
  
  <p>I don't understand why this is happening since I implemented <code>__reduce__</code> in <code>my_array</code>? Implementing <code>__getstate__</code> did not work either.</p>
  
  <p>I know I can overload <code>__reduce__</code> again in <code>my_struct</code> but this seems overly complicated for me since then I have to keep overloading <code>__reduce__</code> everytime I include <code>my_array</code> in a structure.</p>