[pypy-issue] Issue #2066: ctypes.BigEndianStructure does not appear to swap byte order on PyPy (pypy/pypy)

David Wilson issues-reply at bitbucket.org
Tue Jun 16 09:38:14 CEST 2015


New issue 2066: ctypes.BigEndianStructure does not appear to swap byte order on PyPy
https://bitbucket.org/pypy/pypy/issue/2066/ctypesbigendianstructure-does-not-appear

David Wilson:

When run on a little endian machine, the output of the following script differs between CPython and PyPy:

````
import sys
import ctypes

FIELDS = [
    ('n', ctypes.c_uint16)
]

class Native(ctypes.Structure):
    _fields_ = FIELDS

class Big(ctypes.BigEndianStructure):
    _fields_ = FIELDS

class Little(ctypes.LittleEndianStructure):
    _fields_ = FIELDS

def dostruct(c):
    ba = ctypes.create_string_buffer(ctypes.sizeof(c))
    ms = c.from_buffer(ba)
    ms.n = 0xff00
    return repr(ba[:])

print 'Native', dostruct(Native)
print 'Big', dostruct(Big)
print 'Little', dostruct(Little)

if sys.byteorder == 'little':
    assert dostruct(Native) == dostruct(Little)
elif sys.byteorder == 'big':
    assert dostruct(Native) == dostruct(Big)
else:
    assert 0, 'typo'
````

On CPython, we see that the byte order of `Big` is swapped:

````
python p2.py
Native '\x00\xff'
Big '\xff\x00'
Little '\x00\xff'
````

Whereas on PyPy 2.6.0 it is not the case:

````
Native '\x00\xff'
Big '\x00\xff'
Little '\x00\xff'
````





More information about the pypy-issue mailing list