I am trying to "monkey patch" a class derived from Python ctypes "Union", but I am unable to do so - getting weird errors and sometimes seg-faults. The same thing works quite well when deriving from ctypes "Structure".
I have narrowed this to down to the simplest possible test case which I am posting below. I am using Python 3.6.4. I am wondering if I am doing something wrong (or is there a problem with the ctypes "Union" implementation?).
The example python code is attached with this issue.
Please see the output below (for the attached code in Python 3.6.4).
Using Structure:
----------------
Print Type #1: [ 10, 20 ]
Original : <function MyStruct.__str__ at 0x7fdf89d02e18>
Patched : <function display at 0x7fdf8b0ebe18>
Patched (dict) : <function display at 0x7fdf8b0ebe18>
Print Type #2: ( 10, 20 )
Using Union:
------------
Print Type #1: [ 20, 20 ]
Original : <function MyUnion.__str__ at 0x7fdf89d02f28>
Patched : <function MyUnion.__str__ at 0x7fdf89d02f28>
Patched (dict) : <function display at 0x7fdf8b0ebe18>
Traceback (most recent call last):
File "ctypes_bug.py", line 54, in <module>
print(a)
TypeError: 'managedbuffer' object is not callable
Clearly, I am able to "patch" __str__ when the corresponding Python object was derived from "Structure", but I am unable to "patch" __str__ when the corresponding Python object was derived from "Union".
Interestingly, MyUnion.__dict__[__str__] and MyUnion.__str__ shows different results - which is weird as well.
Is there something I am doing wrong here, or is there an issue with ctypes.Union? I highly appreciate any help or insight!
|