[Python-Dev] adding Construct to the standard library?

Thomas Heller theller at python.net
Tue Apr 18 21:45:03 CEST 2006


Paul Moore wrote:
> 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 str_to_ctype(s, typ):
> ...    t = typ()
> ...    memmove(addressof(t), s, sizeof(t))
> ...    return t
> ...
>>>> class ethernet_header(Structure):
> ...  _fields_ = [("destination", c_char * 6),
> ...              ("source", c_char * 6),
> ...              ("type", c_short)]
> ...
>>>> s = "ABCDEF123456\x08\x00"
>>>> e = str_to_ctype(s, ethernet_header)
> 
>>>> 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:
> 
> * ctypes doesn't have a way (that I'm aware of) to specify the
> endianness of types like c_short - so my example, when run on Windows
> (intel architecture) gives type = 8, rather than type = 2048 (from the
> wiki). But the wiki example doesn't explicitly specify endianness, so
> maybe that's a limitation in Construct as well?

The currently undocumented BigEndianStructure or LittleEndianStructure
bases classes allow to specify the byte order.  They are designed so
that they refuse to work when the structure contains pointer fields.

> * ctypes doesn't have an easy way to parse a string based on a
> structure definition - hence my str_to_ctype function. But that's a
> trivial helper function to write, so that's not a big issue.
> 
> 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.

It is not yet too late (but the timeslot left is very small) to propose
enhancements to ctypes.  classmethods like 'from_string', 'from_buffer' or
whatever would probably make sense.

Thomas



More information about the Python-Dev mailing list