This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes bitfield problem
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Can not set value for structure members larger than 32 bits
View: 6493
Assigned To: Nosy List: ned.deily, ocean-city, stutzbach, theller
Priority: normal Keywords:

Created on 2010-09-29 18:34 by stutzbach, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg117637 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-09-29 18:34
The following program should print "0xdead" but instead prints "0x0".  This came from the following stackoverflow question:
http://stackoverflow.com/questions/3824617/python-structure-always-stuck-at-0-no-matter-what-value-you-assign-to-it

import ctypes
class Blah(ctypes.Structure):
    _fields_ = [("a", ctypes.c_uint64, 64),
                ("b", ctypes.c_uint16, 16),
                ("c", ctypes.c_uint8, 8),
                ("d", ctypes.c_uint8, 8)]

x = Blah(0xDEAD,0xBEEF,0x44,0x12)
print(hex(x.a))
msg117663 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2010-09-29 22:00
An endian issue? (i.e. on OS X, it works on ppc but not Intel)
A slightly more detailed test case:

$ more test_ctypes.py
import ctypes
class Blah2(ctypes.Structure):
    _fields_ = [("x", ctypes.c_uint64, 64),
                ("y", ctypes.c_uint64)]
val = 0x0123456789ABCDEF
x = Blah2(val, val)
print(Blah2.x)
print(hex(x.x))
print(Blah2.y)
print(hex(x.y))
$ arch -i386 python2.7 test_ctypes.py
<Field type=c_ulonglong, ofs=0:0, bits=64>
0x0L
<Field type=c_ulonglong, ofs=8, size=8>
0x123456789abcdefL
$ arch -x86_64 python2.7 test_ctypes.py
<Field type=c_ulong, ofs=0:0, bits=64>
0x0L
<Field type=c_ulong, ofs=8, size=8>
0x123456789abcdefL
$ arch -ppc python2.7 test_ctypes.py
<Field type=c_ulonglong, ofs=0:0, bits=64>
0x123456789abcdefL
<Field type=c_ulonglong, ofs=8, size=8>
0x123456789abcdefL
msg117664 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-09-29 22:04
Could be.  FWIW, I had tested and observed the problem on a 32-bit Intel Windows box and 64-bit Intel Linux box.
msg117914 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-03 15:31
I think this issue is duplicate of #6493.
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54198
2010-10-03 15:31:37ocean-citysetstatus: open -> closed

nosy: + ocean-city
messages: + msg117914

superseder: Can not set value for structure members larger than 32 bits
resolution: duplicate
2010-09-29 22:04:37stutzbachsetmessages: + msg117664
2010-09-29 22:00:13ned.deilysetnosy: + ned.deily
messages: + msg117663
2010-09-29 18:34:52stutzbachcreate