Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92825

Re: ctypes and byte order

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'attributes': 0.07; 'incompatible': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:ctypes': 0.09; 'tuple': 0.09; 'typeerror:': 0.09; 'python': 0.11; 'skip:f 30': 0.15; 'array)': 0.16; 'brilliant': 0.16; 'peek': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'structure.': 0.16; 'wrote:': 0.16; 'documented': 0.18; '>>>': 0.20; '(or': 0.21; '"",': 0.22; 'ctypes': 0.22; 'code.': 0.23; 'passing': 0.23; 'slightly': 0.23; "i've": 0.24; 'import': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'required.': 0.26; 'least': 0.27; "doesn't": 0.28; 'actual': 0.29; "i'm": 0.29; 'arrays': 0.29; 'cat': 0.29; 'yields': 0.29; '-----': 0.29; 'array': 0.29; 'becomes': 0.31; 'print': 0.31; 'source': 0.31; 'structure': 0.32; 'class': 0.33; 'traceback': 0.33; 'file': 0.34; 'skip:c 30': 0.35; 'to:addr :python-list': 0.35; 'instance': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'instead': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'data': 0.40; 'received:de': 0.40; 'further': 0.60; 'more': 0.62; 'mar': 0.65; 'results': 0.66; 'reflection': 0.84; '2014,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: ctypes and byte order
Date Thu, 18 Jun 2015 14:24:03 +0200
Organization None
References <mlu6p5$dbo$1@ger.gmane.org> <678134472.4779269.1434626735692.JavaMail.root@sequans.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8c90.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.597.1434630262.13271.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434630262 news.xs4all.nl 2908 [2001:888:2000:d::a6]:53750
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:92825

Show key headers only | View raw


Jean-Michel Pichavant wrote:

> ----- Original Message -----
>> From: "Peter Otten" <__peter__@web.de>
>> becomes
>> 
>> $ cat be2.py
>> import ctypes, sys
>> 
>> iarray_be = ctypes.c_uint32.__ctype_be__*5
>> 
>> class Foo_be(ctypes.BigEndianStructure):
>>         _fields_ = [('bar', iarray_be)]
>> 
>> print sys.version
>> f_be = Foo_be((0,1,2,3,0x11223344))
>> print hex(f_be.bar[4])
>> 
>> $ python be2.py
>> 2.7.6 (default, Mar 22 2014, 22:59:56)
>> [GCC 4.8.2]
>> 0x11223344L
>> 
>> which might do what you want.
> 
> Brilliant !
> 
> I've tested it and it yields the exact same results (binary file content
> wise) than my "workaround" structure. But that's way better since my
> actual structure is more complex and arrays will be required.
> 
> Though I'm slightly puzzled by the ctypes author(s) choice, this is not
> documented and requires to peek into the source code. Dunder attributes
> are rarely part of an interface.

On further reflection it dawned on me that what may have made this work is 
passing in the tuple instead of an array:

>>> import ctypes
>>> array = ctypes.c_uint32 * 3
>>> class B(ctypes.BigEndianStructure):
...     _fields_ = [("bar", array)]
... 
>>> b = B((1,2,3))
>>> b.bar
<ctypes._endian.c_uint_be_Array_3 object at 0x7f87700798c0>
>>> b.bar[2]
3L
>>> b = B(array(1,2,3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: incompatible types, c_uint_Array_3 instance instead of 
c_uint_be_Array_3 instance

Oops...

So the only thing that doesn't work transparently is initialising a data 
structure (or at least array) from its "other-endian" twin.


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: ctypes and byte order Peter Otten <__peter__@web.de> - 2015-06-18 14:24 +0200

csiph-web