I'm trying to create a bitfield class using ctypes in IronPython 2.7.9. I'm trying the example in this answer. but it is failing with a SystemError: Object reference not set to an instance of an object
at the line flags.asbyte = 0xc
, when accessing the Union member.
I also tried to point to CPython stdlib as suggested in this message by adding a CPython 2.7.8 path, but this didn't work either.
import sys
sys.path.append("C:/Python27/DLLs")
import ctypes
c_uint8 = ctypes.c_uint8
class Flags_bits(ctypes.LittleEndianStructure):
_fields_ = [
("logout", c_uint8, 1),
("userswitch", c_uint8, 1),
("suspend", c_uint8, 1),
("idle", c_uint8, 1),
]
class Flags(ctypes.Union):
_fields_ = [("b", Flags_bits),
("asbyte", c_uint8)]
flags = Flags()
flags.asbyte = 0xc
print(flags.b.idle)
print(flags.b.suspend)
print(flags.b.userswitch)
print(flags.b.logout)
An example of ctypes on IronPython? could be interesting, but the accepted answer doesn't prove an example.
EDIT: I dug a little further and this code (inspired from this unit test) doesn't work:
from ctypes import *
class ANON(Union):
_fields_ = [("a", c_int),
("b", c_int)]
a = ANON()
a.a = 5
While this piece of code (from this unit test) works:
from ctypes import *
class X(Structure):
_fields_ = [("a", c_longlong, 1),
("b", c_longlong, 62),
("c", c_longlong, 1)]
x = X()
x.a, x.b, x.c = -1, 7, -1
So it seems an IronPython limitation
Thanks!