The PythonInfo wiki has a solution for this.
FAQ: How do I copy bytes to Python from a ctypes.Structure?
def send(self): return buffer(self)[:]
FAQ: How do I copy bytes to a ctypes.Structure from Python?
def receiveSome(self, bytes): fit = min(len(bytes), ctypes.sizeof(self)) ctypes.memmove(ctypes.addressof(self), bytes, fit)
Their send
is the (more-or-less) equivalent of pack
, and receiveSome
is sort of a pack_into
. If you have a “safe” situation where you’re unpacking into a struct of the same type as the original, you can one-line it like memmove(addressof(y), buffer(x)[:], sizeof(y))
to copy x
into y
. Of course, you’ll probably have a variable as the second argument, rather than a literal packing of x
.