ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ctypes-users
ctypes-users
[ctypes-users] Re: Byte ordering
by Thomas Heller other posts by this author
Nov 8 2005 10:13AM messages near this date
[ctypes-users] CVS snapshot | Re: [ctypes-users] Re: Byte ordering
Nicolas Pinault <nicolasp@[...].com>  writes:

>  Hello,
> 
>  I need to access data structures which are in big-endian format. I
>  would like to be able to declare and use Structures which manage this
>  automaticaly.
>  A keyword could say the structure contains big-endian data. When
>  accessing this structure fields, byte ordering would be done
>  automaticaly.

This is not the first time this request comes up, although personally I
never had a need for it.

First, there are some 'Struct' modules that I know of which are based on
the standard Python struct module, and so can handle different byte
orders.  (Bob Ippolito has something called ptypes, iirc, and Just van
Rossum has sstruct, but I'm less sure on the name of the latter).
Neither of them is compatible with ctypes, afaik.

Second, I would probably accept a patch for ctypes if someone provides
one, although the C code can get hairy because it must also support bit
fields.

Third, if speed isn't too important, this can be done with a custom
metaclass.  I've experimented a bit, and wrote an only slightly tested
module that allows to define Structure types in network byte order - it
as attached to this email.  There are some limitations: it only supports
char/bytem, short, and long, it uses the socket module functions
ntohl/htonl and htons/ntohs to do the byte swapping, doesn't support bit
fields. Let me explain how it works:

The NetworkByteOrdered_Structure metaclass creates the new structure
type, then loops over the defined _fields_. It replaces the descriptors
that ctypes has created for the field by new descriptors that do the
byte swapping when assigning to a field and when retrieving a field's
value.

As usual, I would appreciate feedback ;-)

Thomas
Attachments:
unknown1
y.py

Thread:
Thomas Heller
Bob Ippolito
Thomas Heller

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved