[Python-Dev] adding Construct to the standard library? (original) (raw)

Paul Moore p.f.moore at gmail.com
Tue Apr 18 21:25:34 CEST 2006


On 4/17/06, tomer filiba <tomerfiliba at gmail.com> wrote:

after several people (several > 10) contacted me and said "IMHO 'construct' is a good candidate for stdlib", i thought i should give it a try. of course i'm not saying it should be included right now, but in 6 months time, or such a timeframe (aiming at python 2.6? some 2.5.x release?)

Now that ctypes is part of the standard library, that provides a structured datatype facility. Here's an example demonstrating the first example from the Construct wiki:

from ctypes import *

def strtoctype(s, typ): ... t = typ() ... memmove(addressof(t), s, sizeof(t)) ... return t ... class ethernetheader(Structure): ... fields = [("destination", c_char * 6), ... ("source", c_char * 6), ... ("type", c_short)] ... s = "ABCDEF123456\x08\x00" e = strtoctype(s, ethernetheader)

e.source '123456' e.destination 'ABCDEF' e.type 8

I'm not sure I understand the other wiki examples - but the ones I do, look doable in ctypes.

There are a couple of things to note:

Personally, I'd rather see the ctypes facilities for structure packing and unpacking be better documented, and enhanced if necessary, rather than having yet another way of doing the same thing added to the stdlib.

Paul.



More information about the Python-Dev mailing list