我遇到了一个依赖于操作系统的问题,在pythonctype中定义了一个相对简单的位字段。位域在Windows7x64下运行良好,但在Ubuntu12.04x64中却失败了。两个操作系统都使用python2.7解释器。在
附加代码突出了我的问题。如果我的位域是用c\u uint定义的,它在Ubuntu中就失败了。如果我把它换成了库乌肖特,它会像预期的那样工作。Windows似乎可以使用这两种定义。在
在Windows中,以下代码输出:1 1 3 1 6 7 8 9 10 11 12 13 14 15 0
1 1 3 1 6 7 8 9 10 11 12 13 14 15 0
在Linux中,以下代码输出:
^{pr2}$
>在Ubuntu/GCC 4和Win/MsC9下,C++中有^ {A1},C++都是定义良好的。所以我得出结论,这是一个Python特有的问题。任何帮助都将不胜感激。在
更新:根据Markus的建议,它似乎是一个Python错误。所以我在http://bugs.python.org/issue20629上对Python bug tracker提交了一个bugimport ctypes
class BitField1(ctypes.BigEndianStructure):
_pack_ = 1
_fields_ = [
('a', ctypes.c_uint, 3),
('b', ctypes.c_uint, 1),
('c', ctypes.c_uint, 3),
('d', ctypes.c_uint, 1),
('f', ctypes.c_uint, 8),
('g', ctypes.c_uint, 16),
('h', ctypes.c_uint, 16),
('i', ctypes.c_uint, 16),
('j', ctypes.c_uint, 8),
('k', ctypes.c_uint, 8),
('l', ctypes.c_uint, 8),
('m', ctypes.c_uint, 8),
('n', ctypes.c_ulonglong, 56),
('o', ctypes.c_ulonglong, 4),
('p', ctypes.c_ulonglong, 4)
]
class BitField1U(ctypes.Union):
_pack_ = 1
_fields_ = [("fields", BitField1),
("raw_bytes", ctypes.c_ubyte * 20)]
class BitField2(ctypes.BigEndianStructure):
_pack_ = 1
_fields_ = [
('a', ctypes.c_ushort, 3),
('b', ctypes.c_ushort, 1),
('c', ctypes.c_ushort, 3),
('d', ctypes.c_ushort, 1),
('f', ctypes.c_ushort, 8),
('g', ctypes.c_ushort, 16),
('h', ctypes.c_ushort, 16),
('i', ctypes.c_ushort, 16),
('j', ctypes.c_ushort, 8),
('k', ctypes.c_ushort, 8),
('l', ctypes.c_ushort, 8),
('m', ctypes.c_ushort, 8),
('n', ctypes.c_ulonglong, 56),
('o', ctypes.c_ulonglong, 4),
('p', ctypes.c_ulonglong, 4)
]
class BitField2U(ctypes.Union):
_pack_ = 1
_fields_ = [("fields", BitField2),
("raw_bytes", ctypes.c_ubyte * 20)]
def setup(fields) :
fields.a = 1
fields.b = 1
fields.c = 3
fields.d = 1
fields.f = 6
fields.g = 7
fields.h = 8
fields.i = 9
fields.j = 10
fields.k = 11
fields.l = 12
fields.m = 13
fields.n = 14
fields.o = 15
fields.p = 16
def printFields(fields) :
print(fields.a),
print(fields.b),
print(fields.c),
print(fields.d),
print(fields.f),
print(fields.g),
print(fields.h),
print(fields.i),
print(fields.j),
print(fields.k),
print(fields.l),
print(fields.m),
print(fields.n),
print(fields.o),
print(fields.p)
b1 = BitField1U()
b2 = BitField2U()
setup(b1.fields)
setup(b2.fields)
printFields(b1.fields)
printFields(b2.fields)