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: Monkey Patching class derived from ctypes.Union doesn't work
Type: behavior Stage:
Components: ctypes Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mjpieters, rvijayc
Priority: normal Keywords:

Created on 2018-11-30 19:36 by rvijayc, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
ctypes_bug.py rvijayc, 2018-11-30 19:36 Demo file for possible ctypes "Union" issue.
Messages (3)
msg330817 - (view) Author: Vijay (rvijayc) Date: 2018-11-30 19:36
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!
msg330822 - (view) Author: Martijn Pieters (mjpieters) * Date: 2018-11-30 19:58
This is a repeat of old-tracker issue 1700288, see https://github.com/python/cpython/commit/08ccf202e606a066668f4ef85df9a9c0d07e1ba1#diff-998bfefaefe2ab83d5f523e18f158fa4, which fixed this for StructType_setattro but failed to do the same for UnionType_setattro
msg330900 - (view) Author: Vijay (rvijayc) Date: 2018-12-02 21:46
Wanted to post a possible workaround until this is fixed. One can simply wrap a union anonymously inside a structure. Like so:

struct my_struct_s {
  union my_union_u {
    <original union contents>
  };
};

This temporarily solved the problem in my case.
History
Date User Action Args
2022-04-11 14:59:08adminsetgithub: 79547
2018-12-02 21:46:41rvijaycsetmessages: + msg330900
2018-11-30 19:58:45mjpieterssetnosy: + mjpieters
messages: + msg330822
2018-11-30 19:36:49rvijayccreate