title: Python ctypes: copying Structure's contents
body: <p>I want to mimic a piece of C code in Python with ctypes, the code is something like:</p>
  
  <pre><code>typedef struct {
    int x;
    int y;
  } point;
  
  void copy_point(point *a, point *b) {
    *a = *b;
  }
  </code></pre>
  
  <p>in ctypes it's not possible to do the following:</p>
  
  <pre><code>from ctypes import *
  
  class Point(Structure):
    _fields_ = [("x", c_int),("y", c_int)]
  
  def copy_point(a, b):
    a.contents = b.contents
  
  p0 = pointer(Point())
  p1 = pointer(Point())
  copy_point(p0,p1)
  </code></pre>
  
  <p>as the <code>contents</code> still is a Python ctypes Structure object, that is managed as a reference itself.</p>
  
  <p>An obvious workaround would be to manually copy each field (that is represented as immutable python int's), but that doesn't scale with more complex structures. Also, it would need to be done recursively for fields that are not basic, but structured types.</p>
  
  <p>My other option is to use <code>memmove</code> and copy the objects as if they were buffers, but that seems very error prone (as Python is dynamically typed it would be too easy to use it with objects of distinct type and size, leading to memory corruption or segmentation faults)...</p>
  
  <p>Any suggestions?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>I could also use a fresh new copy of the structure, so maybe this could be useful:</p>
  
  <pre><code>import copy
  p0 = Point()
  p1 = copy.deepcopy(p0) #or just a shallow copy for this example
  </code></pre>
  
  <p>but I don't know if there might be some kind of bizarre behaviours copying ctypes proxys as if they were regular Python objects...</p>