I'm writing an application in python+gtk3 with pygobject 3.24
I'm developing custom widgets and need information about the call stack and return values of do_get_preferred_width
, do_size_allocate
, etc. in order to debug layout issues I'm having.
I'm trying to monkey patch these methods for regular Gtk Widgets like Gtk.Box, Gtk.Paned, etc. The new methods will just call the original methods but print their input/output to a log file so I can follow the layout logic.
I can accomplish this by creating subclasses for each of these, but since my application uses .ui
files which rely on the original classes, I can't install these patched classes and still use the templates (pygobject
doesn't support derived widgets in templates). Recreating the template logic within the application requires a lot of work.
Ideally, I could just monkey patch the methods on the fly like so:
builder = Gtk.Builder.new_from_file('my-template.ui')
box = builder.get_object('my-box')
box.do_get_preferred_width = patched_do_get_preferred_width
But this seems to have no effect. If I try to set it at the class level, it also doesn't work:
Gtk.Box.do_get_preferred_width = patched_do_get_preferred_width
The runtime seems to be completely unaware of these changes. I suspect it's due to the fact that the underlying calls are all in C. At this point I'm tempted to insert logging messages into the c source code and build it myself, but this feels like overkill.
Does anyone know how I can accomplish this monkey patching? Or easily obtain the information I need? (The GTK debugger isn't informative enough)